package agents;
import gameObjects.Player;
import knowledgeAndTechnics.BaseFunctions;
import knowledgeAndTechnics.Intelligence;
import knowledgeAndTechnics.NoFreePlayerException;

import communication.Communicator;
import communication.MessageParser;
import communication.Receiver;
import communication.Sender;
/*
 * Created on Sep 14, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

/**
 * @author tamer
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

public abstract class Agent {	
	protected BaseFunctions baseFunctions;
	protected Intelligence infoPool;
	protected MessageParser m_parser;
	//TODO: Reihenfolge, in der die Assoziationen aufgebaut werden, ueberpruefen (grafisch ?).
	public Agent(String[] args){
		Communicator com = new Communicator();
		if (args.length == 0) {
			System.out.println("usage: java Agentname teamname");
			System.exit(1);
		}
		infoPool = new Intelligence(args[0]);
		Sender sender;
		sender = new Sender(com);
		baseFunctions = new BaseFunctions(sender, infoPool);
		infoPool.setBaseFunctions(baseFunctions);
		m_parser = new MessageParser(infoPool, baseFunctions);
		baseFunctions.setMessageParser(m_parser);
		new Receiver(com, m_parser).start();
	}
	
	
	public abstract void playerInitialization();
	
	public void start(){
		playerInitialization();
		decideAction();
	}
	
	public void handleBeforeKickOff(){
		baseFunctions.move(infoPool.getXPlayerStart(),infoPool.getYPlayerStart());
		/*Fuer den Anstosstypen
		 * if (infoPool.getPlayDirection() == 1)
			sender.move(infoPool.getXPlayerHome(),infoPool.getYPlayerHome());
		else sender.move(-0.3, 0);*/
		
	}
	
	public void handlePlayOn(){
	}
	public void handleTimeOver(){
		infoPool.setPlayerAlive(false);
	}
	
	public void handleKickOffWe(){
		baseFunctions.move(infoPool.getXPlayerStart(),infoPool.getYPlayerStart());
//		Hier braucht man 2 Spieler, die den Anstoss wagen
	}
	
	public void handleKickOffOpponent(){
		baseFunctions.move(infoPool.getXPlayerStart(),infoPool.getYPlayerStart());
	}
	
	public void handleFreeKickWe(){
		if (infoPool.checkBallInFreeKickField() == true){
			baseFunctions.turnNeckWithoutWaiting(0);
			baseFunctions.changeViewWithoutWaiting("normal","high");
			infoPool.setBallPossession(Intelligence.BALL_POSSESSION_ME);
			infoPool.runToBall(100,200);
			try{
			    infoPool.resetLastKickTime();
			    Player freePlayer = infoPool.findFreePlayer();
			    infoPool.resetLastKickTime();
			    infoPool.passToFreePlayer(freePlayer);
			    System.out.println(infoPool.getSenseBodyTime()+"will passen"+ freePlayer);
			} catch (NoFreePlayerException e){
				System.out.println(infoPool.getSenseBodyTime()+"will kicken");
			    infoPool.kickToGoal(); 
			}
		}
	}
	
	public void handleFreeKickOpponent(){
//		Nur auf der Lauer sein und warmhalten ;-)
		//infoPool.setPlayerAlive(false);
	}
	
	public void handleKickInWe(){
	    if (infoPool.checkBallInFreeKickField() == true){
			baseFunctions.turnNeckWithoutWaiting(0);
			baseFunctions.changeViewWithoutWaiting("normal","high");
			infoPool.setBallPossession(Intelligence.BALL_POSSESSION_ME);
			infoPool.runToBall(100,200);
			try{
			    infoPool.resetLastKickTime();
			    Player freePlayer = infoPool.findFreePlayer();
			    infoPool.resetLastKickTime();
			    infoPool.passToFreePlayer(freePlayer);
			    System.out.println(infoPool.getSenseBodyTime()+"will passen"+ freePlayer);
			} catch (NoFreePlayerException e){
				System.out.println(infoPool.getSenseBodyTime()+"will kicken");
			    infoPool.kickToGoal(); 
			}
		}
	}
	public void handleKickInOpponent(){
	
	}
	
	public void handleCornerKickWe(){
	    if (infoPool.checkBallInFreeKickField() == true){
			baseFunctions.turnNeckWithoutWaiting(0);
			baseFunctions.changeViewWithoutWaiting("normal","high");
			infoPool.setBallPossession(Intelligence.BALL_POSSESSION_ME);
			infoPool.runToBall(100,200);
			try{
			    infoPool.resetLastKickTime();
			    Player freePlayer = infoPool.findFreePlayer();
			    infoPool.resetLastKickTime();
			    infoPool.passToFreePlayer(freePlayer);
			    System.out.println(infoPool.getSenseBodyTime()+"will passen"+ freePlayer);
			} catch (NoFreePlayerException e){
				System.out.println(infoPool.getSenseBodyTime()+"will kicken");
			    infoPool.kickToGoal(); 
			}
		}
	}
	
	public void handleCornerKickOpponent(){
		
	}
	
	public void handleGoalWeN(){
		baseFunctions.move(infoPool.getXPlayerStart(),infoPool.getYPlayerStart());
	}
	
	public void handleGoalOpponentN(){
		baseFunctions.move(infoPool.getXPlayerStart(),infoPool.getYPlayerStart());
	}
	
	public void handleGoalKickWe(){
		baseFunctions.turnCorrect(infoPool.angleToObject(-52.5,0));
	}
	
	public void handleGoalKickOpponent(){
		baseFunctions.move(infoPool.getXPlayerStart(),infoPool.getYPlayerStart());
	}
	
	public void handleGoalieCatchBallWe(){		
	}
	public void handleGoalieCatchBallOpponent(){	
	}

	private void handleGoalOpponent() {
		baseFunctions.move(infoPool.getXPlayerStart(),infoPool.getYPlayerStart());
	}

	private void handleGoalWe() {
		baseFunctions.move(infoPool.getXPlayerStart(),infoPool.getYPlayerStart());
	}	

	
	public void decideAction(){
		while (infoPool.isPlayerAlive()){
			int playMode = infoPool.getPlayMode();
			infoPool.setExpectedPlayMode(playMode);
			//System.out.println(infoPool.getSenseBodyTime()+ " Playmode "+ playMode);
			// Hier der Teil für die Playmodes
				
			switch (playMode){
				case Intelligence.BEFORE_KICK_OFF: 
					handleBeforeKickOff();
					break;
					
				case Intelligence.PLAY_ON: 
					handlePlayOn();
					break;
				
				case Intelligence.TIME_OVER:
					handleTimeOver();
					break;
					
				case Intelligence.KICK_OFF_WE:
					handleKickOffWe();
					break;
				
				case Intelligence.KICK_OFF_OPPONENT:
					handleKickOffOpponent();
					break;
				
				case Intelligence.FREE_KICK_WE:
					handleFreeKickWe();	
					break;
				
				case Intelligence.FREE_KICK_OPPONENT:
					handleFreeKickOpponent();
					break;
				
				case Intelligence.KICK_IN_WE:
					handleKickInWe();	
					break;
				
				case Intelligence.KICK_IN_OPPONENT:
					handleKickInOpponent();
					break;
				
				case Intelligence.CORNER_KICK_WE:
					handleCornerKickWe();
					break;	
				
				case Intelligence.CORNER_KICK_OPPONENT:
					handleCornerKickOpponent();
					break;
				
				case Intelligence.GOAL_KICK_WE:
					handleGoalKickWe();
					break;
				
				case Intelligence.GOAL_KICK_OPPONENT:
					handleGoalKickOpponent();
					break;
				
				case Intelligence.GOAL_WE_N:
					handleGoalWeN();
					break;
				
				case Intelligence.GOAL_OPPONENT_N:
					handleGoalOpponentN();
					break;
				
				case Intelligence.GOAL_WE:
					handleGoalWe();
					break;
				
				case Intelligence.GOAL_OPPONENT:
					handleGoalOpponent();
					break;
				
					
				case Intelligence.GOALIE_CATCH_BALL_WE:
					handleGoalieCatchBallWe();
					break;
				
				case Intelligence.GOALIE_CATCH_BALL_OPPONENT:
					handleGoalieCatchBallOpponent();
					break;
				
			}
			Thread.yield();
		}
		baseFunctions.bye();
		System.exit(0);
	}	
	
}