package com.madeorsk.smartnotes;

import java.util.ArrayList;
import java.util.List;

import com.madeorsk.smartnotes.notes.Note.NoteColor;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Cursor;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;

public class NoteColorSelector extends HBox
{
	private List<NoteColor> noteColors = new ArrayList<NoteColor>();
	private ObjectProperty<NoteColor> selectedColor = new SimpleObjectProperty<NoteColor>();

	public NoteColorSelector()
	{
		this.setSpacing(10);
	}

	public void addColor(NoteColor color)
	{
		this.noteColors.add(color);
		if (this.noteColors.size() == 1)
			this.setSelectedColor(color);
		else
			this.updateBox();
	}

	public void setSelectedColor(NoteColor color)
	{
		this.selectedColor.set(color);
		this.updateBox();
	}

	public NoteColor getSelectedColor()
	{
		return this.selectedColor.getValue();
	}

	public ObservableValue<NoteColor> selectedColorProperty()
	{
		return this.selectedColor;
	}

	private void updateBox()
	{
		this.getChildren().clear();
		for (final NoteColor color : this.noteColors)
		{
			Canvas canvas = new Canvas();
			canvas.setCursor(Cursor.HAND);
			canvas.setHeight(50);
			canvas.setWidth(50);
			GraphicsContext gc = canvas.getGraphicsContext2D();
			if (color.equals(this.getSelectedColor()))
			{
				gc.setFill(new Color(color.getColor().getRed(), color.getColor().getGreen(), color.getColor().getBlue(), 0.5));
				gc.fillOval(0, 0, 50, 50);
			}
			gc.setFill(color.getColor());
			gc.fillOval(3, 3, 44, 44);

			canvas.setOnMouseClicked((MouseEvent event) -> {
				NoteColorSelector.this.setSelectedColor(color);
			});

			this.getChildren().add(canvas);
		}
	}
}
