package de.heatup.ui;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import de.heatup.mapengine.CollisionHandler;
import de.heatup.mapengine.MapGrid;
import de.heatup.objects.Player;
import de.heatup.timers.HardwareResetTimers;
import de.heatup.ui.actionPanel.ActionControl;
import de.heatup.ui.actionPanel.ActionWatcher;
import de.heatup.ui.actionPanel.HardewareManipulate;
import de.heatup.ui.menu.MenuPanel;

public class KeyBindings extends KeyAdapter {
	final private static int progressBarTimer = 7;
	private boolean left = false;
	private boolean right = false;
	private boolean up = false;
	private boolean down = false;
	private boolean esc = false;
	private boolean key = false;
	private Player player;

	public KeyBindings(Player player) {
		this.player = player;
	}

	@Override
	public void keyReleased(KeyEvent evt) {
		int keyCode = evt.getKeyCode();
		switch (keyCode) {
		case KeyEvent.VK_LEFT:
			left = false;
			break;
		case KeyEvent.VK_RIGHT:
			right = false;
			break;
		case KeyEvent.VK_UP:
			up = false;
			break;
		case KeyEvent.VK_DOWN:
			down = false;
			break;
		case KeyEvent.VK_ESCAPE:
			if ( esc ) {
				esc = false;
				MenuPanel.setStartGameLabel("[ Spiel Starten ]");
				MenuPanel.setShowCreditsLabel("[ Credits anzeigen ]");
				MenuPanel.setShowTutorialLabel("[ Credits anzeigen ]");
				MenuPanel.setShowHighscoreLabel("[ Highscore anzeigen ]");
				
			} else {
				esc = true;
				MenuPanel.setStartGameLabel(" weiter mit >>ESC<< ");
				MenuPanel.setShowCreditsLabel("");
				MenuPanel.setShowTutorialLabel("");
				MenuPanel.setShowHighscoreLabel("");
			}
			break;
		case KeyEvent.VK_A:
			key = false;
			break;
		case KeyEvent.VK_Q:
			key = false;
			break;
		case KeyEvent.VK_W:
			key = false;
			break;
		case KeyEvent.VK_E:
			key = false;
			break;
		case KeyEvent.VK_R:
			key = false;
			break;
		}
	}

	@Override
	public void keyPressed(KeyEvent evt) {
		int keyCode = evt.getKeyCode();
		switch (keyCode) {
		case KeyEvent.VK_LEFT:
			left = true;
			break;
		case KeyEvent.VK_RIGHT:
			right = true;
			break;
		case KeyEvent.VK_UP:
			up = true;
			break;
		case KeyEvent.VK_DOWN:
			down = true;
			break;
		case KeyEvent.VK_A:
			if(((MapGrid.getGridCell(PanelBuilder.getPlayer().getGridLocation().x, PanelBuilder.getPlayer().getGridLocation().y).isTrigger()
					&& !HardewareManipulate.getHardware(MapGrid.getGridCell(PanelBuilder.getPlayer().getGridLocation().x, PanelBuilder.getPlayer().getGridLocation().y).getCorrespondingHardwareToTrigger()))&&!key)&&ActionControl.getKeyA()){
				ActionWatcher.setAction(true);
				key=true;
			}
			break;
		case KeyEvent.VK_Q:
			if(ActionWatcher.getAction()&&!key){
				if(ActionControl.getOption()==1){
					if(ActionControl.getSmash()=='Q'){
						ActionControl.setProgressBarTimer(progressBarTimer); 
					}
				}else if(!ActionControl.getKeySequenceFail()){
					if(ActionControl.getSequence()[ActionControl.getCount()]=='Q'){
						ActionControl.incCount();
					}else{
						ActionControl.setKeySequenceFail();
					}
					
				}
				key=true;
			}
			break;
		case KeyEvent.VK_W:
			if(ActionWatcher.getAction()&&!key){
				if(ActionControl.getOption()==1){
					if(ActionControl.getSmash()=='W'){;
					ActionControl.setProgressBarTimer(progressBarTimer);
					}
				}else if(!ActionControl.getKeySequenceFail()){
					if(ActionControl.getSequence()[ActionControl.getCount()]=='W'){
						ActionControl.incCount();
					}else{
						ActionControl.setKeySequenceFail();
					}
				}
				key=true;
			}
			break;
		case KeyEvent.VK_E:
			if(ActionWatcher.getAction()&&!key){
				if(ActionControl.getOption()==1){
					if(ActionControl.getSmash()=='E'){
						ActionControl.setProgressBarTimer(progressBarTimer);
					}
				}else if(!ActionControl.getKeySequenceFail()){
					if(ActionControl.getSequence()[ActionControl.getCount()]=='E'){
						ActionControl.incCount();
					}else{
						ActionControl.setKeySequenceFail();
					}
				}
				key=true;
			}
			break;
		case KeyEvent.VK_R:
			if(ActionWatcher.getAction()&&!key){
				if(ActionControl.getOption()==1){
					if(ActionControl.getSmash()=='R'){
						ActionControl.setProgressBarTimer(progressBarTimer);
					}
				}else if(!ActionControl.getKeySequenceFail()){
					if(ActionControl.getSequence()[ActionControl.getCount()]=='R'){
						ActionControl.incCount();
					}else{
						ActionControl.setKeySequenceFail();
					}
				}
				key=true;
			}
			break;
		}
	}

	public void checkKeyActivity() {
		if (isLeft() && CollisionHandler.checkCollision(player, 'l')) {
			if (!player.animate ) {
				player.setGridLocation(player.getGridLocation().x-1, player.getGridLocation().y);
				player.step('x', -1);
				player.startMove = true;
			}
			player.animate = true;
		}
		if (isRight() && CollisionHandler.checkCollision(player, 'r')) {
			if (!player.animate ) {
				player.setGridLocation(player.getGridLocation().x+1, player.getGridLocation().y);
				player.step('x', 1);
				player.startMove = true;
			}
			player.animate = true;
		}
		if (isUp() && CollisionHandler.checkCollision(player, 'u')) {
			if (!player.animate ) {
				player.setGridLocation(player.getGridLocation().x, player.getGridLocation().y-1);
				player.step('y', -1);
				player.startMove = true;
			}
			player.animate = true;
		}
		if (isDown()  && CollisionHandler.checkCollision(player, 'd')) {
			if (!player.animate ) {
				player.setGridLocation(player.getGridLocation().x, player.getGridLocation().y+1);
				player.step('y', 1);
				player.startMove = true;
			}
			player.animate = true;
		}
		if (isESC()) {
			Main.pause();
			esc=false;
		}
	}

	public boolean isLeft() {
		return left;
	}

	public boolean isRight() {
		return right;
	}

	public boolean isUp() {
		return up;
	}

	public boolean isDown() {
		return down;
	}
	public boolean isESC() {
		return esc;
	}
}