package de.frajul.endlessroll.main;

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

import de.frajul.endlessroll.entities.shapes.PlayerShape;
import de.frajul.endlessroll.entities.tools.ToolType;
import de.frajul.endlessroll.sqlDatabase.MyDatabase;
import de.frajul.endlessroll.user.ToolSlotSettings;
import de.frajul.endlessroll.user.User;

/**
 * Created by Julian on 25.04.2016.
 */
public class DataStorageHandler {

    private final String PREFERENCES_NAME = "GamePreferences";
    private final String SOUNDS_MUTED = "SoundsMuted";
    private final String MUSIC_MUTED = "MusicMuted";
    private final String USER_EP = "EP";
    private final String USER_LEVEL = "Level";
    private final String USER_STARS = "Stars";
    private final String USER_ENERGY = "Energy";
    private final String USER_TOOL_1 = "Tool1";
    private final String USER_TOOL_2 = "Tool2";
    private final String USER_TOOL_3 = "Tool3";
    private final String USER_TOOL_4 = "Tool4";
    private final String USER_TOOLS_LOCKED = "ToolsLocked";
    private final String USER_PLAYER_SHAPE = "PlayerShape";
    private final String TOOL_SHOP_TUTORIAL_FINISHED = "ToolShopTutorialFinished";

    private SharedPreferences preferences;
    private MyDatabase database;

    public DataStorageHandler(Activity activity) {
        preferences = activity.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        database = new MyDatabase(activity);
    }

    public boolean readIsSoundsMuted() {
        return preferences.getBoolean(SOUNDS_MUTED, false);
    }

    public void writeSoundsMuted(boolean soundsMuted) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(SOUNDS_MUTED, soundsMuted);
        editor.apply();
    }

    public boolean readIsMusicMuted() {
        return preferences.getBoolean(MUSIC_MUTED, false);
    }

    public void writeMusicMuted(boolean musicMuted) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(MUSIC_MUTED, musicMuted);
        editor.apply();
    }

    public User readUserData(User.LvUpListener lvUpListener) throws Exception {
        int ep = preferences.getInt(USER_EP, 0);
        int level = preferences.getInt(USER_LEVEL, 1);
        int stars = preferences.getInt(USER_STARS, 0);
        int energy = preferences.getInt(USER_ENERGY, 0);
        int toolsLocked = preferences.getInt(USER_TOOLS_LOCKED, 3);
        String tool1 = preferences.getString(USER_TOOL_1, ToolType.RAMP.name());
        String tool2 = preferences.getString(USER_TOOL_2, "null");
        String tool3 = preferences.getString(USER_TOOL_3, "null");
        String tool4 = preferences.getString(USER_TOOL_4, "null");
        ToolSlotSettings toolSlotSettings = new ToolSlotSettings(tool1, tool2, tool3, tool4,
                toolsLocked);
        String playerShapeName = preferences.getString(USER_PLAYER_SHAPE, PlayerShape.BALL.name());
        PlayerShape playerShape;
        try {
            playerShape = PlayerShape.valueOf(playerShapeName);
        } catch (Exception e) {
            playerShape = PlayerShape.BALL;
        }
        return new User(lvUpListener, ep, level, stars, energy, toolSlotSettings, playerShape);
    }

    public void writeUserData(User user) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(USER_EP, user.getEp());
        editor.putInt(USER_LEVEL, user.getLevel());
        editor.putInt(USER_STARS, user.getStarCount());
        editor.putInt(USER_ENERGY, user.getEnergyCount());
        editor.putString(USER_TOOL_1, toolSlotToString(user.getToolSlotSettings(), 0));
        editor.putString(USER_TOOL_2, toolSlotToString(user.getToolSlotSettings(), 1));
        editor.putString(USER_TOOL_3, toolSlotToString(user.getToolSlotSettings(), 2));
        editor.putString(USER_TOOL_4, toolSlotToString(user.getToolSlotSettings(), 3));
        editor.putInt(USER_TOOLS_LOCKED, user.getToolSlotSettings().getLockedSlotCount());
        editor.putString(USER_PLAYER_SHAPE, user.getCurrentPlayerShape().name());
        editor.apply();
    }

    private String toolSlotToString(ToolSlotSettings toolSlotSettings, int index) {
        ToolType toolType = toolSlotSettings.get(index).getToolType();
        return toolType == null ? "null" : toolType.toString();
    }

    public void writeToolShopTutorialFinished(boolean finished) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(TOOL_SHOP_TUTORIAL_FINISHED, finished);
        editor.apply();
    }

    public boolean readToolShopTutorialFinished() {
        return preferences.getBoolean(TOOL_SHOP_TUTORIAL_FINISHED, false);
    }

    public MyDatabase getDatabase() {
        return database;
    }
}
