package com.ar.paintar;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.ar.paintar.utils.DatabaseUtil;

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 = "preferences";
    private static final String KEY_INITIALIZED = "init";

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

        instance = this;

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

        if (!init) {
            initDatabase();
            preferences.edit().putBoolean(KEY_INITIALIZED, true).apply();
        }

    }

    private void initDatabase() {
        Executors.newSingleThreadExecutor().execute(() -> {
            DatabaseUtil databaseUtil = new DatabaseUtil(getContext());
            List<String> imagePaths = databaseUtil.getAllImagePathsFromAssets();
            databaseUtil.insertAllImages(imagePaths);
        });
    }

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

}

