package com.wallpaper.perfectwallpaper;

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


import com.wallpaper.perfectwallpaper.room.Category;
import com.wallpaper.perfectwallpaper.utils.DatabaseUtils;
import com.wallpaper.perfectwallpaper.utils.JsonParse;

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 = "image_database";
    private static final String PREF_NAME = "app_preferences";
    private static final String KEY_INITIALIZED = "isDatabaseInitialized";


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

        application = this;

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

        if (!isDatabaseInitialized) {
            initializeDatabase();
            preferences.edit().putBoolean(KEY_INITIALIZED, true).apply();
        }

    }

    public static Context getContext() {

        return application.getApplicationContext();

    }

    private void initializeDatabase() {
        Executors.newSingleThreadExecutor().execute(() -> {
            List<Category> categoryList = getCategoryList();
            DatabaseUtils databaseUtils = new DatabaseUtils(getContext());
            databaseUtils.insertAllImages(categoryList);
            Log.d("abcscc", "abc" + categoryList);
        });
    }

    public static List<Category> getCategoryList() {
        return JsonParse.parseJson("wallpaper.json");
    }
}
