package com.wall.photography.picture;

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

import com.wall.photography.picture.Data.Data;
import com.wall.photography.picture.tool.CommonUtils;
import com.wall.photography.picture.tool.JsonParser;
import com.wall.photography.picture.tool.ObjectBoxManager;

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 = "Animalssearch.json";
    private String exName = "Experimentalsearch.json";
    private String filmName = "Filmsearch.json";
    private String natureName = "Naturesearch.json";
    private String pattName = "Patternssearch.json";
    private String streetName = "Streetsearch.json";
    private String travelName = "Travelsearch.json";
    private String wallpaperName = "Featuredsearch.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");

        ObjectBoxManager.init(this);
        executor = Executors.newFixedThreadPool(8);
        String[] names = {wallpaperName, animasName, filmName};

        try {
            for (String name : names) {
                InputStream open = getAssets().open(name);
                String covertStr = CommonUtils.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, "開始載入分類: " + realName);
                            List<Data> data = JsonParser.INSTANCE.getData(covertStr, realName);
                            Log.d(MyApp.TAG, "分類 " + realName + " 解析出 " + data.size() + " 條數據");
                            Collections.shuffle(data);
                            int successCount = 0;
                            for (Data data1:data){
                                ObjectBoxManager.addData(data1);
                                successCount++;
                            }
                            Log.d(MyApp.TAG, "分類 " + realName + " 成功添加 " + successCount + " 條數據到數據庫");

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