package org.mote.wiimote.whiteboard.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.uweschmidt.wiimote.whiteboard.ds.IRDot;

@SuppressWarnings("serial")
public class LightLabel extends JLabel {
	
	private static final int RADIUS = 10;
	
	private final int id, number;
	private Point2D[][] lights;
	private JPanel canvas;

	public LightLabel(JPanel canvas, Point2D[][] lights, int id, int number) {
		this(canvas, lights, id, number, WiimoteIcon.COLORS[id - 1]);
	}

	public LightLabel(JPanel canvas, Point2D[][] lights, int id, int number, Color bg) {
		super(String.valueOf(number));
		this.id = id;
		this.number = number;
		this.lights = lights;
		this.canvas = canvas;
		setHorizontalAlignment(SwingConstants.CENTER);
		setForeground(Color.black);
		setBackground(bg);
	}

	public void update() {
		Point2D l = lights[id][number - 1];
		if (l != null) {
			int x = (int) Math.round(l.getX() * canvas.getWidth());
			int y = canvas.getHeight() - (int) Math.round(l.getY() * canvas.getHeight());
			this.setBounds(x - RADIUS, y - RADIUS, RADIUS * 2, RADIUS * 2);
			setVisible(true);
		} else {
			setVisible(false);
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		if (lights[id][number - 1] != null) {
			g2d.setColor(this.getBackground());
			g2d.fillOval(1, 1, RADIUS * 2 - 2, RADIUS * 2 - 2);
			g2d.setColor(this.getForeground());
			g2d.drawOval(1, 1, RADIUS * 2 - 2, RADIUS * 2 - 2);

			double scale = lights[id][number - 1] instanceof IRDot ? ((IRDot)lights[id][number - 1]).getSize() : -1;
			if (scale != -1) {
				g2d.setColor(Color.cyan);
				final int d = (int)Math.round(RADIUS * scale * 10);
				g2d.fillOval(RADIUS - d/2, RADIUS - d/2, d, d);
			}
		}
		super.paintComponent(g);
	}
}
