package com.wallpaper.wallpaperbox;

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.wallpaper.wallpaperbox.data.dao.WallpaperEntityDao;
import com.wallpaper.wallpaperbox.data.database.AppDatabase;
import com.wallpaper.wallpaperbox.data.entity.WallpaperEntity;
import com.wallpaper.wallpaperbox.data.repository.WallpaperRepository;
import com.wallpaper.wallpaperbox.topon.AdManager;
import com.wallpaper.wallpaperbox.util.JsonUtil;

import java.util.List;

public class MyApplication extends Application {

    public static MyApplication application;
    public static final int DB_VERSION = 1;
    public static final String DB_NAME = "wallpaper_database";
    private static final String PREF_NAME = "app_preferences";
    private static final String KEY_INITIALIZED = "isInit";

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

    private static final String AppKey = "a7903e950eb399d148180e8cdf19c8644";
    private static final String APPId = "h67b820108e2ec";

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

        application = this;

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

        if (!isDatabaseInitialized) {
            initializeDatabase();
            preferences.edit().putBoolean(KEY_INITIALIZED, true).apply();
        }

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

        initSdk();
    }

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

    }

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

    private void initializeDatabase() {
        WallpaperEntityDao wallpaperEntityDao = AppDatabase.getInstance(getContext()).wallpaperEntityDao();
        WallpaperRepository wallpaperRepository = new WallpaperRepository(wallpaperEntityDao);
        List<WallpaperEntity> wallpaperEntities = JsonUtil.parseJson(getContext(), "wallpaper.json");
        wallpaperRepository.insertAll(wallpaperEntities);
    }
}
