package com.air.airhornprank;

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


import com.air.airhornprank.data.MyDataBase;
import com.air.airhornprank.data.Mydata;
import com.air.airhornprank.tool.Mytools;
import com.air.airhornprank.topon.AdManager;
import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;

import java.util.ArrayList;
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<>();


    private static final String AppKey = "a48c3836ca5119a551cf008e2c2a57e7a";
    private static final String APPId = "h671ef3aa249f4";

    public static String TAG = "-------AirHornPrank----------";

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

        initSDk();

        initData();
        initCreate();
    }


    private void initSDk() {

        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

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

            }

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

        ATSDK.init(this, APPId, AppKey);
        AdManager.loadAllAd();
        //测试工具
//        ATDebuggerUITest.showDebuggerUI(this,"a8dd4a3f1c38a5a3cadb4f39385fb682af63df07");

    }




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

    }

}
