package com.key.coolkeyboard;

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.key.coolkeyboard.data.Mydata;
import com.key.coolkeyboard.data.MydataBase;
import com.key.coolkeyboard.data.Otherdata;
import com.key.coolkeyboard.tool.Mytool;
import com.key.coolkeyboard.topon.AdManager;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CoolKeyboardSkin extends Application {
    public static CoolKeyboardSkin app;
    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 Context context;
    public static List<Mydata> mydataList;
    private static boolean insert;
    private static boolean init;
    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<Otherdata> alllist = new ArrayList<>();
    public static List<Otherdata> lovelist = new ArrayList<>();
    public static List<Otherdata> coollist = new ArrayList<>();
    public static List<Otherdata> cutelist = new ArrayList<>();
    public static List<Otherdata> neonlist = new ArrayList<>();
    public static List<Otherdata> festivallist = new ArrayList<>();
    public static List<Otherdata> gravitylist = new ArrayList<>();
    public static List<Otherdata> aestheticlist = new ArrayList<>();
    public static List<Otherdata> superlist = new ArrayList<>();


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

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

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

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

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

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

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

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

    private static final String TOP_ID = "h66a23adb0da69";
    private static final String TOP_KEY = "ad31a3e8504709d2642bc5c311c254bcd";
    private static final String TOP_DEBUG = "8a341b6aa5cc3fa453d7741f01a534c1d5dd766a";
    public static List<ATInterstitial> ads;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        context = getApplicationContext();
        mydataList = Mytool.parseJsonToList("keyboard.json");


        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();


        sp = getSharedPreferences(PRE_NAME, MODE_PRIVATE);
        insert = sp.getBoolean(PRE_KEY, false);
        if (!insert) {

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


        initData();

        initKey();

    }

    private void initKey() {
        sp = getSharedPreferences(PRE_NAME, MODE_PRIVATE);
        init = sp.getBoolean(PRE_KEY, false);
        if (!init) {

            Mytool.runIO(new Runnable() {
                @Override
                public void run() {
                    init = true;
                    editor = sp.edit();
                    editor.putBoolean(PRE_KEY, init);
                    editor.apply();
                }
            });
        }
    }

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

        ATSDK.init(context, TOP_ID, TOP_KEY);
        AdManager.loadAllAd();

//        ATSDK.setNetworkLogDebug(true);


//        ATDebuggerUITest.showDebuggerUI(context,TOP_DEBUG);
    }

    public static List<ATInterstitial> lodAd() {

        return null;
    }


    private void initData() {
        Mytool.runIO(new Runnable() {
            @Override
            public void run() {
                alllist = MydataBase.getInstance().myDAO().getAll();
                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");

            }
        });

    }

    public static List<Otherdata> getAlllist() {

        return alllist;
    }

    public static List<Mydata> getMydataList() {
        return mydataList;
    }

    public static Context getContext() {
        return context;
    }


    public static List<Otherdata> getAlldataList() {
        List<Otherdata> otherdata = new ArrayList<>();
        List<Mydata> mydataList = getMydataList();
        Set<String> set = new HashSet<>();
        for (Mydata mydata : mydataList) {
            List<Otherdata> otherdataList = mydata.getList();
            for (Otherdata otherdata1 : otherdataList) {
                String title = otherdata1.getTitle();
                if (!set.contains(title)) {
                    otherdata.add(otherdata1);
                    set.add(title);
                }
            }
        }

        return otherdata;
    }

}
