package com.prank.happypranksounds;

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.happypranksounds.data.dao.HappyDataDao;
import com.prank.happypranksounds.data.entity.HappyData;
import com.prank.happypranksounds.data.database.AppDatabase;
import com.prank.happypranksounds.data.repository.HappyRepository;
import com.prank.happypranksounds.topon.AdManager;
import com.prank.happypranksounds.util.JsonParser;

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 = "a4a9832507013e232aa082b95e6eda7e6";
    private static final String APPId = "h67c12f6782254";

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

        myApplication = this;

        //确保Firebase初始化
//        FirebaseApp.initializeApp(this);
//
//        // 输出日志以确认Firebase初始化
//        if (!FirebaseApp.getApps(this).isEmpty()) {
//            Log.d("TAG", "Firebase1 初始化成功1");
//        } else {
//            Log.e("TAG", "Firebase0 初始化失败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() {
        HappyDataDao happyDataDao = AppDatabase.getInstance(getContext()).happyDataDao();
        HappyRepository happyRepository = new HappyRepository(happyDataDao);
        List<HappyData> happyDataList = JsonParser.parseJson("prank.json");
        happyRepository.insertAll(happyDataList);
    }

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