package com.exquisite;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;


import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.anythink.interstitial.api.ATInterstitial;
import com.exquisite.data.Mydata;
import com.exquisite.data.MydataBase;
import com.exquisite.tools.Mytools;

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

public class KeyboardSkin extends Application {
    public static KeyboardSkin app;
    private static Context context;
    public static List<Mydata> myDataArrayList;
    private static final String TOP_ID = "h66a0b86a344ef";
    private static final String TOP_KEY = "a95ed90a717c7bcff42cd908db3f05664";
    private static final String TOP_DEBUG = "8a341b6aa5cc3fa453d7741f01a534c1d5dd766a";
    public static List<ATInterstitial> ads;
    public static final String VERSION_NAME = "1.0.0";
    public static final String DB_Name = "keyboard_database";
    public static final String Table_Name = "keyboard_table";
    public static final int DB_Version = 1;
    private static boolean insert;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    public static final String PRE_NAME = "Mypre";
    public static final String PRE_KEY = "isinsert";
    public static final String PRE_KEY_INIT = "isinit";
    public static final String TAG = "zzj";
    public static List<Mydata> alllist = new ArrayList<>();
    public static List<Mydata> lovelist = new ArrayList<>();
    public static List<Mydata> coollist = new ArrayList<>();
    public static List<Mydata> cutelist = new ArrayList<>();
    public static List<Mydata> neonlist = new ArrayList<>();
    public static List<Mydata> festivallist = new ArrayList<>();
    public static List<Mydata> gravitylist = new ArrayList<>();
    public static List<Mydata> aestheticlist = new ArrayList<>();
    public static List<Mydata> superlist = new ArrayList<>();

    public static List<Mydata> getLovelist() {
        return lovelist;
    }

    public static List<Mydata> getCoollist() {
        return coollist;
    }

    public static List<Mydata> getCutelist() {
        return cutelist;
    }

    public static List<Mydata> getNeonlist() {
        return neonlist;
    }

    public static List<Mydata> getFestivallist() {
        return festivallist;
    }

    public static List<Mydata> getGravitylist() {
        return gravitylist;
    }

    public static List<Mydata> getAestheticlist() {
        return aestheticlist;
    }

    public static List<Mydata> getSuperlist() {
        return superlist;
    }

    public static int a;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        app = this;
        a = 0;
//        Log.e("zzj","---a app is---"+a);
        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

            @Override
            public void onResultCallback(boolean isEU) {
                Log.e(TAG, "onResultCallback:" + isEU);
                if (isEU && ATSDK.getGDPRDataLevel(context) == ATSDK.UNKNOWN) {
                    ATSDK.showGdprAuth(context);
                }

            }

            @Override
            public void onErrorCallback(String errorMsg) {
                Log.e(TAG, "onErrorCallback:" + errorMsg);
            }
        });

        initSdk();
        myDataArrayList = Mytools.parseJsonToList("keyboard.json");
        sp = getSharedPreferences(PRE_NAME, MODE_PRIVATE);
        insert = sp.getBoolean(PRE_KEY, false);
        if (!insert) {

            Mytools.runIO(new Runnable() {
                @Override
                public void run() {
                    insert = true;
                    MydataBase.getInstance().myDAO().insertAll(myDataArrayList);
                    editor = sp.edit();
                    editor.putBoolean(PRE_KEY, insert);
                    editor.apply();
                }
            });
        }

        initData();


    }

    private void initData() {
        Mytools.runIO(new Runnable() {
            @Override
            public void run() {
                alllist = MydataBase.getInstance().myDAO().getAlldata();
                lovelist = MydataBase.getInstance().myDAO().getotherlist("love");
                coollist = MydataBase.getInstance().myDAO().getotherlist("cool");
                cutelist = MydataBase.getInstance().myDAO().getotherlist("cute");
                neonlist = MydataBase.getInstance().myDAO().getotherlist("neon");
                festivallist = MydataBase.getInstance().myDAO().getotherlist("festival");
                gravitylist = MydataBase.getInstance().myDAO().getotherlist("gravity");
                aestheticlist = MydataBase.getInstance().myDAO().getotherlist("aesthetic");
                superlist = MydataBase.getInstance().myDAO().getotherlist("super theme");

            }
        });
    }

    private void initSdk() {
//        ATSDK.integrationChecking(context);

        ATSDK.init(context, TOP_ID, TOP_KEY);

        ads = Mytools.getAllAd();
//        ATSDK.setNetworkLogDebug(true);
//        ATDebuggerUITest.showDebuggerUI(context,TOP_DEBUG);

    }

    public static List<ATInterstitial> lodAd() {

        return ads;
    }


    public static Context getAppContext() {
        return context;
    }

    public static List<Mydata> getMyDataArrayList() {
        if (myDataArrayList != null) {
            Collections.shuffle(myDataArrayList);
            return myDataArrayList;
        } else {
            return null;
        }

    }


}
