package com.house.wallpapers;

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

import com.ad.toponlibrary.AdManager;
import com.house.wallpapers.mybean.Data;
import com.house.wallpapers.mytool.Comutils;
import com.house.wallpapers.mytool.ObectManager;
import com.house.wallpapers.mytool.JsonData;
import com.up.uploadlibrary.UpLoadManager;

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 App extends Application {
    public static Context mAppContext;

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


    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();
        app = this;
        mAppContext = this;
        defaultFont = Typeface.createFromAsset(getAssets(), "custfont.ttf");
        UpLoadManager.INSTANCE.init(this,TAG,(k1,k2)-> null);
        AdManager.INSTANCE.init(TAG,app,"h69312a043bef5","a2f0c6303ec5c5423fa21b8dd9a7ea477",
                "25e138876a3743aa9037e82dff40dcb657df7a6d",
                "n69312a30b85ce",
                "n69312a314dafe",
                "n69312a319da17",false);
        ObectManager.init(this);
        executor = Executors.newFixedThreadPool(8);
        String[] names = {wallpaperName, animasName, exName, filmName, natureName, pattName, streetName, travelName};

        try {
            for (String name : names) {
                InputStream open = getAssets().open(name);
                String covertStr = Comutils.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(App.TAG, "------------111");
                            List<Data> data = JsonData.INSTANCE.getData(covertStr, realName);
                            Collections.shuffle(data);
                            for (Data data1:data){
                                ObectManager.addData(data1);
                            }

//                            if (i == 8) {
//                                executor.shutdown();
//                                Log.d(MyWallpaper.TAG, "------------data complete");
//                            }

                        }
                    });
                }
            }


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


    }

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


//    private void initTop(){
//        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();
//
//    }
}
