package com.ardrawing.ardrawingspace;

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.ardrawing.ardrawingspace.topon.AdManager;
import com.ardrawing.ardrawingspace.util.FileParsingAndInitialization;
import com.google.firebase.FirebaseApp;

public class MyApplication extends Application {
    public static MyApplication application;
    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 = "initRoom";

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

    private static final String AppKey = "a51e25ab422a76eaf669a89adfb6da236";
    private static final String APPId = "h67ee492745742";
    private static final String debug_KEy = "529b9d845e53ac839536d321c0b6be5f6928b24d";

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

        application = 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() {
        FileParsingAndInitialization fileParsingAndInitialization = new FileParsingAndInitialization(application);
        fileParsingAndInitialization.insertImagesToDatabase();
    }

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