package studio.junk.jam.coloritum.utils;

import java.util.LinkedList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;

public class InputUtils {
	public static final String keySeparation = ", ";

	public static int[] getKeyBindings(PrefUtils key) {
		return stringToKeys(key.getString());
	}

	public static boolean keyBindingExists(PrefUtils prefKey, int key) {
		int[] keys = getKeyBindings(prefKey);

		for (int k : keys) {
			if (k == key) {
				return true;
			}
		}

		return false;
	}

	public static void removeKeyBinding(PrefUtils prefKey, int key) {
		int[] keys = getKeyBindings(prefKey);

		if (keys.length == 0 || !keyBindingExists(prefKey, key)) {
			return;
		}

		int[] newKeys = new int[keys.length - 1];

		for (int i = 0; i < newKeys.length; i++) {
			if (keys[i] == key) {
				i--;
				return;
			}
			newKeys[i] = keys[i];
		}
	}

	public static void addKeyBinding(PrefUtils prefKey, int key) {
		if (keyBindingExists(prefKey, key)) {
			return;
		}

		String keyNames = keysToString(getKeyBindings(prefKey));

		if (!keyNames.isEmpty()) {
			keyNames += keySeparation;
		}
		keyNames += Keys.toString(key);

		setKeyBindings(prefKey, stringToKeys(keyNames));
	}

	public void changeKeyBinding(PrefUtils prefKey, int keyBefore, int keyAfter) {
		removeKeyBinding(prefKey, keyBefore);
		addKeyBinding(prefKey, keyAfter);
	}

	public static void setKeyBindings(PrefUtils prefKey, int... keys) {
		prefKey.set(keysToString(keys));
	}

	public static boolean isKeyPressed(PrefUtils prefKey) {
		int[] keys = getKeyBindings(prefKey);

		for (int key : keys) {
			if (Gdx.input.isKeyPressed(key)) {
				return true;
			}
		}

		return false;
	}

	public static boolean isKeyJustPressed(PrefUtils prefKey) {
		int[] keys = getKeyBindings(prefKey);

		for (int key : keys) {
			if (Gdx.input.isKeyJustPressed(key)) {
				return true;
			}
		}

		return false;
	}

	public static PrefUtils[] getPressedKeys() {
		LinkedList<PrefUtils> list = new LinkedList<PrefUtils>();

		for (PrefUtils k : PrefUtils.inputKeys) {
			if (isKeyPressed(k)) {
				list.add(k);
			}
		}

		return list.toArray(new PrefUtils[0]);
	}

	public static String[] keysToStrings(int... keys) {
		String[] names = new String[keys.length];

		for (int i = 0; i < keys.length; i++) {
			names[i] = Keys.toString(keys[i]);
		}

		return names;
	}

	public static String keysToString(String separation, int... keys) {
		String[] names = keysToStrings(keys);

		String ret = "";

		boolean first = true;

		for (String name : names) {
			if (first == true) {
				first = false;
			} else {
				ret += separation;
			}

			ret += name;
		}

		return ret;
	}

	public static String keysToString(int... keys) {
		return keysToString(keySeparation, keys);
	}

	public static int[] stringToKeys(String separation, String s) {
		String[] names = s.split(separation);

		int[] ret = new int[names.length];

		for (int i = 0; i < names.length; i++) {
			ret[i] = Keys.valueOf(names[i]);
		}

		return ret;
	}

	public static int[] stringToKeys(String s) {
		return stringToKeys(keySeparation, s);
	}
}
