package com.prank.sounds.fart;

import android.app.Application;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.util.Log;

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.prank.sounds.fart.bean.Info;

import com.prank.sounds.fart.bean.MyBean;
import com.prank.sounds.fart.tools.Db;
import com.prank.sounds.fart.tools.MyUtils;
import com.prank.sounds.fart.topon.AdManager;

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

public class MyApplication extends Application {

    public static MyApplication application;
    public static Typeface defaultFont;
    public static List<MyBean> bean;

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

    private static final String AppKey = "a460517c4130df84822198f5e8c922a44";
    private static final String APPId = "h684261e85fc58";
    private static final String debug_KEy = "ed16491faf1c69a7f43813bec58f9fb7bfacbb2b";

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

        initSdk();
    }

    public static Context getContext() {
        return application.getApplicationContext();
    }

    private void initSdk() {

        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

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

            }

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

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

    }
}
