package com.haven.wallpapers;

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

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.haven.wallpapers.Utils.ReadFile;
import com.haven.wallpapers.jsonbean.Data;
import com.haven.wallpapers.jsonbean.Info;
import com.haven.wallpapers.topon.AdManager;

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;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class MyApp extends Application {
    public static Context mAppContext;
    private static List<Info> userList = new ArrayList<Info>();

    public static final String dadgsfgsf = "app_database";
    public static final int aadasdasd = 1;

    private static MultiTransformation<Bitmap> multiTransformation;

    public static String TAG = "-------Wallpaper----------";

    private static int placeImg;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = this;
        initTop();
        placeImg = R.drawable.im_placeholder;
        multiTransformation = new MultiTransformation<>(
                new CenterCrop(),
                new RoundedCornersTransformation(ReadFile.dp2Px(12), 0)
        );
        String name[] = {"animals.json", "film.json", "nature.json", "textures-patterns.json"};
        ExecutorService cachedThreadPool = Executors.newFixedThreadPool(4);

        for (int i = 0; i < name.length; i++) {
            int task = i;
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        InputStream open = getAssets().open(name[task]);
                        String covertStr = ReadFile.getCovertStr(open);
                        if (!covertStr.isEmpty()) {
                            List<Data> list = ReadFile.getData( covertStr);
                            Info info = new Info();
                            int index = name[task].indexOf(".json");
                            String substring = name[task].substring(0, index);
                            info.setName(substring);
                            info.setList(list);
                            userList.add(info);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    if (task == 3) {
                        Log.d("------------", "-------------shutdownNow");
                        cachedThreadPool.shutdownNow();
                    }
                }
            });
        }

    }

    public static int getPlaceImg() {
        return placeImg;
    }

    public static MultiTransformation<Bitmap> getMultiTransformation() {
        return multiTransformation;
    }

    public static Context getmAppContext() {
        return mAppContext;
    }

    public static List<Info> getUserList() {
        Log.d("------------", "-------------userList=" + userList.size());
        return userList;
    }

    private void initTop() {
          String APPId = "h676bca506bb01";
          String AppKey = "aa9f5d83624d4f0812d4f34a60ed6919c";
          String debug_Key = "8a341b6aa5cc3fa453d7741f01a534c1d5dd766a";
        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();

    }
}
