package com.kfzs.duanduan;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.Environment;
import android.support.multidex.MultiDexApplication;
import android.util.DisplayMetrics;
import android.util.Log;

import com.arialyy.aria.core.Aria;
import com.kfzs.appstore.utils.restful.KFZSNetwork;
import com.kfzs.duanduan.db.DataSave;
import com.kfzs.duanduan.utils.ApkUtils;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.service.DownloadService;
import com.sheep.jiuyan.samllsheep.utils.G;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 17/2/10.
 */
public class KFZSApp extends MultiDexApplication {

    public static final boolean DEBUG = BuildConfig.DEBUG;

    public static final String SD_PATH_EXTERNAL = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    private static KFZSApp application;
    public static Context context;
    public static String TAG;
    public static String ownPath;
    public static String path_catch;
    public static String path_img;
    public static String path_img_catch;
    public static String path_download;
    public static String path_Log;
    public static ActMain actMain;
    public String GameCode = null;


    public static final int MAX_DOWNLOAD_TASK_SIZE = 5;
    public HashMap<String, String> mRealDownloadUrl;

    @Override
    public void onCreate() {
        super.onCreate();
        TAG = this.getPackageName();
        mRealDownloadUrl = new HashMap<>();
        setOnlyContext(this);
        initUtils(DEBUG);
        initOwnCatchPath();
        startService(new Intent(this, DownloadService.class));
    }

    private void initUtils(boolean debug) {
        KFZSNetwork.init(application);
        DisplayMetrics metric = getResources().getDisplayMetrics();
        G.DENSITY = metric.density;
        G.HEIGHT = metric.heightPixels;
        G.WIDTH = metric.widthPixels;

        DataSave dataSave = DataSave.getInstance();
        String umengKey = dataSave.getUmengKey();
        String channelName = dataSave.getChannelName();
//        Log.w("DDINIT", "API_VERSION: " + UrlBll.API_VERSION + " |CAPTCHA: " + UrlBll.CAPTCHA_DOMAIN);
//        Log.w("DDINIT", "GRAPH" + BuildConfig.DUANDUAN_GRAPH + " |GRAPH" + BuildConfig.DUANDUAN_GRAPH);
//        Log.w("DDINIT", "umengKey: " + umengKey + " |channelName: " + channelName + " |debug: " + debug);
//        Log.w("DDINIT", "wxAppId: " + BuildConfig.WX_APPID + " |wxAppSecret: " + BuildConfig.WX_APPSECRET + " |debug: " + debug);
//        Log.w("DDINIT", "wbAppId: " + BuildConfig.WB_APPID + " |wbAppSecret: " + BuildConfig.WB_APPSECRET + " |debug: " + debug);
        //先注释掉，报错了
//        MobclickAgent.UMAnalyticsConfig umAnalyticsConfig = new MobclickAgent.UMAnalyticsConfig(application, umengKey, channelName);
//        MobclickAgent.startWithConfigure(umAnalyticsConfig);
//        MobclickAgent.setDebugMode(debug);
        //Log.e("JPushRegzid", JPushInterface.getRegistrationID(this));
//        MobclickAgent.setCatchUncaughtExceptions(!debug);
//        MobclickAgent.enableEncrypt(true);

//        //um Share
//        Config.DEBUG = true;
//        QueuedWork.isUseThreadPool = false;
//        UMShareAPI.get(this);
//        //end
//        initShareSDK();
//    }
//
//    //各个平台的配置，建议放在全局Application或者程序入口
//    private void initShareSDK() {
//        PlatformConfig.setWeixin(BuildConfig.WX_APPID, BuildConfig.WX_APPSECRET);
//        PlatformConfig.setSinaWeibo(BuildConfig.WB_APPID, BuildConfig.WB_APPSECRET, "http://sns.whalecloud.com");
//        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
    }

    /**
     * get MDLApplication instance
     *
     * @return {@link KFZSApp}
     */
    public static KFZSApp getInstance() {
        return application;
    }


    /**
     * quit app safe
     */
    public void quit() {
        try {
            Aria.download(this).stopAllTask();
            stopService(new Intent(this, DownloadService.class));
            ActivityManager.getInstance().finishAllActivity();
        } catch (Exception e) {
            Log.e(TAG, "quit error" + e.getMessage() + "\n" + e.getCause());
        } finally {
            Log.v(TAG, "App quite");
        }

    }

    private static void setContext(Context context) {
        KFZSApp.context = context;
    }

    private void setOnlyContext(Context context) {
        setContext(context);
    }

    private void initOwnCatchPath() {
        initOwnPath();
        File appPath = new File(path_catch);
        if (!appPath.exists()) {
            appPath.mkdirs();
        }
    }

    private void initOwnPath() {
        StringBuilder sb = new StringBuilder();
        sb.append(SD_PATH_EXTERNAL);
        sb.append("/Android/data/");
        sb.append(TAG);
        ownPath = sb.toString();
        sb.append("/catch");
        path_catch = sb.toString();
        sb.setLength(0);
        sb.append(ownPath);
        sb.append("/img");
        path_img = sb.toString();
        sb.setLength(0);
        sb.append(ownPath);
        sb.append("/img/catch");
        path_img_catch = sb.toString();
        sb.setLength(0);
        sb.append(ownPath);
        sb.append("/download");
        path_download = sb.toString();
        sb.setLength(0);
        sb.append(ownPath);
        sb.append("/log");
        path_Log = sb.toString();
    }

    public KFZSApp() {
        super();
        application = this;
    }

    public Map<String, PackageInfo> loadPackagesInfoRealTime() {
        return ApkUtils.getInstalledApks(this);
    }


    @Override
    public void onTerminate() {
        super.onTerminate();
        Log.d(TAG, "onTerminate");
    }

}
