package main

import (
	"seehuhn.de/go/ncurses"
)

const MAX_TODOS = 3

type item struct {
	desc string
	done bool
}

func make_x(isDone bool) string {
	s := ""
	switch isDone {
	case true:
		s = "x"
	case false:
		s = " "
	}
	return s
}

func main() {
	hasQuit := false
	currTodo := 0

	todos := []item{
		{
			"Make TODO app",
			false,
		},
		{
			"Make coffee",
			true,
		},
		{
			"Shit the bed",
			false,
		},
		{
			"Make a cup of tea",
			false,
		},
	}

	// Init window
	win := ncurses.Init()
	defer ncurses.EndWin()
	ncurses.CursSet(ncurses.CursorOff)

	for !hasQuit {
		win.Erase()
		win.Println("TODO:")
		win.Println("----------------------")
		for index, item := range todos {
			if currTodo == index {
				win.AttrOn(ncurses.AttrStandout)
				win.Printf("[%s]", make_x(item.done))
				win.Printf(" %s", item.desc)
				win.Println("")
				win.AttrOff(ncurses.AttrStandout)
			} else {
				win.AttrOn(ncurses.AttrNormal)
				win.Printf("[%s]", make_x(item.done))
				win.Printf(" %s", item.desc)
				win.Println("")
				win.AttrOff(ncurses.AttrNormal)
			}
		}

		ch := win.GetCh()
		switch ch {

		case ncurses.KeyUp:
			if currTodo == 0 {
				currTodo = 0
			} else {
				currTodo -= 1
			}

		case ncurses.KeyDown:
			if currTodo >= len(todos)-1 {
				currTodo = len(todos) - 1
			} else {
				currTodo += 1
			}

		case ' ':
			todos[currTodo].done = !todos[currTodo].done

		case 'q':
			hasQuit = !hasQuit
		}
	}

	win.Refresh()
}
