package com.tjzhxx.union.system;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import androidx.multidex.MultiDexApplication;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshFooterCreator;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator;
import com.tjzhxx.union.R;
import com.tjzhxx.union.public_store.AppDataFile;
import com.tjzhxx.union.public_store.CrashHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ynyxmac on 16/2/4.
 */
public class MyApplication extends MultiDexApplication {

    protected static final String TAG = "MyApplication";

    private static MyApplication mInstance;

    /**
     * 获取全局Application
     *
     * @return
     */
    public static MyApplication getInstance() {
        return mInstance;
    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
                return new MaterialHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                ClassicsFooter.REFRESH_FOOTER_LOADING = context.getResources().getString(R.string.rc_loadmore_loading);
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });

    }
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

//        if(BuildConfig.DEBUG) Stetho.initializeWithDefaults(this);
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this.getApplicationContext());
//        SangforAuthForward.getInstance().initSangforHook();
        AppDataFile.init(AppDataFile.FILE_PICTURE);
    }

    private List<Activity> mActivityList = new ArrayList<>();

    public void addActivity(Activity activity) {
        mActivityList.add(activity);
    }

    public void removeActivity(Activity activity) {
        mActivityList.remove(activity);
    }
    public void removeAllActivity() {
        try {
            for (Activity activity : mActivityList) {
                if (activity != null)
                    activity.finish();
            }
            mActivityList.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exit() {
        try {
            for (Activity activity : mActivityList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    public static void openPlatform(Context context) {
        Intent intent = null;
        PackageManager pm = context.getPackageManager();
        try {
            String packagename = "com.yxst.epic.unifyplatform";
            PackageInfo pi = pm.getPackageInfo(packagename, 0);

            Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
            //resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            resolveIntent.setPackage(pi.packageName);

            List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);

            ResolveInfo ri = apps.iterator().next();
            if (ri != null) {
                String packageName = ri.activityInfo.packageName;
                String className = ri.activityInfo.name;

                intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);

                ComponentName cn = new ComponentName(packageName, className);

                intent.setComponent(cn);
            }
        } catch (PackageManager.NameNotFoundException e) {
        }

        if (intent != null) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//设置标志如果app已经打开后直接激活
            context.startActivity(intent);
        }
    }
}
