package studio.junk.jam.coloritum.utils;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics.DisplayMode;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Preferences;

public enum PrefUtils {

	resolutionWidth(Gdx.graphics.getDisplayMode().width), //
	resolutionHeight(Gdx.graphics.getDisplayMode().height), //
	resolutionRefreshRate(Gdx.graphics.getDisplayMode().refreshRate), //
	resolutionBitsPerPixel(Gdx.graphics.getDisplayMode().bitsPerPixel), //
	fullscreen(true),

	// Key definition

	keyMoveLeft(InputUtils.keysToString(Keys.A, Keys.LEFT)), //
	keyMoveRight(InputUtils.keysToString(Keys.D, Keys.RIGHT)), //
	keyJump(InputUtils.keysToString(Keys.SPACE)), //
	keyAbility(InputUtils.keysToString(Keys.SHIFT_LEFT, Keys.SHIFT_RIGHT));

	public static final PrefUtils[] inputKeys = { keyMoveLeft, keyMoveRight, keyJump, keyAbility };

	public static final String filePath = "cala_prefs";

	public final Object defaultValue;

	private PrefUtils(Object defaultValue) {
		this.defaultValue = defaultValue;
	}

	public void set(int value) {
		Preferences prefs = getPrefs();
		prefs.putInteger(name(), value);
		prefs.flush();
	}

	public void set(long value) {
		Preferences prefs = getPrefs();
		prefs.putLong(name(), value);
		prefs.flush();
	}

	public void set(float value) {
		Preferences prefs = getPrefs();
		prefs.putFloat(name(), value);
		prefs.flush();
	}

	public void set(String value) {
		Preferences prefs = getPrefs();
		prefs.putString(name(), value);
		prefs.flush();
	}

	public void set(boolean value) {
		Preferences prefs = getPrefs();
		prefs.putBoolean(name(), value);
		prefs.flush();
	}

	public int getInt() {
		return getPrefs().getInteger(name(), (Integer) defaultValue);
	}

	public long getLong() {
		return getPrefs().getLong(name(), (Long) defaultValue);
	}

	public float getFloat() {
		return getPrefs().getFloat(name(), (Float) defaultValue);
	}

	public String getString() {
		return getPrefs().getString(name(), (String) defaultValue);
	}

	public boolean getBoolean() {
		return getPrefs().getBoolean(name(), (Boolean) defaultValue);
	}

	public static Preferences getPrefs() {
		return Gdx.app.getPreferences(filePath);
	}

	public static DisplayMode getDisplayModeFromPreferences() {

		int dmw = resolutionWidth.getInt();
		int dmh = resolutionHeight.getInt();
		int dmr = resolutionRefreshRate.getInt();
		int dmb = resolutionBitsPerPixel.getInt();

		DisplayMode[] displayModes = Gdx.graphics.getDisplayModes();

		DisplayMode bestFit = null;

		for (DisplayMode cdm : displayModes) {
			if (bestFit == null) {
				bestFit = cdm;

			} else if (Math.abs(bestFit.width * bestFit.height - dmw * dmh) > Math
					.abs(cdm.width * cdm.height - dmw * dmh)) {
				bestFit = cdm;

			} else if (Math.abs(bestFit.width * bestFit.height - dmw * dmh) == Math
					.abs(cdm.width * cdm.height - dmw * dmh)) {
				if (Math.abs(bestFit.bitsPerPixel - dmb) > Math.abs(cdm.bitsPerPixel - dmb)) {
					bestFit = cdm;

				} else if (Math.abs(bestFit.bitsPerPixel - dmb) == Math.abs(cdm.bitsPerPixel - dmb)) {
					if (Math.abs(bestFit.refreshRate - dmr) > Math.abs(cdm.refreshRate - dmr)) {
						bestFit = cdm;

					}
				}
			}
		}

		return bestFit;
	}

	public static void setDisplayModePreferences(DisplayMode dm) {
		resolutionWidth.set(dm.width);
		resolutionHeight.set(dm.height);
		resolutionRefreshRate.set(dm.refreshRate);
		resolutionBitsPerPixel.set(dm.bitsPerPixel);
	}
}
