import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.File;

/**
 * @author Dan Maynes-Aminzade <monzy@mit.edu>
 */


public class SoccerClient {

	Frame myFrame; //the window to draw in
	Image picture;
	BufferedImage buffer; // a image buffer to draw onto
	Graphics2D graphics; // a drawing handle
	UDPClient client;
	Datagram data;
	int width = 700;
	int height = 480;
	int currentplayer = 0;
	static String hostname = "localhost";

	/** Creates a new instance of SoccerClient */
	public SoccerClient() {
		client = new UDPClient(hostname);
		data = new Datagram();
		myFrame = new Frame("Soccer Client"){
			public void paint(Graphics g){
				if(buffer != null)
					g.drawImage(buffer,0,0,null);// redraws image when it needs to be repainted
			}
		};
		myFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent evt){
				//window closing handler
				System.exit(0);
			}
		});
		myFrame.addMouseListener(new MouseAdapter(){
			//overload methods to handle mouse events
		});
		myFrame.addMouseMotionListener(new MouseMotionAdapter(){
			//overload methods handle mouse events;
		});
		myFrame.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e) {
				switch(e.getKeyCode()) {
					case KeyEvent.VK_1: currentplayer = 0; break;
					case KeyEvent.VK_2: currentplayer = 1; break;
					case KeyEvent.VK_3: currentplayer = 2; break;
					case KeyEvent.VK_4: currentplayer = 3; break;
					case KeyEvent.VK_5: currentplayer = 4; break;
					case KeyEvent.VK_UP:
						data.players[currentplayer].vy -= 1.0;
						break;
					case KeyEvent.VK_DOWN:
						data.players[currentplayer].vy += 1.0;
						break;
					case KeyEvent.VK_LEFT:
						data.players[currentplayer].vx -= 1.0;
						break;
					case KeyEvent.VK_RIGHT:
						data.players[currentplayer].vx += 1.0;
						break;
					case KeyEvent.VK_SPACE:
						data.players[currentplayer].kick = 3.0f;
						break;
				}
				client.SendPacket(data);
				for (int i=0; i<5; i++) data.players[i].kick = 0.0f;
			}
		});
		myFrame.setSize(width,height);
		buffer = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		graphics = buffer.createGraphics();
		graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//if you want anti-aliasing
        
        try{ picture = loadImage("background.jpg");}
        catch(Exception x){
            x.printStackTrace();
            System.exit(1);
        }
        
        myFrame.show();

        draw();
    }
    
    public void draw(){
        graphics.clearRect(0,0,width,height);//background is black by default
        graphics.drawImage(picture,30,30,null);
        myFrame.getGraphics().drawImage(buffer,0,0,null);
    }
    
    /** loads the image data from a local file (JPEG, GIF, PNG)
     *@param path full path for the file
     */
    public Image loadImage(String path) throws Exception{
        File f = new File(path); 
        MediaTracker tracker;
        if(!f.exists())
            throw new Exception("File not found" );

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image image = toolkit.createImage(path);
        tracker = new MediaTracker(myFrame);
        tracker.addImage(image, 0);
        tracker.waitForID(0);
        return image;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
	if (args.length < 1) {
		System.out.println("Usage: java SoccerClient [hostname]");
		return;	
	}
		hostname = args[0];
        new SoccerClient();
    }
    
}
