package com.zhaohaoting.framework.application;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.multidex.MultiDex;

import com.zhaohaoting.framework.mvchelper.mvc.MVCHelper;
import com.zhaohaoting.framework.ZhtLibrary;
import com.zhaohaoting.framework.view.MyLoadViewFactory;

import org.litepal.LitePal;

import java.util.List;


/**
 * Created by xxh on 2018-4-10.
 */
public abstract class ZhtApplication extends Application {

    private static Context _context;
    private static Resources _resources;
    private static ZhtApplication instance = null;

    public abstract String getSpName();

    public abstract int getIcLauncher();

    public abstract Class<? extends Activity> getEntranceActivity();

    @Override
    public void onCreate() {
        super.onCreate();
        if (isMainProcess()) {
            _context = getApplicationContext();
            _resources = getResources();
            instance = this;
            // 设置LoadView的factory，用于创建使用者自定义的加载失败，加载中，加载更多等布局,写法参照DeFaultLoadViewFactory
            MVCHelper.setLoadViewFractory(new MyLoadViewFactory());
            LitePal.initialize(this);
            LitePal.getDatabase();
            ZhtLibrary.init(getContext(), getIcLauncher(), getEntranceActivity(), getSpName());
//            if (BuildConfig.DEBUG) {
//                LeakCanary.install(this);
//            }
        }
    }

    public static ZhtApplication getInstance() {
        return instance;
    }

    public static Context getContext() {
        return _context;
    }

    public static Resources getAppResource() {
        return _resources;
    }


    public static int getDimentoPx(int dimen) {
        return getAppResource().getDimensionPixelOffset(dimen);
    }

    public static int getVersionCode() {
        int localVersion = 0;
        try {
            PackageInfo packageInfo = getContext().getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(getContext().getPackageName(), 0);
            localVersion = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /**
     * 设置字体不跟随系统字体
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

    public boolean isMainProcess() {
        return getCurProcessName(this).equals(getApplicationInfo().packageName);
    }

    protected String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> list = activityManager.getRunningAppProcesses();
        if (list != null) {
            for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : list) {
                if (runningAppProcessInfo.pid == pid) {
                    return runningAppProcessInfo.processName;
                }
            }
        }
        return "";
    }
}
