package com.newapp.paperwall;

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

import com.newapp.paperwall.tools.UiUtils;
import com.newapp.paperwall.tools.DbManager;
import com.newapp.paperwall.tools.JsonData;

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

public class BaseApp extends Application {
    public static Context mAppContext;

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


    private String animaLs = "Animals.json";
    private String experiment = "Experimental.json";
    private String film = "Film.json";
    private String nature = "Nature.json";
    private String patterns = "Patterns.json";
    private String street = "Street.json";
    private String travel = "Travel.json";
    private String wallpaper = "Featured.json";
    private ExecutorService executor;
    private static List<String> CategoryNames = new ArrayList<>();
    private int i = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = this;
        regularFont = Typeface.createFromAsset(getAssets(), "custfont.ttf");
        Log.d("YYYYYYY","init before");
        DbManager.init(this);
        executor = Executors.newFixedThreadPool(8);
        String[] names = {wallpaper, animaLs, experiment, film, nature, patterns, street, travel};

        try {
            for (String name : names) {
                InputStream open = getAssets().open(name);
                String covertStr = UiUtils.getCovertStr(open);
                Log.d("YYYYYY",covertStr);
                if (!covertStr.isEmpty()) {
                    String realName = name.substring(0, name.indexOf("."));
                    CategoryNames.add(realName);
                    executor.execute(new Runnable() {
                        @Override
                        public void run() {
                            i++;
                            Log.d(TAG, "------------111");
                            JsonData.INSTANCE.getData(covertStr, realName);
//                            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();
//
//    }
}
