package coolPlayerControl;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import knowledgeAndTechnics.BaseFunctions;


/*
 * Created on 27.10.2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class KeyControl implements KeyListener{
	private final int timer = 7;
	private final int inc = 1;
	private final boolean logging = true;
	private final int turnAmount = 20;
	private final int dashAmount = 90;
	
	private boolean  spacePressed;
	private boolean  upArrowPressed;
	private boolean  downArrowPressed;
	private boolean  leftArrowPressed;
	private boolean  rightArrowPressed;
	private int      runPriority = 0;
	private int      turnPriority = 0;
	private boolean  shooting;
	private SpaceInc shooter = null;
	private boolean  shoot = false;
	private int      shootPower;
	private int runDir = 0;
	private int turnDir = 0;
	private CoolPlayerControl parent;
	private BaseFunctions baseFunctions;
	
	public KeyControl(CoolPlayerControl parent, BaseFunctions baseFunctions){
		this.parent = parent;
		this.baseFunctions = baseFunctions;
	}
	
	
	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	 */
	public void keyPressed(KeyEvent event) {
		switch (event.getKeyCode()){
			case(KeyEvent.VK_SPACE): startShooting(); break;
			case(KeyEvent.VK_UP): upArrowPressed = true; break;
			case(KeyEvent.VK_DOWN): downArrowPressed = true; break;
			case(KeyEvent.VK_LEFT): leftArrowPressed = true; break;
			case(KeyEvent.VK_RIGHT): rightArrowPressed = true; break;
		}
	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	 */
	public void keyReleased(KeyEvent event) {
		switch (event.getKeyCode()){
			case(KeyEvent.VK_SPACE): stopShooting(); break;
			case(KeyEvent.VK_UP): upArrowPressed = false; runPriority = 0; turnPriority = 0; break;
			case(KeyEvent.VK_DOWN): downArrowPressed = false; runPriority = 0; turnPriority = 0; break;
			case(KeyEvent.VK_LEFT): leftArrowPressed = false; runPriority = 0; turnPriority = 0; break;
			case(KeyEvent.VK_RIGHT): rightArrowPressed = false; runPriority = 0; turnPriority = 0; break;
		}
	}

	private synchronized void startShooting(){
		if (shooting) return;
		shooter = new SpaceInc(timer, inc, this);
		shooter.start();
		shooting = true;
	}
	
	private void stopShooting(){
		shooter.stopRunning();
		shootPower = shooter.getValue();
		shoot = true;
		shooting = false;
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	 */
	public void keyTyped(KeyEvent event) {
		/*do nothing*/
	}

	public void tick() {
		if (shoot) {
			/*sender.kick(power > 100? 100 : power);*/
			//log ("Kick! power: " + (shootPower > 100? 100 : shootPower));
			baseFunctions.kick(shootPower, 0);
			shoot = false;
			return;
		}
		
		runDir = 0;
		turnDir = 0;
		if (upArrowPressed ^ downArrowPressed){
			if (upArrowPressed) runDir = 1; 
			else runDir = -1;
		}	
		if (leftArrowPressed ^ rightArrowPressed){ 
			if (rightArrowPressed) turnDir = 1; 
			else turnDir = -1;
		}
		
		if ((runDir != 0) && (turnDir != 0)){
			if (turnPriority > runPriority){
				/*sender.turn(turnDir * turnAmount);*/
				//log ("turn! amount: " + turnDir * turnAmount);
				baseFunctions.turn(turnDir * turnAmount);
				runPriority++;
				turnPriority = 0;
			} else {
				/*sender.dash(runDir * dashAmount);*/
				//log ("dash! amount: " + runDir * dashAmount);
				baseFunctions.dash(runDir * dashAmount);
				turnPriority++;
				runPriority = 0;
			}
			return;
		}
		if (runDir != 0){
			/*sender.dash(runDir * dashAmount);*/
			baseFunctions.dash(runDir * dashAmount);;
			return;
		}
		if (turnDir != 0){
			baseFunctions.turn(turnDir * turnAmount);
			
		}
	}
	
	CoolPlayerControl getParent(){
		return parent;
	}
	
}



class SpaceInc extends Thread{
	private int        time;
	private int        inc;
	private KeyControl parent;
	private boolean    running = true;
	private int        value = 0;
	
	public SpaceInc(int time, int inc, KeyControl parent){
		this.time   = time;
		this.inc    = inc;
		this.parent = parent;
	}
	public void run(){
		while (running) {
			value += inc;
			parent.getParent().setKickPower(value);
			try{
				Thread.sleep(time);
			}
			catch (InterruptedException e){
				/*do nothing*/
			}
		}
		
	}
	
	public void stopRunning(){
		running = false;
	}
	
	
	public int getValue(){
		return value;
	}
}
