package com.wall.various.wallpaper;

import android.app.Application;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.anythink.debug.api.ATDebuggerUITest;
import com.wall.various.wallpaper.ad.AdManager;
import com.wall.various.wallpaper.myjson.Data;
import com.wall.various.wallpaper.helper.Common;
import com.wall.various.wallpaper.helper.JsonData;
import com.wall.various.wallpaper.helper.Dbhelper;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyApp extends Application {
    public static Context mAppContext;

    public static String TAG = "-------MyApp---------";
    public static Typeface defaultFont;


    private String animasName = "Animals.json";
    private String exName = "Experimental.json";
    private String filmName = "Film.json";
    private String natureName = "Nature.json";
    private String pattName = "Patterns.json";
    private String streetName = "Street.json";
    private String travelName = "Travel.json";
    private String wallpaperName = "Featured.json";
    private ExecutorService executor;
    private static List<String> CategoryNames = new ArrayList<>();
    private int i = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = this;
        defaultFont = Typeface.createFromAsset(getAssets(), "custfont.ttf");

        Dbhelper.init(this);
        executor = Executors.newFixedThreadPool(8);
        String[] names = {wallpaperName, animasName, exName, filmName, natureName, pattName, streetName, travelName};
        initTop();
        try {
            for (String name : names) {
                InputStream open = getAssets().open(name);
                String covertStr = Common.getCovertStr(open);
                if (!covertStr.isEmpty()) {
                    String realName = name.substring(0, name.indexOf("."));
                    CategoryNames.add(realName);
                    executor.execute(new Runnable() {
                        @Override
                        public void run() {
                            i++;
                            Log.d(MyApp.TAG, "------------111");
                            List<Data> data = JsonData.INSTANCE.getData(covertStr, realName);
                            Collections.shuffle(data);
                            for (Data data1:data){
                                Dbhelper.addData(data1);
                            }
                        }
                    });
                }
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public static List<String> getCategoryNames() {
        return CategoryNames;
    }


    private void initTop(){
        String APPId = "h68085f63bd606";
        String AppKey = "a9ced41fc972a9dfe7dc59a21493c2228";
        String debug_KEy = "8a341b6aa5cc3fa453d7741f01a534c1d5dd766a";
        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

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

            }

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

        ATSDK.init( this, APPId, AppKey);

//        ATDebuggerUITest.showDebuggerUI(this,debug_KEy);
        AdManager.loadAllAd();

    }
}
