package com.dskj.trustlend;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.dskj.trustlend.bean.UserBean;
import com.dskj.trustlend.config.Config;
import com.dskj.trustlend.utils.DeviceUtil;
import com.dskj.trustlend.utils.GsonUtils;
import com.dskj.trustlend.utils.LogUtils;
import com.tencent.bugly.crashreport.CrashReport;


/**
 * Application 主入口
 *
 * @author xuhuixiang
 */

public class InitApp extends Application {
    public static Context AppContext;
    public static InitApp initApp;

    public static Context getAppContext() {
        return AppContext;
    }


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

    }



    @Override
    public void onCreate() {
        super.onCreate();
        AppContext = getApplicationContext();
        initApp = this;
        DeviceUtil.init(this);
        initARouter();
        CrashReport.initCrashReport(getApplicationContext(), "67a2a1ead7", false);


    }


    private void initARouter() {

    }

    //----------------------------SharedPreferences的全局存储实现 人懒 懒得抽文件---------------------------------------

    public static void saveInt(String key, int value) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static void saveFloat(String key, float value) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    public static void saveLong(String key, long value) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static void saveString(String key, String value) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static void deleteKey(String key) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        if (!sp.contains(key)) return;
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.apply();
    }

    public static void saveBoolean(String key, boolean value) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static int getInt(String key, int defValue) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    public static float getFloat(String key, float defValue) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        return sp.getFloat(key, defValue);
    }

    public static long getLong(String key, long defValue) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        return sp.getLong(key, defValue);
    }

    public static boolean getBoolean(String key, boolean defValue) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        return sp.getBoolean(key, defValue);
    }

    public static String getString(String key, String defValue) {
        Context context = AppContext;
        if (context == null) {
            return defValue;
        }
        SharedPreferences sp = context.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }

    public static UserBean getUserBean() {
        Context context = AppContext;
        if (context == null) {
            return null;
        }
        String name = getString("user_bean","");
        if(TextUtils.isEmpty(name)){
            return null;
        }
        return GsonUtils.getObjFromJSON(name,UserBean.class);
    }


    public static String getToken() {
        Context context = AppContext;
        if (context == null) {
            return "";
        }
        return getString(Config.API_TOKEN_TAG,"");
    }

    public static void setToken(String value) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(Config.API_TOKEN_TAG, value);
        editor.apply();
    }

    public static String getUserToken() {
        Context context = AppContext;
        if (context == null) {
            LogUtils.i("进这里干嘛？");
            return "";
        }
        return getString(Config.API_TOKEN_TAG1,"");
    }

    public static void setUserToken(String value) {
        SharedPreferences sp = AppContext.getSharedPreferences("InitApp", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(Config.API_TOKEN_TAG1, value);
        editor.apply();
    }
}
