package gui

import "core:fmt"
import "core:path/filepath"
import "core:strings"
import "../core"

format_suffix :: proc(f: core.Export_Format) -> string {
	switch f {
	case .PDF: return ".pdf"
	case .PNG: return ".zip"
	case .CBZ: return ".cbz"
	}
	return ".pdf"
}

trim_known_export_suffix :: proc(path: string) -> string {
	if strings.has_suffix(path, ".pdf") { return path[:len(path)-4] }
	if strings.has_suffix(path, ".zip") { return path[:len(path)-4] }
	if strings.has_suffix(path, ".cbz") { return path[:len(path)-4] }
	if strings.has_suffix(path, ".png") { return path[:len(path)-4] }
	return path
}

export_path_for_format :: proc(path: string, f: core.Export_Format) -> string {
	base := trim_known_export_suffix(path)
	return fmt.aprintf("%s%s", base, format_suffix(f))
}

default_export_filename_for_format :: proc(f: core.Export_Format) -> string {
	switch f {
	case .PDF: return "comic.pdf"
	case .PNG: return "comic_png.zip"
	case .CBZ: return "comic.cbz"
	}
	return "comic.pdf"
}

default_export_path_for_format :: proc(f: core.Export_Format) -> string {
	return fmt.aprintf("./%s", default_export_filename_for_format(f))
}

export_path_in_project_dir :: proc(project_path: string, f: core.Export_Format) -> string {
	dir, _ := filepath.split(project_path)
	if len(dir) == 0 {
		dir = "./"
	}
	parts := []string{dir, default_export_filename_for_format(f)}
	joined, err := filepath.join(parts)
	if err != nil {
		return default_export_path_for_format(f)
	}
	return joined
}

reset_helper_fields :: proc(export_path, local_script_pages, autosave_interval_text: ^string, f: core.Export_Format) {
	export_path^ = default_export_path_for_format(f)
	local_script_pages^ = "2"
	autosave_interval_text^ = "20"
}

sync_export_path_to_project_dir :: proc(project_path: string, export_path: ^string, f: core.Export_Format) {
	export_path^ = export_path_in_project_dir(project_path, f)
}

project_path_in_export_dir :: proc(export_path: string) -> string {
	dir, _ := filepath.split(export_path)
	if len(dir) == 0 {
		dir = "./"
	}
	parts := []string{dir, "gui_project.comic.json"}
	joined, err := filepath.join(parts)
	if err != nil {
		return "./gui_project.comic.json"
	}
	return joined
}

normalize_project_path :: proc(path: string) -> string {
	trimmed := strings.trim_space(path)
	if len(trimmed) == 0 {
		return "./gui_project.comic.json"
	}
	if strings.has_suffix(trimmed, ".comic.json") {
		return trimmed
	}
	if strings.has_suffix(trimmed, ".json") {
		return fmt.aprintf("%s.comic.json", trimmed[:len(trimmed)-5])
	}
	return fmt.aprintf("%s.comic.json", trimmed)
}

normalize_project_path_field :: proc(path: ^string) {
	path^ = normalize_project_path(path^)
}

normalize_export_path_field :: proc(path: ^string, f: core.Export_Format) {
	trimmed := strings.trim_space(path^)
	if len(trimmed) == 0 {
		path^ = default_export_path_for_format(f)
		return
	}
	path^ = export_path_for_format(trimmed, f)
}

fix_all_paths :: proc(project_path, export_path: ^string, f: core.Export_Format) {
	normalize_project_path_field(project_path)
	normalize_export_path_field(export_path, f)
}

set_export_preset_with_message :: proc(export_path: ^string, f: core.Export_Format) -> string {
	export_path^ = default_export_path_for_format(f)
	return fmt.aprintf("Preset export path: %s", export_path^)
}

set_export_path_from_project_with_message :: proc(export_path: ^string, project_path: string, f: core.Export_Format) -> string {
	export_path^ = export_path_in_project_dir(project_path, f)
	return fmt.aprintf("Export path from project dir: %s", export_path^)
}

set_project_path_from_export_with_message :: proc(project_path: ^string, export_path: string) -> string {
	project_path^ = project_path_in_export_dir(export_path)
	return fmt.aprintf("Project path from export dir: %s", project_path^)
}

normalize_project_path_with_message :: proc(project_path: ^string) -> string {
	normalize_project_path_field(project_path)
	return fmt.aprintf("Normalized project path: %s", project_path^)
}

normalize_export_path_with_message :: proc(export_path: ^string, f: core.Export_Format) -> string {
	normalize_export_path_field(export_path, f)
	return fmt.aprintf("Normalized export path: %s", export_path^)
}

fix_all_paths_with_message :: proc(project_path, export_path: ^string, f: core.Export_Format) -> string {
	fix_all_paths(project_path, export_path, f)
	return fmt.aprintf("Normalized paths: P=%s E=%s", project_path^, export_path^)
}

project_path_is_normalized :: proc(path: string) -> bool {
	trimmed := strings.trim_space(path)
	return len(trimmed) > 0 && strings.has_suffix(trimmed, ".comic.json")
}

export_path_matches_format :: proc(path: string, f: core.Export_Format) -> bool {
	trimmed := strings.trim_space(path)
	if len(trimmed) == 0 {
		return false
	}
	return strings.has_suffix(trimmed, format_suffix(f))
}

path_health_hint :: proc(project_ok, export_ok: bool) -> string {
	if project_ok && export_ok {
		return ""
	}
	if !project_ok && !export_ok {
		return "Fix paths: P/E/PE buttons or Ctrl+Shift+U"
	}
	if !project_ok {
		return "Fix project path: P button or Ctrl+Shift+K"
	}
	return "Fix export path: E button or Ctrl+Shift+M"
}
