package com.fun.funnyprank;

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


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.fun.funnyprank.data.LikeData;
import com.fun.funnyprank.data.Mydata;
import com.fun.funnyprank.data.MydataBase;
import com.fun.funnyprank.tools.Mytools;

import java.util.ArrayList;
import java.util.List;

public class FunnyPrankSkin extends Application {
    public static final String DB_Name = "like_database";
    public static final String Table_Name = "like_table";
    public static final int DB_Version = 1;
    public static FunnyPrankSkin app;
    private static Context context;
    public static List<Mydata> myDataArrayList;
    private static boolean isinsert;
    private static final String PREFS_NAME = "MyPrefs";
    private static final String KEY_IS_INSERTED = "isinserted";
    public static final String AD_BROADCAST = "on_success_action";
    private static final String MAX_SDK = "6GTFERcq2m3KHo37uityeCg0kfcqx3iT31OJG_a5weBmXsslFg5NuR8mlBXEqZ3ExDdXIl16PTMg0442tdRaJs";
    public static Boolean initSDK = false;
    private static List<LikeData> mydataList = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        initSDk();


        context = getApplicationContext();
        List<Mydata> myData = Mytools.parseJsonToList("prank.json");
        myDataArrayList = myData;
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        isinsert = prefs.getBoolean(KEY_IS_INSERTED, false);
        if (!isinsert) {
            Mytools.runIO(new Runnable() {
                @Override
                public void run() {
                    MydataBase.getInstance().likeDao().insertAll(getOtherdata());
                    isinsert = true;
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean(KEY_IS_INSERTED, isinsert);
                    editor.apply();
                }
            });
        }


        initCreate();
    }

    private void initCreate() {
        Mytools.runIO(new Runnable() {
            @Override
            public void run() {
                List<LikeData> allData = MydataBase.getInstance().likeDao().getAll();
                mydataList.clear();
                int[] indices = {0, 456, 111, 333, 222, 555, 444, 345, 234};

                for (int index : indices) {
                    if (index < allData.size()) {
                        LikeData data = allData.get(index);
                        data.setIsrank(true);
                        MydataBase.getInstance().likeDao().update(data);
                        mydataList.add(data);
                    }
                }

            }
        });
    }

    private void initSDk() {

        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(MAX_SDK, this)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        AppLovinSdk.getInstance(this).initialize(initConfig, new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration sdkConfig) {
                initSDK = true;
                Log.e("zzj", "------------initSDk--------success");
                LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(AD_BROADCAST));
            }
        });
        AppLovinSdk.getInstance(this).getSettings().setVerboseLogging(true);
    }

    public static Context getAppContext() {
        return context;
    }

    public static List<Mydata> getMyDataArrayList() {

        return myDataArrayList;
    }

    public static List<LikeData> getOtherdata() {
        List<Mydata> mydata = FunnyPrankSkin.getMyDataArrayList();
        List<LikeData> newlist = new ArrayList<>();
        for (Mydata myDataItem : mydata) {
            List<LikeData> dataList = myDataItem.getList();
            for (LikeData otherdata : dataList) {
                newlist.add(otherdata);
            }
        }

        return newlist;
    }

}
