package com.stm.asset.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.widget.Toast;

import androidx.multidex.MultiDex;

import com.stm.asset.model.UserInfo;
import com.tencent.bugly.crashreport.CrashReport;

import java.lang.ref.WeakReference;
import java.util.Stack;

/**
 * 类名称：StmApplication
 * 类描述：自定义Application
 * 创建人：story
 * 创建时间：2021-08-07 09:49
 */
public class StmApplication extends Application {

    private static StmApplication instance;
    private Stack<WeakReference<Activity>> mActivityStack;
    private boolean isShowBackground = true; // 是否提示切换到后台，防止某些地方不需要提示
    private static Toast toastLong;
    private static Toast toastShort;
    private UserInfo userInfo;
    private AppExecutors appExecutors;

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

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
//        try {
//            //实例化ReaderHelper并setContext
//            ReaderHelper.setContext(getApplicationContext());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        appExecutors = new AppExecutors();

        // 开启bugly异常捕获
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(getApplicationContext());
        strategy.setAppChannel("HKYH");  //设置渠道
        CrashReport.initCrashReport(getApplicationContext(), "24e17bedee", true, strategy);
    }

    public static StmApplication getInstance() {
        return instance;
    }

    public void enterActivity(WeakReference<Activity> weakReference) {
        if (mActivityStack == null) {
            mActivityStack = new Stack<>();
        }
        mActivityStack.add(weakReference);
    }

    public void removeActivity(WeakReference<Activity> weakReference) {
        if (mActivityStack != null) {
            mActivityStack.remove(weakReference);
        }
    }

    public Activity getCurrentAct() {
        Activity activity = null;
        if (mActivityStack != null) {
            activity = mActivityStack.lastElement().get();
        }
        return activity;
    }

    public boolean isShowBackground() {
        return isShowBackground;
    }

    public void setShowBackground(boolean showBackground) {
        isShowBackground = showBackground;
    }

    public void showToastLong(String msg) {
        if (toastLong == null) {
            toastLong = Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG);
        } else {
            toastLong.setText(msg);
        }
        toastLong.show();
    }

    public void showToastShort(String msg) {
        if (toastShort == null) {
            toastShort = Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT);
        } else {
            toastShort.setText(msg);
        }
        toastShort.show();
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public AppExecutors getAppExecutors() {
        return appExecutors;
    }
}
