package com.madeorsk.smartnotes;

import com.madeorsk.smartnotes.notes.Note;
import com.madeorsk.smartnotes.paths.FolderPath;

import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;

public class ExplorerItem extends HBox
{
	private boolean folder;
	private Object item;

	public ExplorerItem(boolean folder, Object item, final NotesExplorer explorer)
	{
		this.folder = folder;
		this.item = item;

		this.setCursor(Cursor.HAND);
		this.setOnMouseClicked((MouseEvent event) -> {
			if (ExplorerItem.this.folder)
				explorer.goToFolder((FolderPath) ExplorerItem.this.item);
			else
			{
				explorer.requestLoad((Note) ExplorerItem.this.item);
				SmartNotes.instance.setContent(((Note) ExplorerItem.this.item).getViewBox(explorer));
			}
		});

		this.setAlignment(Pos.CENTER_LEFT);
		this.setSpacing(10);

		if (this.folder)
		{
			FolderPath folderPath = (FolderPath) this.item;
			this.getChildren().add(new ImageView(new Image("/com/madeorsk/smartnotes/res/FolderIcon.png")));
			Label folderNameLabel = new Label(folderPath.getName());
			folderNameLabel.setId("noteItemName");
			this.getChildren().add(folderNameLabel);
		}
		else
		{
			Note note = (Note) this.item;
			this.getChildren().add(new ImageView(new Image("/com/madeorsk/smartnotes/res/" + note.getClass().getSimpleName() + "Icon-" + note.getNoteColor().name() + ".png")));
			Label noteNameLabel = new Label(note.getName());
			noteNameLabel.setId("noteItemName");
			this.getChildren().add(noteNameLabel);
		}
	}

	public ExplorerItem setFolderIcon(String path)
	{
		this.getChildren().clear();

		FolderPath folderPath = (FolderPath) this.item;
		this.getChildren().add(new ImageView(new Image(path)));
		Label folderNameLabel = new Label(folderPath.getName());
		folderNameLabel.setId("noteItemName");
		this.getChildren().add(folderNameLabel);

		return this;
	}
}
