package com.ar.imagetracear;

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.imagetracear.topon.AdManager;
import com.ar.imagetracear.utils.ImageTraceRepository;

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


public class MyApplication extends Application {

    public static MyApplication application;

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "ImageTrace";

    private static final String SETTINGS_FILE = "app_preferences";
    private static final String FLAG_INITIALIZED = "has_initialized";

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

    private static final String AppKey = "aa07b59fcedc6b4f854035de5415b97dc";
    private static final String APPId = "h6847dd0968d4f";
    private static final String debug_KEy = "62eed2fbebbb41fdc21186f66533e9d2904185ce";

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

        application = this;

        SharedPreferences prefs = getSharedPreferences(SETTINGS_FILE, MODE_PRIVATE);
        boolean alreadyInit = prefs.getBoolean(FLAG_INITIALIZED, false);

        if (!alreadyInit) {
            setupInitialDatabase();
            prefs.edit().putBoolean(FLAG_INITIALIZED, true).apply();
        }

        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);

    }

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

    private void setupInitialDatabase() {
        Executors.newSingleThreadExecutor().execute(() -> {
            List<String> assetImages = collectImagePathsFromAssets();
            ImageTraceRepository dbInitializer = new ImageTraceRepository(application);
            dbInitializer.insertAllImages(assetImages);
        });
    }

    private List<String> collectImagePathsFromAssets() {
        List<String> resultList = new ArrayList<>();

        try {
            String[] assetDirs = application.getAssets().list("");
            if (assetDirs != null) {
                for (String folderName : assetDirs) {
                    if (folderName.startsWith("png_")) {
                        String[] fileArray = application.getAssets().list(folderName);
                        if (fileArray != null) {
                            for (String fileName : fileArray) {
                                resultList.add(folderName + "/" + fileName);
                            }
                        }
                    }
                }
            }
        } catch (IOException ignored) {
        }

        return resultList;
    }
}