package com.prank.funny.pranks;

import android.app.Application;
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.funny.pranks.bean.Info;

import com.prank.funny.pranks.bean.MyBean;
import com.prank.funny.pranks.tools.DataBase;
import com.prank.funny.pranks.tools.Common;

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

public class FunnyApp extends Application {

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

    public static boolean initSDkOK = false;
    public static String initAction = "INIT_SDK";
    private String SDK_KEY = "A8q_8gENDJGKqq6bzfJLd8yNn0brHCopf1iv1xT0Tudcg5MafTgslq2IByc8cPySOfOieiKTJqCr9cd8Vw4bCz";

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


    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(FunnyApp.this).sendBroadcast(new Intent(initAction));
            }
        });
    }
}
