package com.wall.snap.wallpaper;

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.snap.wallpaper.mysnapbean.MysnapData;
import com.wall.snap.wallpaper.toolsnap.ComUtilssnap;
import com.wall.snap.wallpaper.toolsnap.JsonDatasnap;
import com.wall.snap.wallpaper.toolsnap.ObectBoxManagersnap;

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

    public static String TAG = "-------MysnapApp---------";
    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");
        // 上传
        UpLoadManager.INSTANCE.init(this, "update_load", (s, s2) -> null);
        ObectBoxManagersnap.init(this);
        executor = Executors.newFixedThreadPool(8);
        String[] names = {wallpaperName, animasName, exName, filmName, natureName, pattName, streetName, travelName};
        TradPlusSdk.initSdk(this, "0BD6FE6AA41F6DAC752AAFCBBBEDD911");
        try {
            for (String name : names) {
                InputStream open = getAssets().open(name);
                String covertStr = ComUtilssnap.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(MysnapApp.TAG, "------------111");
                            List<MysnapData> data = JsonDatasnap.INSTANCE.getData(covertStr, realName);
                            Collections.shuffle(data);
                            for (MysnapData data1:data){
                                ObectBoxManagersnap.addData(data1);
                            }
                        }
                    });
                }
            }


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


    }

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