package com.ardraw.ardrawinggallery;

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.ardraw.ardrawinggallery.topon.AdManager;
import com.ardraw.ardrawinggallery.util.ArDataInsert;

public class MyApplication extends Application {
    public static MyApplication application;
    private static final String PREF_NAME = "preferences";
    private static final String KEY_INITIALIZED = "initRoom";

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

    private static final String AppKey = "a06286132dc373d26645ec72e90bac766";
    private static final String APPId = "h67ceb38985a9f";
    private static final String debug_KEy = "8a341b6aa5cc3fa453d7741f01a534c1d5dd766a";

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

        application = this;

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

        if (!init) {
            initDatabase();
            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);

    }


    private void initDatabase() {
        ArDataInsert arDataInsert = new ArDataInsert(getContext());
        arDataInsert.insertImagesToDatabase();
        Log.d("MyApplication", "Database initialized");
    }

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