package com.ar.artraceartpro;

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.ar.artraceartpro.data.local.entity.ImageData;
import com.ar.artraceartpro.data.repository.ImageRepository;
import com.ar.artraceartpro.topon.AdManager;
import com.ar.artraceartpro.utils.Files;

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

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 = "initDatabase";

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

    private static final String AppKey = "a63169c648e729370921fe0df68151e2d";
    private static final String APPId = "h676ba79d5e6a0";
    private static final String debug_KEy = "5c12401a5c59afe9a3ac946dd22ae5f1fecd8c1c";

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

        application = this;

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

        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() {
        ImageRepository imageRepository = new ImageRepository(getContext());
        List<String> imagePaths = Files.getAllImagePath();
        imageRepository.insertAll(insertAll(imagePaths));
    }

    private List<ImageData> insertAll(List<String> imagePaths) {
        List<ImageData> imageEntries = new ArrayList<>();
        for (String path : imagePaths) {
            imageEntries.add(new ImageData(false, false, path));
        }
        return imageEntries;
    }

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

}
