import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

public class LightCanvasJPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private static final int WIDTH = 320; // = 300 + padding * 2
	private static final int HEIGHT = 220; // = 200 + padding * 2
	private static final int LIGHT_RADIUS = 4;
	private static final int PADDING = 10;
	private double[][] lightsCoords;
	private double[] lights = null;
	
	public LightCanvasJPanel() { this(MusicPath.LIGHT_COORDS); }
	public LightCanvasJPanel(double[][] lightsCoords) {
		this.lightsCoords = lightsCoords;
		setPreferredSize(new Dimension(WIDTH, HEIGHT));
	}
	
	public void paintLights(double[] lights) {
		this.lights = lights;
		repaint();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		// background
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, WIDTH, HEIGHT);
		
		// lights
		if(lightsCoords == null) return;
		g.setColor(Color.BLACK);
		for(double[] light : lightsCoords) {
			if(light.length != 2)
				throw new MalformedCoordinates();
			int centerX = (int) (light[0] * (WIDTH - PADDING * 2)) + PADDING;
			int centerY = (int) (light[1] * (HEIGHT - PADDING * 2)) + PADDING;
			
			g.fillOval(centerX - LIGHT_RADIUS, centerY - LIGHT_RADIUS, LIGHT_RADIUS * 2, LIGHT_RADIUS * 2);
		}
		
		// lights values
		if(lights == null || lights.length != lightsCoords.length)
			return;
		g.setColor(Color.YELLOW);
		for(int i=0; i<lights.length; i++) {
			if(lightsCoords[i].length != 2)
				throw new MalformedCoordinates();
			int centerX = (int) (lightsCoords[i][0] * (WIDTH - PADDING * 2)) + PADDING;
			int centerY = (int) (lightsCoords[i][1] * (HEIGHT - PADDING * 2)) + PADDING;
			
			g.fillOval((int) (centerX - (LIGHT_RADIUS * lights[i])),
					(int) (centerY - (LIGHT_RADIUS * lights[i])),
					(int) (LIGHT_RADIUS * lights[i] * 2),
					(int) (LIGHT_RADIUS * lights[i] * 2));
		}
	}
}
