package com.prank.happypranksounds;

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

import com.prank.happypranksounds.data.dao.HappyDataDao;
import com.prank.happypranksounds.data.entity.HappyData;
import com.prank.happypranksounds.data.database.AppDatabase;
import com.prank.happypranksounds.data.repository.HappyRepository;
import com.prank.happypranksounds.util.JsonParser;

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() {
        HappyDataDao happyDataDao = AppDatabase.getInstance(getContext()).happyDataDao();
        HappyRepository happyRepository = new HappyRepository(happyDataDao);
        List<HappyData> happyDataList = JsonParser.parseJson("prank.json");
        happyRepository.insertAll(happyDataList);
    }

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