package com.prank.funnypranksounds;

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

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.google.firebase.FirebaseApp;
import com.prank.funnypranksounds.data.local.entity.AudioData;
import com.prank.funnypranksounds.data.repository.AudioRepository;
import com.prank.funnypranksounds.topon.AdManager;
import com.prank.funnypranksounds.utils.JsonParser;

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

public class MyApplication extends Application {

    public static MyApplication myApplication;
    public static final String Database_Name = "Database";
    public static final int Database_Version = 1;
    private static final String PREF_NAME = "preferences";
    private static final String KEY_INITIALIZED = "initDatabase";

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

    private static final String AppKey = "a3724ef4af9a6a8b9f8826716e6f67f9b";
    private static final String APPId = "h677366609b2b9";
    private static final String debug_KEy = "5c12401a5c59afe9a3ac946dd22ae5f1fecd8c1c";

    @Override
    public void onCreate() {
        super.onCreate();

        myApplication = this;

        //确保Firebase初始化
//        FirebaseApp.initializeApp(this);
//
//        // 输出日志以确认Firebase初始化
//        if (!FirebaseApp.getApps(this).isEmpty()) {
//            Log.d(TAG, "Firebase 初始化成功1");
//        } else {
//            Log.e(TAG, "Firebase 初始化失败0");
//        }

        initSdk();

        SharedPreferences preferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
        boolean init = preferences.getBoolean(KEY_INITIALIZED, false);

        if (!init) {
            initDatabase();
            preferences.edit().putBoolean(KEY_INITIALIZED, true).apply();
        }
    }

    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);

    }

    private void initDatabase() {
        AudioRepository imageRepository = new AudioRepository(getContext());
        List<AudioData> audioDataList = JsonParser.parseJson("prank.json");
        imageRepository.insertAll(audioDataList);
    }

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