package com.prank.tool;

import android.app.Application;
import android.content.res.AssetManager;
import android.graphics.Typeface;

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.prank.tool.jbean.Info;

import com.prank.tool.jbean.MyBean;
import com.prank.tool.top.AdManager;
import com.prank.tool.utils.DbManager;
import com.prank.tool.utils.Tools;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyApp extends Application {

    public static MyApp app;
    public static Typeface defaultFont;
    public static List<MyBean> bean;
    public static String TAG = "=======";

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        AssetManager assets = getAssets();
        defaultFont = Typeface.createFromAsset(assets, "font.ttf");
        DbManager.init(this);
        initializationSDK();
        if (bean != null)
            return;
        try {
            InputStream open = getAssets().open("prank.json");
            String string = Tools.getString(open);
            if (!string.isEmpty()) {
                bean = Tools.getBean(string);
                for (MyBean category : bean) {
                    for (Info info1 : category.getInfoList()) {
                        info1.setLike(false);
                        DbManager.getBoxBean().put(info1);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void initializationSDK() {
        String APPId = "h67d938c1ea945";
        String AppKey = "ad2965c924e1ef149e9126f9779bf8a13";
        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

            @Override
            public void onResultCallback(boolean isEU) {
                if (isEU && ATSDK.getGDPRDataLevel(MyApp.this) == ATSDK.UNKNOWN) {
                    ATSDK.showGdprAuth(MyApp.this);
                }

            }

            @Override
            public void onErrorCallback(String errorMsg) {

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