package agents;
import gameObjects.Player;
import other.BaseFunctions;
import other.Communicator;
import other.InformationPool;
import other.MessageParser;
import other.NoFreePlayerException;
import other.Receiver;
import other.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 InformationPool 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 InformationPool(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){
			try{
			    Player freePlayer = infoPool.findFreePlayer();
			    infoPool.passToFreePlayer(freePlayer);
			} catch (NoFreePlayerException e){
			    //TODO:NoFreePlayerException behandeln! 
			}
		}
	}
	
	public void handleFreeKickOpponent(){
//		Nur auf der Lauer sein und warmhalten ;-)
		//infoPool.setPlayerAlive(false);
	}
	
	public void handleKickInWe(){
		if (infoPool.checkBallInFreeKickField() == true){
		    try{
		        Player freePlayer = infoPool.findFreePlayer();
		        infoPool.passToFreePlayer(freePlayer);
			} catch (NoFreePlayerException e){
			    //TODO:NoFreePlayerException behandeln! 
			}					
		}
	}
	public void handleKickInOpponent(){
	
	}
	
	public void handleCornerKickWe(){
		if (infoPool.checkBallInFreeKickField() == true){
		    try{
		        Player freePlayer = infoPool.findFreePlayer();
		        infoPool.passToFreePlayer(freePlayer);
			} catch (NoFreePlayerException e){
			    //TODO:NoFreePlayerException behandeln! 
			}					
		}
	}
	
	public void handleCornerKickOpponent(){
	}
	
	public void handleGoalKickWe(){
	}
	
	public void handleGoalKickOpponent(){
	}
	
	public void handleGoalieCatchBallWe(){		
	}
	public void handleGoalieCatchBallOpponent(){	
	}
	
	public void decideAction(){
		while (infoPool.isPlayerAlive()){
			int playMode = infoPool.getPlayMode();
			infoPool.setExpectedPlayMode(playMode);

			// Hier der Teil für die Playmodes
				
			switch (playMode){
				case InformationPool.BEFORE_KICK_OFF: 
					handleBeforeKickOff();
					break;
					
				case InformationPool.PLAY_ON: 
					handlePlayOn();
					break;
				
				case InformationPool.TIME_OVER:
					handleTimeOver();
					break;
					
				case InformationPool.KICK_OFF_WE:
					handleKickOffWe();
					break;
				
				case InformationPool.KICK_OFF_OPPONENT:
					handleKickOffOpponent();
					break;
				
				case InformationPool.FREE_KICK_WE:
					handleFreeKickWe();	
					break;
				
				case InformationPool.FREE_KICK_OPPONENT:
					handleFreeKickOpponent();
					break;
				
				case InformationPool.KICK_IN_WE:
					handleKickInWe();	
					break;
				
				case InformationPool.KICK_IN_OPPONENT:
					handleKickInOpponent();
					break;
				
				case InformationPool.CORNER_KICK_WE:
					handleCornerKickWe();
					break;	
				
				case InformationPool.CORNER_KICK_OPPONENT:
					handleCornerKickOpponent();
					break;
				
				case InformationPool.GOAL_KICK_WE:
					handleGoalKickWe();
					break;
				
				case InformationPool.GOAL_KICK_OPPONENT:
					handleGoalKickOpponent();
					break;
				
				case InformationPool.GOALIE_CATCH_BALL_WE:
					handleGoalieCatchBallWe();
					break;
				
				case InformationPool.GOALIE_CATCH_BALL_OPPONENT:
					handleGoalieCatchBallOpponent();
					break;
				
			}
			Thread.yield();
		}
		baseFunctions.bye();
		System.exit(0);
	}	
	
}