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

export component MainView inherits Rectangle {

	background: Palette.background;
	private property<image> icon-visible: @image-url("./images/visible.png");
	private property<image> icon-not-visible: @image-url("./images/not-visible.png");
	private property<bool> completed-tasks-visible: false;

	pure function formatZeroPadding(number: int) -> string {
		if (number < 10) {
			return "0\{number}";
		}
		return number;
	}

	VerticalLayout {
		horizontal-stretch: 1;
		padding: 16px;
		spacing: 16px;
		alignment: start;
		HorizontalLayout {
			horizontal-stretch: 1;
			alignment: start;
			spacing: 8px;
			VButton {
				text: "New task";
				clicked => { Backend.open_new_task_form({
					eventSourceId: -1,
					eventId: -1,
				})}
				icon-source: @image-url("./images/add.png");
				icon-colorize: Colors.greenyellow;
			}
			VButton {
				text: "New event";
				clicked => { Backend.open_new_event_form() }
				icon-source: @image-url("./images/add.png");
				icon-colorize: Colors.greenyellow;
			}
			VButton {
				text: "Show/Hide completed tasks";
				clicked => {
					Backend.toggle_show_completed_tasks();
					completed-tasks-visible = !completed-tasks-visible;
				}
				icon-source: completed-tasks-visible ? icon-visible : icon-not-visible;
				icon-colorize: Palette.control-foreground;
			}
		}
		Rectangle {
			horizontal-stretch: 1;
			background: Palette.background.brighter(0.2);
			height: 1px;
		}
		Flickable {
			horizontal-stretch: 1;
			VerticalLayout {
				alignment: start;
				spacing: 16px;
				if Backend.visible_tasks.length == 0 : VText {
					text: "There is no task to show";
					horizontal-alignment: center;
				}
				for day[dayIndex] in Backend.visible_tasks: VerticalLayout {
					Rectangle {
						background: Palette.card-background;
						border-radius: 8px;
						VerticalLayout {
							padding: 16px;
							HorizontalLayout {
								alignment: start;
								VText {
									text: Backend.formatDate(day.date);
									color: day.isLate ? Palette.orange : Palette.foreground;
									font-size: 1.2rem;
								}
								if day.isToday : VerticalLayout {
									alignment: center;
									VText {
										text: " - Today";
										color: Palette.foreground-hint;
										font-size: 1rem;
									}
								}
							}
							for event[eventIndex] in day.events: VerticalLayout {
								padding-top: 16px;
								EventGroup {
									event: event;
								}
							}
							for task[taskIndex] in day.tasks: VerticalLayout {
								padding-top: taskIndex == 0 ? 16px : 0px;
								padding-bottom: 8px;
								TaskLine {
									task: task;
									source-index: task.sourceId;
									task-index: task.id;
								}
							}
						}
					}
				}
			}
		}
	}
}
