package de.siphalor.was.game;

import de.siphalor.was.WhatAStorage;
import de.siphalor.was.content.lang.I18n;

import java.util.Locale;
import java.util.prefs.Preferences;

/**
 * Holds the game's options and is responsible for saving and loading them.
 */
public class Options {
	/**
	 * Whether valid targets for quests/products should be highlighted
	 */
	private boolean previewValidTargets;
	/**
	 * Whether quest should get automatically added when fulfilled
	 */
	private boolean autoRefillQuests;
	/**
	 * Allow resolving of two quests that target the same product. One asks to deposit it, the other to deliver it
	 */
	private boolean allowQuestResolving;
	/**
	 * Whether the sound is enabled
	 */
	private boolean soundEnabled;

	/**
	 * Gets whether quest resolving is enabled.
	 * @return Whether quest resolving is enabled
	 */
	public boolean getAllowQuestResolving() {
		return allowQuestResolving;
	}

	/**
	 * Sets whether quest resolving should be enabled.
	 * @param allowQuestResolving Whether it should be enabled
	 */
	public void setAllowQuestResolving(boolean allowQuestResolving) {
		this.allowQuestResolving = allowQuestResolving;
	}

	/**
	 * Gets whether fulfilled quests should automatically be refilled.
	 * @return Whether fulfilled quests should automatically be refilled
	 */
	public boolean getAutoRefillQuests() {
		return autoRefillQuests;
	}

	/**
	 * Sets whether fulfilled quests should automatically be refilled.
	 * @param autoRefillQuests Whether fulfilled quests should automatically be refilled
	 */
	public void setAutoRefillQuests(boolean autoRefillQuests) {
		this.autoRefillQuests = autoRefillQuests;
	}

	/**
	 * Gets whether the sound is enabled.
	 * @return Whether the sound is enabled
	 */
	public boolean getSoundEnabled() {
		return soundEnabled;
	}

	/**
	 * Sets whether the sound should be enabled.
	 * @param soundEnabled Whether the sound should be enabled
	 */
	public void setSoundEnabled(boolean soundEnabled) {
		this.soundEnabled = soundEnabled;
	}

	public boolean getPreviewValidTargets() {
		return previewValidTargets;
	}

	public void setPreviewValidTargets(boolean previewValidTargets) {
		this.previewValidTargets = previewValidTargets;
	}

	/**
	 * Gets a reference to the preferences object used for session independent storage.
	 * @return The preferences object
	 */
	private Preferences getPreferences() {
		return Preferences.userNodeForPackage(WhatAStorage.class);
	}

	/**
	 * Loads the options from the storage.
	 */
	public void load() {
		Preferences preferences = getPreferences();
		previewValidTargets = preferences.getBoolean("preview-valid-targets", true);
		autoRefillQuests = preferences.getBoolean("auto-refill-quests", false);
		allowQuestResolving = preferences.getBoolean("allow-quest-resolving", true);
		soundEnabled = preferences.getBoolean("sound-enabled", false);

		String lang = preferences.get("preferred-lang", Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry());
		I18n.getInstance().setLang(lang, WhatAStorage.getInstance().getContentManager());
	}

	/**
	 * Saves the options data to the storage
	 */
	public void save() {
		Preferences preferences = getPreferences();
		preferences.putBoolean("preview-valid-targets", previewValidTargets);
		preferences.putBoolean("auto-refill-quests", autoRefillQuests);
		preferences.putBoolean("allow-quest-resolving", allowQuestResolving);
		preferences.putBoolean("sound-enabled", soundEnabled);
		preferences.put("preferred-lang", I18n.getInstance().getLang().getCode());
	}
}
