package zero1hd.rhythmbullet.desktop;

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

public enum Controls {
	UP("Forward", Keys.UP), DOWN("Backwards", Keys.DOWN), LEFT("Left", Keys.LEFT), RIGHT("Right", Keys.RIGHT), SHOOT("Shoot", Keys.SPACE), ACCELERATE("Boost", Keys.SHIFT_LEFT);
	private int keycode;
	private final String friendly;
	private final int defectKey;
	
	private Controls(String friendly, int defectKey) {
		this.friendly = friendly;
		this.defectKey = defectKey;
		this.keycode = defectKey;
	}
	
	public Controls setKeycode(int keycode) {
		Controls[] controls = Controls.values();
		for (int i = 0; i < controls.length; i++) {
			if (controls[i] != this && controls[i].getKeycode() == keycode) {
				return controls[i];
			}
		}
		this.keycode = keycode;
		return null;
	}
	
	public int getKeycode() {
		return keycode;
	}
	
	public void loadKeyFromPreferences(Preferences preferences) {
		setKeycode(preferences.getInteger(name(), defectKey));
	}
	
	public void saveKeyToPreferences(Preferences preferences) {
		preferences.putInteger(name(), keycode);
	}
	
	@Override
	public String toString() {
		return friendly;
	}
}
