package com.wall.dream.wallpapers;

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

import com.tradplus.ads.open.TradPlusSdk;
import com.up.uploadlibrary.UpLoadManager;
import com.wall.dream.wallpapers.data.DataBase;
import com.wall.dream.wallpapers.GJ.ComUtilsGJ;
import com.wall.dream.wallpapers.GJ.JsonDataBase;
import com.wall.dream.wallpapers.GJ.ObectBoxManagerGJ;

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

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


    private String animasName = "AnimalsGood.json";
    private String exName = "ExperimentalGood.json";
    private String filmName = "FilmGood.json";
    private String natureName = "NatureGood.json";
    private String pattName = "PatternsGood.json";
    private String streetName = "StreetGood.json";
    private String travelName = "TravelGood.json";
    private String wallpaperName = "FeaturedGood.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(), "custfontGood.ttf");

        ObectBoxManagerGJ.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 = ComUtilsGJ.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(DreamWallpaper.TAG, "------------111");
                            List<DataBase> data = JsonDataBase.INSTANCE.getData(covertStr, realName);
                            Collections.shuffle(data);
                            for (DataBase data1:data){
                                ObectBoxManagerGJ.addData(data1);
                            }

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

                        }
                    });
                }
            }


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


        UpLoadManager.INSTANCE.init(this, "ocean", (s, s2) -> null);
        TradPlusSdk.initSdk(this, "561313B7BB2FBAABFFA4E860CDA15B11");
    }

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

}
