package gui

import "core:fmt"
import rl "vendor:raylib"
import "../core"
import "../shared"
import "../ui"

set_status :: proc(status: ^string, msg: string) {
	delete(status^)
	status^ = fmt.aprintf("%s", msg)
}

push_status :: proc(status: ^string, log: ^Action_Log, msg: string) {
	set_status(status, msg)
	action_log_push(log, status^)
}

push_status_if_nonempty :: proc(status: ^string, log: ^Action_Log, msg: string) {
	if len(msg) > 0 {
		push_status(status, log, msg)
	}
}

push_dirty_status :: proc(is_dirty: ^bool, status: ^string, log: ^Action_Log, msg: string) {
	is_dirty^ = true
	push_status(status, log, msg)
}

screen_status_label :: proc(screen: ui.App_Screen) -> string {
	switch screen {
	case .Story: return "Story"
	case .Script: return "Script"
	case .Characters: return "Characters"
	case .Panels: return "Panels"
	case .Layout: return "Layout"
	case .Bubbles: return "Bubbles"
	case .Export: return "Export"
	case .Community: return "Community"
	}
	return "Unknown"
}

navigate_screen_with_status :: proc(controller: ^ui.App_Controller, screen: ui.App_Screen) -> string {
	err := ui.navigate_to_screen(controller, screen)
	if !shared.is_ok(err) {
		return err.message
	}
	return fmt.aprintf("Screen: %s", screen_status_label(screen))
}

request_confirmation :: proc(show_confirm_overlay, show_help_overlay: ^bool, pending_confirm_action: ^Pending_Confirm_Action, action: Pending_Confirm_Action, prompt: string) -> string {
	show_confirm_overlay^ = true
	show_help_overlay^ = false
	pending_confirm_action^ = action
	return prompt
}

toggle_autosave_with_message :: proc(autosave_enabled: ^bool) -> string {
	autosave_enabled^ = !autosave_enabled^
	return fmt.aprintf("Autosave: %s", yn(autosave_enabled^))
}

reset_helper_fields_with_message :: proc(export_path, local_script_pages, autosave_interval_text: ^string, f: core.Export_Format) -> string {
	reset_helper_fields(export_path, local_script_pages, autosave_interval_text, f)
	return "Reset helper fields to defaults"
}

toggle_help_overlay :: proc(show_help_overlay: ^bool) {
	show_help_overlay^ = !show_help_overlay^
}

close_help_overlay_if_open :: proc(show_help_overlay: ^bool) {
	if show_help_overlay^ {
		show_help_overlay^ = false
	}
}

clear_action_log_with_message :: proc(log: ^Action_Log) -> string {
	action_log_dispose(log)
	return "Action log cleared"
}

reset_log_view :: proc(log_show_lines: ^i32, log_oldest_first: ^bool) {
	log_show_lines^ = 6
	log_oldest_first^ = false
}

reset_log_view_with_message :: proc(log_show_lines: ^i32, log_oldest_first: ^bool) -> string {
	reset_log_view(log_show_lines, log_oldest_first)
	return "Reset log view"
}

toggle_log_lines_with_message :: proc(log_show_lines: ^i32) -> string {
	if log_show_lines^ == 6 {
		log_show_lines^ = 4
	} else {
		log_show_lines^ = 6
	}
	return fmt.aprintf("Log lines: %d", log_show_lines^)
}

toggle_log_order_with_message :: proc(log_oldest_first: ^bool) -> string {
	log_oldest_first^ = !log_oldest_first^
	order := "newest"
	if log_oldest_first^ {
		order = "oldest"
	}
	return fmt.aprintf("Log order: %s first", order)
}

clear_selected_field :: proc(selected_field: int, state: ^core.Comic_State, export_path, local_script_pages, project_path, autosave_interval_text: ^string) -> bool {
	switch selected_field {
	case 0:
		if len(state.story_idea) == 0 { return false }
		state.story_idea = ""
	case 1:
		if len(state.story_genre) == 0 { return false }
		state.story_genre = ""
	case 2:
		if len(state.target_audience) == 0 { return false }
		state.target_audience = ""
	case 3:
		if len(export_path^) == 0 { return false }
		export_path^ = ""
	case 4:
		if len(local_script_pages^) == 0 { return false }
		local_script_pages^ = ""
	case 5:
		if len(project_path^) == 0 { return false }
		project_path^ = ""
	case 6:
		if len(autosave_interval_text^) == 0 { return false }
		autosave_interval_text^ = ""
	case:
		return false
	}
	return true
}

clear_selected_field_with_message :: proc(selected_field: int, state: ^core.Comic_State, export_path, local_script_pages, project_path, autosave_interval_text: ^string, is_dirty: ^bool) -> string {
	if clear_selected_field(selected_field, state, export_path, local_script_pages, project_path, autosave_interval_text) {
		is_dirty^ = true
		return "Cleared selected field"
	}
	return "Selected field already empty"
}

paste_clipboard_into_selected_field_with_message :: proc(selected_field: int, state: ^core.Comic_State, export_path, local_script_pages, project_path, autosave_interval_text: ^string, is_dirty: ^bool) -> string {
	clip_raw := rl.GetClipboardText()
	if clip_raw == nil {
		return ""
	}
	clip_text := fmt.aprintf("%s", clip_raw)
	switch selected_field {
	case 0: state.story_idea = clip_text
	case 1: state.story_genre = clip_text
	case 2: state.target_audience = clip_text
	case 3: export_path^ = clip_text
	case 4: local_script_pages^ = clip_text
	case 5: project_path^ = clip_text
	case 6: autosave_interval_text^ = clip_text
	}
	is_dirty^ = true
	return "Pasted clipboard into selected field"
}

Action_Log :: struct {
	entries: [8]string,
	owned: [8]bool,
	entry_times: [8]f64,
	count: int,
	last_push_at: f64,
}

action_log_push :: proc(log: ^Action_Log, msg: string) {
	now := rl.GetTime()
	idx := log.count % len(log.entries)
	if log.owned[idx] {
		delete(log.entries[idx])
	}
	log.entries[idx] = fmt.aprintf("%s", msg)
	log.owned[idx] = true
	log.entry_times[idx] = now
	log.count += 1
	log.last_push_at = now
}

action_log_dispose :: proc(log: ^Action_Log) {
	for i in 0..<len(log.entries) {
		if log.owned[i] {
			delete(log.entries[i])
			log.owned[i] = false
		}
		log.entry_times[i] = 0
	}
	log.count = 0
	log.last_push_at = 0
}
