package org.mote.wiimote.whiteboard.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsDevice;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JPanel;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.mote.wiimote.whiteboard.WiimoteDataHandler;
import org.mote.wiimote.whiteboard.WiimoteWhiteboard;
import org.mote.wiimote.whiteboard.WiimoteDataHandler.WiimoteDataListener;
import org.mote.wiimote.whiteboard.calibration.WiimoteCalibration;
import org.mote.wiimote.whiteboard.ds.IRDot;
import org.mote.wiimote.whiteboard.ds.Wiimote;
import org.mote.wiimote.whiteboard.preferences.WWPreferences;
import org.mote.wiimote.whiteboard.util.Util;

@SuppressWarnings("serial")
public class WarpedMonitor extends JDialog implements WiimoteDataListener {

	private static final int REPAINT_FREQ = 1000 / 50;
	static final int RADIUS = 10;

	private JPanel canvas;
	private Point2D[][] lights = new Point2D[WWPreferences.WIIMOTES+1][4];
	private LightLabel[][] labels = new LightLabel[WWPreferences.WIIMOTES+1][4];
	private WiimoteCalibration calibration;
	private WiimoteDataHandler dh;

	public WarpedMonitor(WiimoteDataHandler dh, final WiimoteCalibration calibration) {
		super(Application.getInstance(WiimoteWhiteboard.class).getMainFrame(), "Warped Point Monitor");
		setLayout(new BorderLayout());
		dh.addWiimoteDataListener(this);
		this.calibration = calibration;
		this.dh = dh;
		((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

		canvas = new JPanel(null, true);
		canvas.setBorder(BorderFactory.createLineBorder(Color.black));
		add(canvas, BorderLayout.CENTER);
		
		setUndecorated(true);
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_F || e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					GraphicsDevice screen = calibration.getScreen();
					if (screen != null) {
						if (screen.getFullScreenWindow() == WarpedMonitor.this) {
							((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
							screen.setFullScreenWindow(null);
							Util.placeDialogWindow(WarpedMonitor.this, 640, 480);
						} else {
							if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
								WarpedMonitor.this.setVisible(false);
							else {
								((JPanel) getContentPane()).setBorder(BorderFactory.createEmptyBorder());
								screen.setFullScreenWindow(WarpedMonitor.this);
							}
						}
					}
				}
			}
		});

		Util.placeDialogWindow(this, 640, 480);
		new Timer(true).schedule(new UpdateTask(), 0, REPAINT_FREQ);
	}

	@Action
	public void monitor() {
		setVisible(true);
	}

	private class UpdateTask extends TimerTask {
		@Override
		public void run() {
			if (isVisible()) {
				for (int i = WWPreferences.WIIMOTES; i >= 0; i--)
					for (int j = 0; j < 4; j++) {
						if (labels[i][j] != null)
							labels[i][j].update();
					}
			}
		}
	}

	public void irLights(Wiimote wiimote, IRDot[] lights) {
	}
	
	public void irWarped(Map<Wiimote, IRDot[]> data, Point[] warped) {
		if (isVisible()) {
			Rectangle bounds = calibration.getScreen().getDefaultConfiguration().getBounds();
			for (int i = 0; i < 4; i++) {
				final Point w = warped[i];
				this.lights[0][i] = w == null ? null : new Point2D.Double(w.getX() / bounds.getWidth(), 1 - w.getY() / bounds.getHeight());
			}
			for (Wiimote wiimote : dh.getConnectedWiimotes()) {
				for (int i = 0; i < 4; i++) {
					final Point2D w = calibration.warp(i, wiimote, data);
					this.lights[wiimote.getId()][i] = w == null ? null : new Point2D.Double(w.getX() / bounds.getWidth(), 1 - w.getY() / bounds.getHeight());
				}
			}
		}
	}

	public void batteryLevel(Wiimote wiimote, double level) {
	}

	public void wiimoteConnected(Wiimote wiimote) {
		if (wiimote.getId() == 1)
			for (int i = 0; i < 4; i++)
				canvas.add(labels[0][i] = new LightLabel(canvas, lights, 0, i + 1, Color.red));
		for (int i = 0; i < 4; i++)
			canvas.add(labels[wiimote.getId()][i] = new LightLabel(canvas, lights, wiimote.getId(), i + 1));
	}

	public void wiimoteDisconnected(Wiimote wiimote) {
		for (int i = 0; i < 4; i++) {
			canvas.remove(labels[wiimote.getId()][i]);
			labels[wiimote.getId()][i] = null;
		}
	}

}
