package com.prank.funnypranksounds;

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

import com.prank.funnypranksounds.data.local.entity.AudioData;
import com.prank.funnypranksounds.data.repository.AudioRepository;
import com.prank.funnypranksounds.utils.JsonParser;

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

public class MyApplication extends Application {

    public static MyApplication myApplication;
    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();

        myApplication = 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() {
        AudioRepository imageRepository = new AudioRepository(getContext());
        List<AudioData> audioDataList = JsonParser.parseJson("prank.json");
        imageRepository.insertAll(audioDataList);
    }

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