package main

import "core:flags"
import "core:fmt"
import "core:time"

Command :: enum {
	usage,
	init,
	add,
	symptom,
	event,
	show,
	events,
	version,
}

Command_Func :: distinct proc(args: []string)

Command_Data := [Command]struct {
	func: Command_Func,
	desc: string,
} {
	.usage = {func = command_usage, desc = "this screen"},
	.init = {func = command_init, desc = "set up the hoard"},
	.add = {func = command_add, desc = "log today's daily entry"},
	.symptom = {func = command_symptom, desc = "note a symptom"},
	.event = {func = command_event, desc = "log a timestamped event"},
	.show = {func = command_show, desc = "show a specific date"},
	.events = {func = command_events, desc = "list events"},
	.version = {func = command_version, desc = "print version"},
}

command_from_string :: proc(cmd_str: string) -> Command {
	cmd: Command
	switch cmd_str {
	case "usage":
		cmd = .usage
	case "init":
		cmd = .init
	case "add":
		cmd = .add
	case "symptom":
		cmd = .symptom
	case "event":
		cmd = .event
	case "show":
		cmd = .show
	case "events":
		cmd = .events
	case "version":
		cmd = .version
	}
	return cmd
}

RESET :: "\e[0m"
BOLD :: "\e[1m"
DIM :: "\e[2m"
YELLOW :: "\e[33m"
GREEN :: "\e[32m"

command_usage :: proc(args: []string) {
	fmt.printf("%s🧌 Data Goblin%s v%s\n\n", BOLD, RESET, "0.1")
	fmt.printf("  %sThe goblin stares at you. You forgot to say what you want.%s\n\n", DIM, RESET)
	fmt.printf("  Available commands:\n\n")
	for data, cmd in Command_Data {
		fmt.printf("    %s%-14s%s %s%s%s\n", GREEN, cmd, RESET, DIM, data.desc, RESET)
	}
	fmt.printf("\n  Usage: %sdgob  [flags]%s\n\n", YELLOW, RESET)
}

Init_Options :: struct {
	db: string `usage:"path to sqlite-file to use (defaults to ./data.db)"`,
}

command_init :: proc(args: []string) {
	fmt.printf("init\n")
	fmt.printf("args: %v\n", args)

	opt: Init_Options
	flags.parse_or_exit(&opt, args, .Unix)
}

Add_Options :: struct {
	mood:          int `args:"required" usage:"mood of the day, 1-5"`,
	sleep:         f32 `args:"required" usage:"the amount of slept hours (ex: 1.5)"`,
	sleep_quality: int `args:"required" usage:"the previous night's sleep quality, 1-5"`,
	date:          time.Time `usage:"date to record this data to"`,
	notes:         string `usage:"any extra notes for the day"`,
}

command_add :: proc(args: []string) {
	fmt.printf("add\n")
	fmt.printf("args: %v\n", args)

	opt: Add_Options
	flags.parse_or_exit(&opt, args, .Unix)
	if err := storage_add_to_daily_log(nil, opt.mood, opt.sleep, opt.sleep_quality, opt.date, opt.notes); err != nil {
		fmt.eprintf("failed adding log: %v\n", err)
	}
}

command_symptom :: proc(args: []string) {
	fmt.printf("symptom\n")
	fmt.printf("args: %v\n", args)
}

command_event :: proc(args: []string) {
	fmt.printf("event\n")
	fmt.printf("args: %v\n", args)
}

command_show :: proc(args: []string) {
	fmt.printf("show\n")
	fmt.printf("args: %v\n", args)
}

command_events :: proc(args: []string) {
	fmt.printf("events\n")
	fmt.printf("args: %v\n", args)
}

command_version :: proc(args: []string) {
	fmt.printf("version\n")
	fmt.printf("args: %v\n", args)
}