package zero1hd.polyjet.controls;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

import zero1hd.polyjet.Polyjet;

public class KeyMap {
	TextureAtlas keyTextures;
	private Preferences keyBindPrefs;
	
	public static final String UP = "up";
	public static final String DOWN = "down";
	public static final String LEFT = "left";
	public static final String RIGHT = "right";
	
	public static final String SHOOT = "shoot";
	
	public static final String FIRSTTHIRDTELEPORT = "firstThirdTP";
	public static final String SECONDTHIRDTELEPORT = "secondThirdTP";
	public static final String THIRDTHIRDTELEPORT = "thirdThirdTP";
	
	/**
	 * forward binding.
	 */
	public static int up;
	/**
	 * backward binding.
	 */
	public static int down;
	/**
	 * left binding.
	 */
	public static int left;
	/**
	 * right binding.
	 */
	public static int right;
	/**
	 * shoot binding
	 */
	public static int shoot;
	/**
	 * teleport first third binding
	 */
	public static int firstThirdTeleport;
	/**
	 * teleport second third binding
	 */
	public static int secondThirdTeleport;
	/**
	 * teleport third third binding
	 */
	public static int thirdThirdTeleport;
	
	
	public KeyMap(Polyjet core) {
		keyTextures = core.getAssetManager().get("keyboard.atlas", TextureAtlas.class);
		
		setKeys(Gdx.app.getPreferences("PolyJet_Controls"));
		updateKeys();
	}

	public Preferences getKeys() {
		return keyBindPrefs;
	}

	public void setKeys(Preferences keys) {
		this.keyBindPrefs = keys;
	}
	
	public TextureRegion getIcon(int keycode) {
//		Gdx.app.debug("Keycode texture name", Keys.toString(keycode));

		switch (keycode) {
		case Keys.ALT_LEFT:
		case Keys.ALT_RIGHT:
			return keyTextures.findRegion("Keyboard_Black_Alt");
		case Keys.SHIFT_LEFT:
		case Keys.SHIFT_RIGHT:
			return keyTextures.findRegion("Keyboard_Black_Shift");
		case Keys.LEFT_BRACKET:
			return keyTextures.findRegion("Keyboard_Black_Bracket_Left");
		case Keys.RIGHT_BRACKET:
			return keyTextures.findRegion("Keyboard_Black_Bracket_Right");
		case Keys.SEMICOLON:
			return keyTextures.findRegion("Keyboard_Black_Semicolon");
		case Keys.SLASH:
			return keyTextures.findRegion("Keyboard_Black_Slash");
		case Keys.NUM:
			return keyTextures.findRegion("Keyboard_Black_Num_Lock");
		default:
			return keyTextures.findRegion("Keyboard_Black_" + Keys.toString(keycode).replace(' ', '_'));
		}
	}
	
	public void updateKeys() {
		up = keyBindPrefs.getInteger(KeyMap.UP, Keys.UP);
		down = keyBindPrefs.getInteger(KeyMap.DOWN, Keys.DOWN);
		left = keyBindPrefs.getInteger(KeyMap.LEFT, Keys.LEFT);
		right = keyBindPrefs.getInteger(KeyMap.RIGHT, Keys.RIGHT);
		
		shoot = keyBindPrefs.getInteger(KeyMap.SHOOT, Keys.SPACE);
		
		firstThirdTeleport = keyBindPrefs.getInteger(KeyMap.FIRSTTHIRDTELEPORT, Keys.Q);
		secondThirdTeleport = keyBindPrefs.getInteger(KeyMap.SECONDTHIRDTELEPORT, Keys.W);
		thirdThirdTeleport = keyBindPrefs.getInteger(KeyMap.THIRDTHIRDTELEPORT, Keys.E);
	}
	
	/**
	 * resets all binding to default;
	 */
	public void resetAllBinding() {
		keyBindPrefs.clear();
		keyBindPrefs.flush();
	}
	
	public int stringToID(String control) {
		if (control == KeyMap.UP) {
			return KeyMap.up;
		} else if (control == KeyMap.DOWN) {
			return KeyMap.down;
		} else if (control == KeyMap.LEFT) {
			return KeyMap.left;
		} else if (control == KeyMap.RIGHT) {
			return KeyMap.right;
		} else if (control == KeyMap.SHOOT) {
			return KeyMap.shoot;
		} else if (control == KeyMap.FIRSTTHIRDTELEPORT) {
			return KeyMap.firstThirdTeleport;
		} else if (control == KeyMap.SECONDTHIRDTELEPORT) {
			return KeyMap.secondThirdTeleport;
		} else if (control == KeyMap.THIRDTHIRDTELEPORT) {
			return KeyMap.thirdThirdTeleport;
		} else {
			return -1;
		}
	}
}
