import { Backend, TaskData, Event } from "Backend.slint";
import { ScrollView } from "std-widgets.slint";
import { SideBar } from "SideBar.slint";
import { VCheckBox, VButton, VTag, VPopupIconMenu, VText, Palette } from "@vynui";
import { TaskLine } from "TaskLine.slint";
import { Utils } from "Utils.slint";

export component EventGroup {
	in property<Event> event;

	eventPopup := VPopupIconMenu {
		VButton {
			icon-source: @image-url("./images/add.png");
			icon-colorize: Colors.greenyellow;
			icon-size: 1.5rem;
			border-radius: 0;
			clicked => { Backend.open_new_task_form({
				eventSourceId: event.sourceId,
				eventId: event.id,
			})}
		}
		VButton {
			icon-source: @image-url("./images/edit.png");
			icon-colorize: Colors.grey;
			icon-size: 1.5rem;
			border-radius: 0;
			clicked => { Backend.open_edit_event_form(event.sourceId, event.id) }
		}

		VButton {
			icon-source: @image-url("./images/delete.png");
			icon-colorize: Colors.pink;
			icon-size: 1.5rem;
			border-radius: 0;
			clicked => { Backend.delete_event_clicked(event.sourceId, event.id) }
		}
	}

	ta := TouchArea {
		pointer-event(e) => { 
			if (e.button == PointerEventButton.right && e.kind == PointerEventKind.up) {
				eventPopup.show(ta.mouse-x, ta.mouse-y); 
			}
		}
	}

	HorizontalLayout {
		VerticalLayout {
			spacing: 4px;
			VText {
				text: Utils.timeToString(event.startsAt);
				color: Palette.accent;
			}
			HorizontalLayout {
				alignment: center;
				Rectangle {
					width: 4px;
					background: Palette.accent;
					border-radius: 8px;

				}
			}
			VText {
				text: Utils.timeToString(event.endsAt);
				color: Palette.accent;
			}
		}
		VerticalLayout {
			horizontal-stretch: 1;
			padding: 16px;
			padding-top: 32px;
			padding-bottom: 32px;
			padding-right: 0px;
			VText {
				text: event.title;
				font-size: 1.1rem;
			}
			for task[taskIndex] in event.tasks: VerticalLayout {
				padding-top: taskIndex == 0 ? 16px : 0px;
				padding-bottom: 8px;
				TaskLine {
					task: task;
					source-index: task.sourceId;
					task-index: task.id;
				}
			}
		}
	}

}
