import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommException;
import lejos.pc.comm.NXTCommFactory;
import lejos.pc.comm.NXTInfo;

public class Map extends JPanel {
	private final class KeyboardEvent implements KeyListener {
		public void keyTyped(KeyEvent e) {
		}

		public void keyReleased(KeyEvent e) {
		}

		public void keyPressed(KeyEvent e) {
			char input = e.getKeyChar();
			if (input == '1') map.setVisual(0);
			else if (input == '2') map.setVisual(1);
			else if (input == '3') map.setVisual(2);
			else if (input == 'l') map.showLine();
			else if (input == 's') map.save();

			System.out.println(e.getKeyCode());
		    switch(e.getKeyCode()) { 
		        case KeyEvent.VK_UP:
		        		sendthread.send(SendThread.FORWARD);
		            break;
		        case KeyEvent.VK_DOWN:
		        		sendthread.send(SendThread.BACKWARD);
		            break;
		        case KeyEvent.VK_LEFT:
		        		sendthread.send(SendThread.LEFT);
		            break;
		        case KeyEvent.VK_RIGHT :
		        		sendthread.send(SendThread.RIGHT);
		            break;
		    }
		}
	}

	private ImageMap map;
	private SendThread sendthread;
	private DataOutputStream output;
	
	public Map (DataInputStream input, DataOutputStream output) throws IOException {
		JFrame frame = new JFrame("Mapa MAC0318");
		KeyboardEvent event = new KeyboardEvent();
		output = this.output;
		sendthread = new SendThread(output);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        this.map = new ImageMap();
		frame.add(this.map);
		frame.setSize(800, 800);
		frame.setVisible(true);
		
		frame.setFocusable(true);
		frame.requestFocusInWindow();
		
		frame.addKeyListener(event);
		String text = "1,2,3 - Change view mode.\n";
		text += "s - Save image.\n";
		text += "l - Show trace.\n";
		JOptionPane.showMessageDialog(null, text);
		
		int bytes_valiable = -1;
		
		while(true) {
			try {
				bytes_valiable = input.available();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if (bytes_valiable >= 0) {
				if (input.readByte() != '@') continue;
				int angle = input.readByte();
				float x = input.readFloat();
				int distance = input.readByte();
				float y = input.readFloat();
				System.out.println(x+" "+y);
			}
		}
	}
	
    public void addPoint(Point3D p) {
    		map.addPoint(p);
	}

	public static void main(String[] args) throws NXTCommException, IOException {
		NXTComm nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH); 
		NXTInfo[] nxtInfo = nxtComm.search("NXT8"); //find brick with NXT_ID by doing a Bluetooth inquiry
		if (nxtInfo.length == 0) { // failed to find a brick with the ID
			System.err.println("NO NXT found");
			System.exit(1);
		}
		if (!nxtComm.open(nxtInfo[0])) { // the brick was found but a connection could not be establish
			System.err.println("Failed to open NXT");
			System.exit(1);
		}
		
		DataInputStream input = new DataInputStream(nxtComm.getInputStream()); // open data input stream 
		DataOutputStream output = new DataOutputStream(nxtComm.getOutputStream()); // open data output stream
				
		Map map = new Map(null, null);

	}
}
