#!/usr/bin/env -S godot --no-window -s
extends SceneTree

var config: ConfigManager = preload("res://config_manager.tres")

const valid_commands := ["list", "stop", "start"]

func _init():
	var cmd := CMD.new()
	for command in valid_commands:
		if cmd.has_argument(command):
			call(command, cmd.get_argument(command))
			return
	print("no command provided -- exiting")
	help()
	quit(1)


func help(_show: bool = true) -> void:
	print("Valid commands are:")
	for command in valid_commands:
		prints(" -",command)


func list(show_all: bool = true) -> void:
	var entries := config.timesheet.entries
	for item in entries:
		if show_all:
			print(item)
		else:
			if not item.is_closed:
				print(item)
	quit()


func stop(entry_name: String) -> void:
	var success := config.timesheet.stop_entry(entry_name)
	if success:
		print("closed %s"%[entry_name])
	else:
		print("could not close %s"%[entry_name])
	quit()


func start(entry_name: String) -> void:
	# warning-ignore:return_value_discarded
	config.timesheet.add_entry(entry_name)
	quit()
