package com.apps.chillscreen;

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

import com.ad.toponlibrary.AdManager;
import com.apps.chillscreen.tool.PictureManager;
import com.apps.chillscreen.tool.JsonConvert;
import com.apps.chillscreen.tool.OpenUtil;
import com.up.uploadlibrary.UpLoadManager;

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 BaseApplication extends Application {
    private Context context;
    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 static List<String> categoryList = new ArrayList<>();
    private ExecutorService executorService;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        PictureManager.init(this);
        executorService = Executors.newFixedThreadPool(8);//开启8个线程的线程池
        String[] names = {animaLs, experiment, film, nature, patterns, street, travel, wallpaper};
        AdManager.INSTANCE.init("TAG",this,"h687de4865cef8","a3e080462348b3222e1bc9afe4fe6c50a",
                "d0a434af69020f58cd1c246da4adb0630eb18a02",
                "n687de501cb85f",
                "n687de4f81a79c",
                "n687de4ee06429",false);
        UpLoadManager.INSTANCE.init(this,"TAG");
        try {
            for (String name : names) {
                InputStream inputStream = getAssets().open(name);
                String convert = OpenUtil.getConvert(inputStream);
                Log.d("YYYYYY", convert);
                if (!convert.isEmpty()) {
                    String substring = name.substring(0, name.indexOf("."));
                    categoryList.add(substring);
                    executorService.execute(() -> {
                        JsonConvert.INSTANCE.getData(convert, substring);
                    });
                }

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            executorService.shutdown(); // 平滑关闭（等待任务完成）
        }
    }

    public static List<String> getCategoryList(){
        return categoryList;
    }
}
