package com.key.vibekeyboard;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.anythink.core.api.ATSDK;
import com.anythink.core.api.NetTrafficeCallback;
import com.key.vibekeyboard.Room.Category;
import com.key.vibekeyboard.Room.MyDatabase;
import com.key.vibekeyboard.Room.WallpaperInfo;
import com.key.vibekeyboard.Utils.Mytool;
import com.key.vibekeyboard.topon.AdManager;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class AppApplication extends Application {

    public static AppApplication instance;
    public static List<Category> categories;

    public static final int DB_Version = 1;
    public static final String DB_Name = "image_database";

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

    private static final String AppKey = "a8a9ae70a7de9cc350e99f820674c1456";
    private static final String APPId = "h67468b57d3beb";
    private static final String debug_KEy = "5c12401a5c59afe9a3ac946dd22ae5f1fecd8c1c";

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        categories = Mytool.parseJsonToList("keyboard.json");

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

        initSdk();

        Mytool.runIO(new Runnable() {
            @Override
            public void run() {

                MyDatabase.getInstance().wallpaperInfoDao().insertAll(getAlldataList());

            }
        });

    }

    private void initSdk() {

        ATSDK.checkIsEuTraffic(this, new NetTrafficeCallback() {

            @Override
            public void onResultCallback(boolean isEU) {
                Log.e(TAG, "onResultCallback:" + isEU);
                if (isEU && ATSDK.getGDPRDataLevel(AppApplication.this) == ATSDK.UNKNOWN) {
                    ATSDK.showGdprAuth(AppApplication.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);

    }

    public static AppApplication getInstance() {
        return instance;
    }

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

    public static List<Category> getCategories() {
        return categories;
    }

    public static List<WallpaperInfo> getAlldataList() {
        List<WallpaperInfo> otherdata = new ArrayList<>();
        List<Category> mydataList = getCategories();
        Set<String> set = new HashSet<>();
        for (Category mydata : mydataList) {
            List<WallpaperInfo> otherdataList = mydata.getList();
            for (WallpaperInfo otherdata1 : otherdataList) {
                String title = otherdata1.getTitle();
                if (!set.contains(title)) {
                    otherdata.add(otherdata1);
                    set.add(title);
                }
            }
        }

        return otherdata;
    }
}

