package com.example.wallpapermagazine;

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

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.example.wallpapermagazine.json.Info;
import com.example.wallpapermagazine.json.Info_data;
import com.example.wallpapermagazine.topon.AdManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class WallPaperSelect extends Application {
    private static final List<Info> userList = new ArrayList<>();
    private static List<Info_data> cataname = new ArrayList<>();
    public static Context context;
    public static final String aaa = "app_database";
    public static final int vvvaa = 1;
    public static String TAG = "=============";

    public static boolean initSDkOK = false;
    public static String initAction = "ACTION_INIT";

    private static final String APPId = "h66f13d8c6deec";
    private static final String AppKey = "aecf9a5d538826e16c0030b758024b1cf";

    @Override
    public void onCreate() {
        super.onCreate();
        context = getBaseContext();
        initTopOnSDk();
        try {
            InputStream open = getAssets().open("wallpaper.json");
            String covertStr = ReadFile.getCovertStr(open);
            if (!covertStr.isEmpty()) {
                Gson gson = new Gson();
                Type type = new TypeToken<List<Info_data>>() {
                }.getType();
                List<Info_data> names = gson.fromJson(covertStr, type);
                for (Info_data Name : names) {
                    cataname.add(Name);
                    for (Info info : Name.getDatas()) {
                        userList.add(info);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<Info_data> getCataName() {

        return cataname;
    }

    public static List<Info> getUserList() {
        Collections.shuffle(userList);
        return userList;
    }

    private void initTopOnSDk() {

        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

            @Override
            public void onResultCallback(boolean isEU) {
                Log.e(TAG, "onResultCallback:" + isEU);
                if (isEU && ATSDK.getGDPRDataLevel(WallPaperSelect.this) == ATSDK.UNKNOWN) {
                    ATSDK.showGdprAuth(WallPaperSelect.context);
                }

            }

            @Override
            public void onErrorCallback(String errorMsg) {
                Log.e(TAG, "onErrorCallback:" + errorMsg);
            }
        });

        ATSDK.init( this, APPId, AppKey);
        AdManager.loadAllAd();
        //测试工具
//        ATDebuggerUITest.showDebuggerUI(this,debug_KEy);
    }
}
