package com.fun.airhornprank;

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


import com.fun.airhornprank.data.MyDataBase;
import com.fun.airhornprank.data.Mydata;
import com.fun.airhornprank.tool.Mytools;

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


public class PrankSkin extends Application {
    public static PrankSkin app;
    public static final String DB_Name = "prank_database";
    public static final String Table_Name = "prank_table";
    public static final int DB_Version = 1;
    private List<Mydata> allData = new ArrayList<>();
    private SharedPreferences sp;
    private static boolean isinsert;
    private static boolean iscreate;
    private SharedPreferences.Editor editor;
    private static final String PREFS_NAME = "MyPrefs";
    private static final String KEY_IS_INSERTED = "isinserted";
    private static final String KEY_IS_CREATE = "iscreate";
    private static List<Mydata> mydataList = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;

        initData();
        initCreate();
    }

    private void initCreate() {
        Mytools.runIO(new Runnable() {
            @Override
            public void run() {
                List<Mydata> allData = MyDataBase.getInstance().likeDAO().getAll();
                mydataList.clear();
                int[] indices = {0, 456, 111, 222, 333, 444, 555, 345, 234};

                for (int index : indices) {
                    if (index < allData.size()) {
                        Mydata data = allData.get(index);
                        data.setRank(true);
                        MyDataBase.getInstance().likeDAO().update(data);
                        mydataList.add(data);
                    }
                }

            }
        });
    }

    private void initData() {
        allData = Mytools.getList("prank.json");
        sp = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        isinsert = sp.getBoolean(KEY_IS_INSERTED, false);
        iscreate = sp.getBoolean(KEY_IS_CREATE, false);
        if (!isinsert) {
            Mytools.runIO(new Runnable() {
                @Override
                public void run() {
                    MyDataBase.getInstance().likeDAO().insertAll(allData);
                    editor = sp.edit();
                    editor.putBoolean(KEY_IS_INSERTED, true);
                    editor.commit();
                }
            });
        }

    }

}
