package com.prank.sound.funnyapp;

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

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.prank.sound.funnyapp.bean.MyInfo;

import com.prank.sound.funnyapp.bean.MyBean;
import com.prank.sound.funnyapp.bean.DbManager;
import com.prank.sound.funnyapp.mytool.Common;

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 = "--------------";

    public static boolean initSDkOK = false;
    public static String initAction = "ACTION_INIT";
    private String SDK_KEY = "yhOU4TgSF10NVhdOBNZy6VbUxFc25c78nh_LA3905OB6rHVXJdsa0DVUhsIA6DWqJaHKDkimoBWXD5CktvrVXg";

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

        initMAxSDk();
    }

    private void initMAxSDk() {
        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(SDK_KEY, this)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        AppLovinSdk.getInstance(this).initialize(initConfig, new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(AppLovinSdkConfiguration appLovinSdkConfiguration) {
                initSDkOK = true;
                LocalBroadcastManager.getInstance(getContext()).sendBroadcast(new Intent(initAction));
            }
        });
        AppLovinSdk.getInstance(this).getSettings().setVerboseLogging(true);

        //    AppLovinSdk.getInstance( this ).showMediationDebugger();
    }

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