package com.casic.dcms.base;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.casic.dcms.R;
import com.casic.dcms.greendao.DaoMaster;
import com.casic.dcms.greendao.DaoSession;
import com.casic.dcms.utils.FileUtils;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.igexin.sdk.IUserLoggerInterface;
import com.igexin.sdk.PushManager;
import com.pengxh.androidx.lite.utils.SaveKeyValues;
import com.qmuiteam.qmui.arch.QMUISwipeBackActivityManager;

public class BaseApplication extends Application {

    private static final String TAG = "BaseApplication";
    private static DaoSession daoSession;
    private volatile static BaseApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        QMUISwipeBackActivityManager.init(this);
        SaveKeyValues.initSharedPreferences(this);
        FileUtils.init(this);
        ArcGISRuntimeEnvironment.setLicense(getString(R.string.arcgis_license));//去掉开发版水印
        ArcGISRuntimeEnvironment.setApiKey(getString(R.string.arcgis_key));
        //个推初始化
        PushManager.getInstance().initialize(this);
        PushManager.getInstance().setDebugLogger(this, new IUserLoggerInterface() {
            @Override
            public void log(String s) {
                Log.d(TAG, s);
            }
        });
        initDataBase();
    }

    /**
     * 双重锁单例
     */
    public static BaseApplication get() {
        if (instance == null) {
            synchronized (BaseApplication.class) {
                if (instance == null) {
                    instance = new BaseApplication();
                }
            }
        }
        return instance;
    }

    private void initDataBase() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "CityAdminister.db");
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoSession() {
        return daoSession;
    }
}
