package com.sheep.jiuyan.samllsheep;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.util.DisplayMetrics;

import com.kfzs.duanduan.react.SharedPreferences;
import com.sheep.gamegroup.di.components.DaggerNetComponent;
import com.sheep.gamegroup.di.components.NetComponent;
import com.sheep.gamegroup.di.modules.NetModule;
import com.sheep.gamegroup.model.entity.AppRecord;
import com.sheep.gamegroup.model.entity.TaskListen;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.gamegroup.util.AppUtil;
import com.sheep.gamegroup.util.ConnectAddress;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.MyDbManager;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.tencent.bugly.Bugly;

/**
 * Created by kemllor on 2017/12/21.
 */

public class SheepApp extends BaseApplication {

    public static Context mContext;
    public static SheepApp mSheepApp;
    public ConnectAddress connectAddress;


    private NetComponent netComponent;
    private AppRecord lastAppRecord;
    private long lastTime;
    private boolean isShowQB;
    private String packgeName;

    public String getPackgeName() {
        return packgeName;
    }

    public void setPackgeName(String packgeName) {
        this.packgeName = packgeName;
    }

    public boolean isShowQB() {
        return isShowQB;
    }

    public void setShowQB(boolean showQB) {
        isShowQB = showQB;
    }

    public AppRecord getLastAppRecord() {
        return lastAppRecord;
    }

    public long getLastTime() {
        return lastTime;
    }

    public void setLastAppRecord(AppRecord lastAppRecord) {
        this.lastAppRecord = lastAppRecord;
    }

    public void setLastTime(long lastTime) {
        this.lastTime = lastTime;
    }
    private TaskListen taskEty;

    public TaskListen getTaskEty() {
        return MyDbManager.getInstance().dbFindTask();
    }

    public void setTaskEty(TaskListen taskEty) {
        this.taskEty = taskEty;
    }

    public static SheepApp get(Context context) {


        return (SheepApp) context.getApplicationContext();
    }

    public ConnectAddress getConnectAddress() {
        return connectAddress;
    }

    public void setConnectAddress(ConnectAddress connectAddress) {
            if(this.connectAddress != connectAddress){
                SharedPreferences.getInstance().putString("flavor", connectAddress.name());
                quit();
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
            } else {
                G.showToast("已经是该服务器");
            }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if(AppUtil.isMainPross(this)) {
            mContext = getApplicationContext();
            mSheepApp = this;
            String flavor = SharedPreferences.getInstance().getString("flavor", BuildConfig.FLAVOR);
            if("sheep".equals(BuildConfig.FLAVOR)){
                flavor = BuildConfig.FLAVOR;
            }
            connectAddress = ConnectAddress.valueOf(flavor);

            registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
            initGoble();
            Bugly.init(getApplicationContext(), Config.BUGLY_APPID, BuildConfig.DEBUG);
            initNet();
        }
    }

    private void initNet() {
        netComponent = DaggerNetComponent.builder().netModule(new NetModule()).build();
    }


    /**
     * 初始化系统常用参数
     */
    private void initGoble() {
        DisplayMetrics metric = getResources().getDisplayMetrics();
        G.HEIGHT = metric.heightPixels;
        G.WIDTH = metric.widthPixels;
        G.DENSITY = metric.density;
    }

    public int mActivityAount;
    private boolean notStop = false;
    /**
     * Activity 生命周期监听，用于监控app前后台状态切换
     */
    ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            ActivityManager.getInstance().pushActivity(activity);
            if(mActivityAount == 0) {
                LogUtil.println("SheepApp SHEEP_START");
                UMConfigUtils.Event.SHEEP_START.onEvent();
            }
        }

        @Override
        public void onActivityStarted(Activity activity) {
            mActivityAount++;
        }

        @Override
        public void onActivityResumed(Activity activity) {
            UMConfigUtils.onPageStart(activity);
            if (mActivityAount > 0) {
                if(!notStop) {
                    notStop = true;
                    LogUtil.println("SheepApp SHEEP_RESUME");
                    UMConfigUtils.Event.SHEEP_RESUME.onEvent();
                }
                Jump2View.getInstance().startFloat(activity, false);
            }
        }

        @Override
        public void onActivityPaused(Activity activity) {
            UMConfigUtils.onPageEnd(activity);
        }

        @Override
        public void onActivityStopped(Activity activity) {
            mActivityAount--;
            if (mActivityAount <= 0) {
                if(notStop) {
                    notStop = false;
                    LogUtil.println("SheepApp SHEEP_PAUSE");
                    UMConfigUtils.Event.SHEEP_PAUSE.onEvent();
                }
                Jump2View.getInstance().startFloat(activity, true);
            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            if(mActivityAount == 0) {
                LogUtil.println("SheepApp SHEEP_STOP");
                UMConfigUtils.Event.SHEEP_STOP.onEvent();
            }
        }
    };

    public NetComponent getNetComponent() {
        return netComponent;
    }

    public static SheepApp getInstance() {
        return mSheepApp;
    }

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