package ui

import "../core"
import "../shared"

UI_Command_Kind :: enum {
	Navigate,
	Set_Workflow,
	Start_Generate,
	Set_Progress,
	Complete_Job,
	Fail_Job,
	Cancel_Job,
}

UI_Command :: struct {
	kind: UI_Command_Kind,
	screen: App_Screen,
	workflow_step: core.Workflow_Step,
	job_type: Job_Type,
	job_id: int,
	progress: f32,
	message: string,
}

UI_Runtime_Result :: struct {
	job_id: int,
	err: shared.App_Error,
}

apply_command :: proc(c: ^App_Controller, cmd: UI_Command) -> UI_Runtime_Result {
	res := UI_Runtime_Result{job_id = 0, err = shared.ok()}
	switch cmd.kind {
	case .Navigate:
		res.err = navigate_to_screen(c, cmd.screen)
	case .Set_Workflow:
		res.err = set_workflow_step(c, cmd.workflow_step)
	case .Start_Generate:
		res.job_id = start_background_job(c, cmd.job_type, cmd.message)
	case .Set_Progress:
		set_generation_progress(c, cmd.progress)
	case .Complete_Job:
		res.err = finish_background_job(c, cmd.job_id, "")
	case .Fail_Job:
		res.err = finish_background_job(c, cmd.job_id, cmd.message)
	case .Cancel_Job:
		res.err = cancel_background_job(c, cmd.job_id)
	}
	return res
}

apply_commands :: proc(c: ^App_Controller, cmds: []UI_Command) -> shared.App_Error {
	for cmd in cmds {
		res := apply_command(c, cmd)
		if !shared.is_ok(res.err) {
			return res.err
		}
	}
	return shared.ok()
}
