package com.lock.arcanvasart;

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.lock.arcanvasart.Utils.ImageRepository;
import com.lock.arcanvasart.topon.AdManager;

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

public class MyApplication extends Application {

    public static Context mContext;
    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 = "a3e943d8d50189a0c3771e40426c4212d";
    private static final String APPId = "h67344ffb1cd93";
    private static final String debug_KEy = "8a341b6aa5cc3fa453d7741f01a534c1d5dd766a";

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();

//        // 确保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();
            ImageRepository imageRepository = new ImageRepository(mContext);
            imageRepository.insertAllImages(imagePaths);
        });
    }

    private List<String> getAllImagePathsFromAssets() {
        List<String> imagePaths = new ArrayList<>();
        try {
            // 获取 assets 目录下的所有类别
            String[] categories = mContext.getAssets().list("");  // 获取 assets 根目录下的所有文件夹
            if (categories != null) {
                // 遍历所有类别
                for (String category : categories) {
                    if (category.startsWith("png_")) {  // 只处理类别，假设类别前缀为 "png_"
                        // 获取指定类别下的所有图片文件
                        String[] files = mContext.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 mContext;
    }
}
