package com.hw.nativeapp;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.os.StrictMode;

import com.blankj.utilcode.util.Utils;
import com.hw.nativeapp.config.Contents;
import com.hw.nativeapp.config.exceptions.CrashHandler;
import com.hw.nativeapp.httpnet.ApiService;
import com.hw.nativeapp.httpnet.ErrorConsumer;
import com.hw.nativeapp.httpnet.FileApiService;
import com.hw.nativeapp.httpnet.ResponseConsumer;
import com.hw.nativeapp.httpnet.RetrofitFileManager;
import com.hw.nativeapp.httpnet.RetrofitManager;
import com.hw.nativeapp.ui.dialogs.DialogCommon;
import com.hw.nativeapp.utils.ActivityUtils;
import com.hw.nativeapp.utils.LogUtils;
import com.hw.nativeapp.utils.RxUtil;
import com.hw.nativeapp.utils.ToastUtils;
import com.liulishuo.filedownloader.FileDownloader;


/**
 * Class Describe：APP 应用
 * Create Time：2016/10/13 11:30
 * Update Person：
 * Update Time：
 * Update Remark：
 */
public class MApplication extends Application {
    private static MApplication instance;
//    private static String defaultDeviceId;
    private static volatile ApiService mApiService;
    private static volatile FileApiService mFileApiService;

    /**
     * 线程池
     */

    public MApplication() {
        super();
        instance = this;

    }

    public static synchronized MApplication context() {
        return (MApplication) getAppContext().getApplicationContext();
    }


    public static MApplication getInstance() {
        if (instance == null)
            throw new IllegalStateException();
        return instance;
    }

    public static Context getAppContext() {
        if (instance == null)
            throw new IllegalStateException();
        return instance.getApplicationContext();
    }


    @Override
    public void onCreate() {
        super.onCreate();

//        Log.i(Constants.TAG, "MApplication Start");
        //讯飞语音初始化
//        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=d6905360");
        RetrofitManager.init(this);
        RetrofitManager.setBaseUrl(BuildConfig.API_HOST);
        RetrofitFileManager.init(this);
        LogUtils.init(this, true,false, 'd', "aleyds");

        Utils.init(this);
        FileDownloader.init(this);

//        startService();
//        startAlarm();

        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);


        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
    }


    /**
     * 获取api接口实例
     *
     * @return ApiService
     */
    public static ApiService getApiService() {
        if (mApiService == null) {
            synchronized (MApplication.class) {
                if (mApiService == null) {
                    mApiService = RetrofitManager.create(ApiService.class);
                }
            }
        }
        return mApiService;
    }


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



    /**
     * 在模拟环境中程序终止时会被调用
     */
    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    /**
     * 获取api接口实例
     *
     * @return ApiService
     */
    public static FileApiService getFileApiService() {
        if (mFileApiService == null) {
            synchronized (MApplication.class) {
                if (mFileApiService == null) {
                    mFileApiService = RetrofitFileManager.create(FileApiService.class);
                }
            }
        }
        return mFileApiService;
    }
}