package com.ar.ardrawingboard;

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.ardrawingboard.topon.AdManager;
import com.ar.ardrawingboard.util.RoomInit;

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 DB_VERSION = 1;
    public static final String DB_NAME = "room";

    private static final String PREF_NAME = "app_preferences";
    private static final String KEY_INIT = "isInit";

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

    private static final String AppKey = "a8e977e907379b1057704fe995baa4389";
    private static final String APPId = "h682153551167d";
    private static final String debug_KEy = "d0a434af69020f58cd1c246da4adb0630eb18a02";

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

        application = this;

        SharedPreferences preferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
        boolean isInit = preferences.getBoolean(KEY_INIT, false);

        if (!isInit) {
            initRoom();
            preferences.edit().putBoolean(KEY_INIT, 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 initRoom() {
        Executors.newSingleThreadExecutor().execute(() -> {
            List<String> imagePaths = getAllImagePathsFromAssets();
            RoomInit roomInit = new RoomInit(application);
            roomInit.insertAllImages(imagePaths);
        });
    }

    private List<String> getAllImagePathsFromAssets() {
        List<String> imagePaths = new ArrayList<>();
        try {
            String[] categories = application.getAssets().list("");
            if (categories != null) {
                for (String category : categories) {
                    if (category.startsWith("png_")) {
                        String[] files = application.getAssets().list(category);
                        if (files != null) {
                            for (String file : files) {
                                imagePaths.add(category + "/" + file);
                            }
                        }
                    }
                }
            }
        } catch (IOException ignored) {
        }
        return imagePaths;
    }

}
