package agents;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;



class Coach
{
	private DatagramSocket	 coach_socket;
	private	InetAddress	 coach_host;
	private int		 coach_port;
	private static final int MSG_SIZE = 1024;

	public static void main(String a[])
        throws SocketException, IOException
	{
		String	hostName = new String("localhost");
		int	port = 6001;

		try
		{
			for( int c = 0 ; c < a.length ; c += 2 )
			{
				if( a[c].compareTo("-h") == 0 )
					hostName = a[c+1];
				if( a[c].compareTo("-p") == 0 )
					port = Integer.parseInt(a[c+1]);
				else
					throw new Exception();
			} 
		}
		catch(Exception e)
		{
			return;
		}
 
		Coach coach = new Coach(InetAddress.getByName(hostName),
						port);
		coach.mainLoop();						
	}

	public Coach(InetAddress host, int port)
		throws SocketException
	{
		coach_socket = new DatagramSocket();
		coach_host = host;
		coach_port = port;
	}
 	
	public void finalize()
	{
		coach_socket.close();
	}

	protected void mainLoop() throws IOException
	{ 

		byte[] buffer = new byte[MSG_SIZE];

		DatagramPacket packet = new DatagramPacket(buffer, MSG_SIZE);

		while( true )
		{
		    System.out.flush();
		    select();
		}
	}

	public void select() throws IOException
	{
		String team = "",message = "";
		int number;
		double x,y,dir;
		DataInputStream in = new DataInputStream(System.in);
		System.out.println();
		System.out.println("move ball       -> 1");
		System.out.println("check ball      -> 2");
		System.out.println("look            -> 3");
		System.out.println("move player     -> 4");
		System.out.println("before kick off -> 5");
		System.out.println("kick off        -> 6");
		System.out.println("play on         -> 7");
		System.out.println("kick in         -> 8");
		System.out.println("say             -> 9");
		System.out.println("Please input number!");
		System.out.print  ("                -> ");

		int i = in_int();

		if(i == 1)
		    {
		        System.out.print("   Please input   X   =  ");
		        x = in_double();
		        System.out.print("   Please input   Y   =  ");
		        y = in_double();
		        System.out.print("   Please input Angle =  ");
		        dir = in_double();
		        move_ball(x, y, dir);
		    }
		else if(i == 2)
		    {
			check_ball();			    
		    }
		else if(i == 3)
		    {
		        look();
		    }
		else if(i == 4)
		    {
		       System.out.print("   Please input team  =  ");
		       team = in_str();
		       System.out.print("   Please input number=  ");
		       number = in_int();
		       System.out.print("   Please input   X   =  ");
		       x = in_double();
		       System.out.print("   Please input   Y   =  ");
		       y = in_double();
		       System.out.print("   Please input Angle =  ");
		       dir = in_double();
		       move_player(team,number,x, y, dir);
		    }
                else if(i == 5)
                    {
                        b_kick_off();
                    }
                else if(i == 6)
                    {
                        kick_off();
                    }
                else if(i == 7)
                    {
                        play_on();
                    }
                else if(i == 8)
                    {
                        kick_in();
                    }
                else if(i == 9)
                    {
                        System.out.print("Message -> ");
                        message = in_str();
                        say(message);
                    }
		else
		    {
			System.out.println("input miss!");
		    }
	}

	/**
	 * ?R?}???h?W
	 */
	private void move_ball(double x, double y, double dir) 
	{ 
		send("(move (ball) " + x + " " + y + " " + dir + ")\n"); 
		parseSensorInformation(receive());
	} 

	private void check_ball() 
	{ 
		send("(check_ball)\n"); 
		parseSensorInformation(receive());
	} 

	private void look()
	{
		send("(look)\n");
		parseSensorInformation(receive());
	}

	private void move_player(String t, int n, double x, double y, double dir)
	{
		send("(move (player " + t +" " + n + ") " + x + " " + y + " " + dir + ")\n"); 
		parseSensorInformation(receive());
	}

	private void parseSensorInformation(String message) 
	{
		System.out.println(message);
	}


	private void b_kick_off()
	{
		send("(change_mode before_kick_off)\n");
		parseSensorInformation(receive());
	}

        private void kick_off()
        {
                send("(change_mode kick_off)\n");
        }

        private void play_on()
        {
                send("(change_mode play_on)\n");
        }
   

        private void kick_in()
        {
                send("(change_mode kick_in)\n");
        }

        private void say(String m)
        {
                send("(say " + m +")\n");
                parseSensorInformation(receive());
        }


    /**  
     * double ?W??????
     * 
     * @exception IOException 
     */
     public double in_double() throws IOException
        {
	    BufferedReader in =
		   new BufferedReader(new InputStreamReader(System.in));
	    double d = 0.0;

	    String line = in.readLine();
	    // System.out.println(line);

	    d = Double.parseDouble(line);
              
	    return d;
        }

    /**  
     * int ?W??????
     * 
     * @exception IOException 
     */
     public int in_int() throws IOException
        {
	    BufferedReader in =
		   new BufferedReader(new InputStreamReader(System.in));
	    int i;

	    String line = in.readLine();
	 // System.out.println(line);

	    i = Integer.parseInt(line);
              
	    return i;
        }

    /**
     * String ?W??????
     * 
     * @exception IOException 
     */
     public String in_str() throws IOException
        {
	    BufferedReader in =
		   new BufferedReader(new InputStreamReader(System.in));
	    String s = "";

	    String line = in.readLine();
	 // System.out.println(line);

	    s = line;
              
	    return s;
        }


	/**
	 * ?R?}???h??????
	 */
	private void send(String message) 
	{ 
		byte[] buffer = new byte[MSG_SIZE]; 

		buffer = message.getBytes();

		/* ?f?[?^?O?????p?P?b?g?????? 
		 */
 		DatagramPacket packet = new DatagramPacket(buffer, 
					buffer.length, coach_host,
							   coach_port); 
		try
		{ 
			coach_socket.send(packet); 
		}
		catch(IOException e)
		{ 
			System.err.println("socket sending error " + e); 
		} 
	} 

	/**
	 * ???V?[?u????
	 */
	private String receive()  
	{ 
		byte[] buffer = new byte[MSG_SIZE]; 
		DatagramPacket packet = new DatagramPacket(buffer, MSG_SIZE); 
		try
		{ 
			coach_socket.receive(packet); 
		}
		catch(IOException e)
		{ 
			System.err.println("socket receiving error " + e); 
		} 
		return new String(buffer);
	}
}