package com.ar.arsketch;

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.arsketch.topon.AdManager;
import com.ar.arsketch.utils.DatabaseUtils;
import com.google.firebase.FirebaseApp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

public class MyApplication extends Application {

    private static MyApplication instance;

    public static final int DB_VERSION = 1;
    public static final String DB_NAME = "image_database";

    private static final String PREF_NAME = "app_preferences";
    private static final String KEY_INITIALIZED = "isDatabaseInitialized";

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

    private static final String AppKey = "a69745311ee01dac38f8f32942ac4a3d4";
    private static final String APPId = "h674b24206bb7e";


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

        instance = this;

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

        initSdk();

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

        if (!isDatabaseInitialized) {
            initializeDatabase();
            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 initializeDatabase() {
        Executors.newSingleThreadExecutor().execute(() -> {
            List<String> imagePaths = getAllImagePathsFromAssets();
            DatabaseUtils databaseUtils = new DatabaseUtils(getContext());
            databaseUtils.insertAllImages(imagePaths);
        });
    }

    private List<String> getAllImagePathsFromAssets() {
        List<String> imagePaths = new ArrayList<>();
        try {
            String[] categories = getContext().getAssets().list("");
            if (categories != null) {
                for (String category : categories) {
                    if (category.startsWith("png_")) {
                        String[] files = getContext().getAssets().list(category);
                        if (files != null) {
                            for (String file : files) {
                                imagePaths.add(category + "/" + file);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            Log.e("MyApplication", "Error loading images from assets", e);
        }
        return imagePaths;
    }

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

}

