package com.ar.artraceartpro;

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

import com.ar.artraceartpro.data.local.entity.ImageData;
import com.ar.artraceartpro.data.repository.ImageRepository;
import com.ar.artraceartpro.utils.Files;

import java.util.ArrayList;
import java.util.List;

public class MyApplication extends Application {

    public static MyApplication application;
    public static final String Database_Name = "Database";
    public static final int Database_Version = 1;
    private static final String PREF_NAME = "preferences";
    private static final String KEY_INITIALIZED = "initDatabase";

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

        application = 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() {
        ImageRepository imageRepository = new ImageRepository(getContext());
        List<String> imagePaths = Files.getAllImagePath();
        imageRepository.insertAll(insertAll(imagePaths));
    }

    private List<ImageData> insertAll(List<String> imagePaths) {
        List<ImageData> imageEntries = new ArrayList<>();
        for (String path : imagePaths) {
            imageEntries.add(new ImageData(false, false, path));
        }
        return imageEntries;
    }

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

}
