package main

import (
	"time"

	"tinygo.org/x/tinydraw"
	"tinygo.org/x/tinyfont"
	"tinygo.org/x/tinyfont/freesans"
	"tinygo.org/x/tinyfont/proggy"
)

func schedule(day int, hour int) {
	quit := false
	for {
		scheduleShowDay(day, hour)
		for {
			time.Sleep(200 * time.Millisecond)
			getInput()
			if !buttonsOldState[buttonDown] && buttonsState[buttonDown] {
				hour++
				if hour > len(scheduleData[day].talks)-4 {
					hour = len(scheduleData[day].talks) - 4
				} else {
					break
				}
			}
			if !buttonsOldState[buttonUp] && buttonsState[buttonUp] {
				hour--
				if hour < 0 {
					hour = 0
				} else {
					break
				}
			}
			if !buttonsOldState[buttonLeft] && buttonsState[buttonLeft] {
				day--
				if day < 0 {
					day = 0
				} else {
					hour = 0
					break
				}
			}
			if !buttonsOldState[buttonRight] && buttonsState[buttonRight] {
				day++
				if day > len(scheduleData)-1 {
					day = len(scheduleData) - 1
				} else {
					hour = 0
					break
				}
			}
			if goBack() {
				quit = true
				break
			}
		}
		if quit {
			break
		}
	}
}

func scheduleShowDay(day int, hour int) {
	display.FillScreen(colorWhite)

	tinydraw.Line(&display, 0, 76, displayWidth, 76, colorBlack)
	tinydraw.Line(&display, 0, 126, displayWidth, 126, colorBlack)
	tinydraw.Line(&display, 0, 176, displayWidth, 176, colorBlack)
	tinydraw.Line(&display, 50, 0, 50, displayHeight, colorBlack)

	display.FillRectangle(0, 0, displayWidth, 26, colorBlack)
	tinyfont.WriteLine(&display, &freesans.Bold9pt7b, 8, 18, scheduleData[day].title, colorWhite)

	display.FillRectangle(0, displayHeight-14, displayWidth, 14, colorBlack)
	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 0, displayHeight-4, " [arrows] TO NAVIGATE                     [buttonB] TO EXIT", colorWhite)

	tinyfont.WriteLine(&display, &freesans.Regular9pt7b, 2, 46, scheduleData[day].talks[hour].startHour, colorBlack)
	tinyfont.WriteLine(&display, &freesans.Regular9pt7b, 2, 69, scheduleData[day].talks[hour].endHour, colorBlack)

	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 40, scheduleData[day].talks[hour].line1, colorBlack)
	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 53, scheduleData[day].talks[hour].line2, colorBlack)
	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 66, scheduleData[day].talks[hour].line3, colorBlack)

	tinyfont.WriteLine(&display, &freesans.Regular9pt7b, 2, 96, scheduleData[day].talks[hour+1].startHour, colorBlack)
	tinyfont.WriteLine(&display, &freesans.Regular9pt7b, 2, 119, scheduleData[day].talks[hour+1].endHour, colorBlack)

	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 90, scheduleData[day].talks[hour+1].line1, colorBlack)
	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 103, scheduleData[day].talks[hour+1].line2, colorBlack)
	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 116, scheduleData[day].talks[hour+1].line3, colorBlack)

	tinyfont.WriteLine(&display, &freesans.Regular9pt7b, 2, 146, scheduleData[day].talks[hour+2].startHour, colorBlack)
	tinyfont.WriteLine(&display, &freesans.Regular9pt7b, 2, 169, scheduleData[day].talks[hour+2].endHour, colorBlack)

	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 140, scheduleData[day].talks[hour+2].line1, colorBlack)
	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 153, scheduleData[day].talks[hour+2].line2, colorBlack)
	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 166, scheduleData[day].talks[hour+2].line3, colorBlack)

	tinyfont.WriteLine(&display, &freesans.Regular9pt7b, 2, 196, scheduleData[day].talks[hour+3].startHour, colorBlack)
	tinyfont.WriteLine(&display, &freesans.Regular9pt7b, 2, 219, scheduleData[day].talks[hour+3].endHour, colorBlack)

	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 190, scheduleData[day].talks[hour+3].line1, colorBlack)
	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 203, scheduleData[day].talks[hour+3].line2, colorBlack)
	tinyfont.WriteLine(&display, &proggy.TinySZ8pt7b, 56, 216, scheduleData[day].talks[hour+3].line3, colorBlack)
}
