package com.key.simplekeyboard;

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

import com.key.simplekeyboard.data.Mydata;
import com.key.simplekeyboard.data.MydataBase;
import com.key.simplekeyboard.data.Otherdata;
import com.key.simplekeyboard.tools.Mytools;

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


public class SimpleKeyboardSkins extends Application {
    public static SimpleKeyboardSkins app;
    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 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 isinsert;
    private static final String PREFS_NAME = "MyPrefs";
    private static final String KEY_IS_INSERTED = "isinserted";

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        context = getApplicationContext();
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        isinsert = prefs.getBoolean(KEY_IS_INSERTED, false);
        List<Mydata> myData = Mytools.parseJsonToList("keyboard.json");
        mydataList = myData;
        if (!isinsert) {
            Mytools.runIO(new Runnable() {
                @Override
                public void run() {
                    MydataBase.getInstance().myDAO().insertAll(getOtherdata());
                    isinsert = true;
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean(KEY_IS_INSERTED, isinsert);
                    editor.apply();
                }
            });
        }

        initData();
    }


    public static Context getContext() {
        return context;
    }

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

    public static List<Otherdata> getOtherdata() {
        List<Mydata> mydata = SimpleKeyboardSkins.getMyDataArrayList();
        List<Otherdata> newlist = new ArrayList<>();
        Set<String> titlesSet = new HashSet<>();
        for (Mydata myDataItem : mydata) {
            List<Otherdata> dataList = myDataItem.getList();
            for (Otherdata otherdata : dataList) {
                String title = otherdata.getTitle();
                if (!titlesSet.contains(title)) {
                    newlist.add(otherdata);
                    titlesSet.add(title);
                }
            }
        }

        return newlist;
    }

    private void initData() {
        Mytools.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<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;
    }
}
