//! Centralized color definitions for Cagire TUI.
//! Supports multiple color schemes with runtime switching.

pub mod palette;
pub mod build;
mod catppuccin_latte;
mod catppuccin_mocha;
mod dracula;
mod eden;
mod ember;
mod everforest;
mod georges;
mod fairyfloss;
mod gruvbox_dark;
mod hot_dog_stand;
mod iceberg;
mod jaipur;
mod kanagawa;
mod letz_light;
mod monochrome_black;
mod monochrome_white;
mod monokai;
mod nord;
mod fauve;
mod pitch_black;
mod tropicalia;
mod rose_pine;
mod tokyo_night;
pub mod transform;

use ratatui::style::Color;
use std::cell::RefCell;

/// Entry in the theme registry: id, display label, and palette constructor.
pub struct ThemeEntry {
    pub id: &'static str,
    pub label: &'static str,
    pub palette: fn() -> palette::Palette,
}

/// All available themes.
pub const THEMES: &[ThemeEntry] = &[
    ThemeEntry { id: "CatppuccinMocha", label: "Catppuccin Mocha", palette: catppuccin_mocha::palette },
    ThemeEntry { id: "CatppuccinLatte", label: "Catppuccin Latte", palette: catppuccin_latte::palette },
    ThemeEntry { id: "Nord", label: "Nord", palette: nord::palette },
    ThemeEntry { id: "Dracula", label: "Dracula", palette: dracula::palette },
    ThemeEntry { id: "GruvboxDark", label: "Gruvbox Dark", palette: gruvbox_dark::palette },
    ThemeEntry { id: "Monokai", label: "Monokai", palette: monokai::palette },
    ThemeEntry { id: "MonochromeBlack", label: "Monochrome (Black)", palette: monochrome_black::palette },
    ThemeEntry { id: "MonochromeWhite", label: "Monochrome (White)", palette: monochrome_white::palette },
    ThemeEntry { id: "PitchBlack", label: "Pitch Black", palette: pitch_black::palette },
    ThemeEntry { id: "TokyoNight", label: "Tokyo Night", palette: tokyo_night::palette },
    ThemeEntry { id: "RosePine", label: "Rosé Pine", palette: rose_pine::palette },
    ThemeEntry { id: "Kanagawa", label: "Kanagawa", palette: kanagawa::palette },
    ThemeEntry { id: "Fairyfloss", label: "Fairyfloss", palette: fairyfloss::palette },
    ThemeEntry { id: "HotDogStand", label: "Hot Dog Stand", palette: hot_dog_stand::palette },
    ThemeEntry { id: "LetzLight", label: "Letz Light", palette: letz_light::palette },
    ThemeEntry { id: "Ember", label: "Ember", palette: ember::palette },
    ThemeEntry { id: "Eden", label: "Eden", palette: eden::palette },
    ThemeEntry { id: "Georges", label: "Georges", palette: georges::palette },
    ThemeEntry { id: "Iceberg", label: "Iceberg", palette: iceberg::palette },
    ThemeEntry { id: "Everforest", label: "Everforest", palette: everforest::palette },
    ThemeEntry { id: "Fauve", label: "Fauve", palette: fauve::palette },
    ThemeEntry { id: "Tropicalia", label: "Tropicalia", palette: tropicalia::palette },
    ThemeEntry { id: "Jaipur", label: "Jaipur", palette: jaipur::palette },
];

thread_local! {
    static CURRENT_THEME: RefCell<ThemeColors> = RefCell::new(build::build(&(THEMES[0].palette)()));
}

/// Return the current thread-local theme.
pub fn get() -> ThemeColors {
    CURRENT_THEME.with(|t| t.borrow().clone())
}

/// Set the current thread-local theme.
pub fn set(theme: ThemeColors) {
    CURRENT_THEME.with(|t| *t.borrow_mut() = theme);
}

/// Complete set of resolved colors for all UI components.
#[derive(Clone)]
pub struct ThemeColors {
    pub ui: UiColors,
    pub status: StatusColors,
    pub selection: SelectionColors,
    pub tile: TileColors,
    pub header: HeaderColors,
    pub modal: ModalColors,
    pub flash: FlashColors,
    pub list: ListColors,
    pub link_status: LinkStatusColors,
    pub syntax: SyntaxColors,
    pub table: TableColors,
    pub values: ValuesColors,
    pub hint: HintColors,
    pub view_badge: ViewBadgeColors,
    pub nav: NavColors,
    pub editor_widget: EditorWidgetColors,
    pub browser: BrowserColors,
    pub input: InputColors,
    pub search: SearchColors,
    pub markdown: MarkdownColors,
    pub engine: EngineColors,
    pub dict: DictColors,
    pub title: TitleColors,
    pub meter: MeterColors,
    pub sparkle: SparkleColors,
    pub confirm: ConfirmColors,
}

/// Core UI colors: background, text, borders.
#[derive(Clone)]
pub struct UiColors {
    pub bg: Color,
    pub bg_rgb: (u8, u8, u8),
    pub text_primary: Color,
    pub text_muted: Color,
    pub text_dim: Color,
    pub border: Color,
    pub header: Color,
    pub unfocused: Color,
    pub accent: Color,
    pub surface: Color,
}

/// Playback status bar colors.
#[derive(Clone)]
pub struct StatusColors {
    pub playing_bg: Color,
    pub playing_fg: Color,
    pub stopped_bg: Color,
    pub stopped_fg: Color,
    pub fill_on: Color,
    pub fill_off: Color,
    pub fill_bg: Color,
}

/// Step grid selection and cursor colors.
#[derive(Clone)]
pub struct SelectionColors {
    pub cursor_bg: Color,
    pub cursor_fg: Color,
    pub selected_bg: Color,
    pub selected_fg: Color,
    pub in_range_bg: Color,
    pub in_range_fg: Color,
    pub cursor: Color,
    pub selected: Color,
    pub in_range: Color,
}

/// Step tile colors for various states.
#[derive(Clone)]
pub struct TileColors {
    pub playing_active_bg: Color,
    pub playing_active_fg: Color,
    pub playing_inactive_bg: Color,
    pub playing_inactive_fg: Color,
    pub active_bg: Color,
    pub active_fg: Color,
    pub content_bg: Color,
    pub inactive_bg: Color,
    pub inactive_fg: Color,
    pub active_selected_bg: Color,
    pub active_in_range_bg: Color,
    pub link_bright: [(u8, u8, u8); 5],
    pub link_dim: [(u8, u8, u8); 5],
}

/// Top header bar segment colors.
#[derive(Clone)]
pub struct HeaderColors {
    pub tempo_bg: Color,
    pub tempo_fg: Color,
    pub bank_bg: Color,
    pub bank_fg: Color,
    pub pattern_bg: Color,
    pub pattern_fg: Color,
    pub stats_bg: Color,
    pub stats_fg: Color,
}

/// Modal dialog border colors.
#[derive(Clone)]
pub struct ModalColors {
    pub border: Color,
    pub border_accent: Color,
    pub border_warn: Color,
    pub border_dim: Color,
    pub confirm: Color,
    pub rename: Color,
    pub input: Color,
    pub editor: Color,
    pub preview: Color,
}

/// Flash notification colors.
#[derive(Clone)]
pub struct FlashColors {
    pub error_bg: Color,
    pub error_fg: Color,
    pub success_bg: Color,
    pub success_fg: Color,
    pub info_bg: Color,
    pub info_fg: Color,
}

/// Pattern list row state colors.
#[derive(Clone)]
pub struct ListColors {
    pub playing_bg: Color,
    pub playing_fg: Color,
    pub staged_play_bg: Color,
    pub staged_play_fg: Color,
    pub staged_stop_bg: Color,
    pub staged_stop_fg: Color,
    pub edit_bg: Color,
    pub edit_fg: Color,
    pub hover_bg: Color,
    pub hover_fg: Color,
    pub muted_bg: Color,
    pub muted_fg: Color,
    pub soloed_bg: Color,
    pub soloed_fg: Color,
}

/// Ableton Link status indicator colors.
#[derive(Clone)]
pub struct LinkStatusColors {
    pub disabled: Color,
    pub connected: Color,
    pub listening: Color,
}

/// Syntax highlighting (fg, bg) pairs per token category.
#[derive(Clone)]
pub struct SyntaxColors {
    pub gap_bg: Color,
    pub executed_bg: Color,
    pub selected_bg: Color,
    pub emit: (Color, Color),
    pub number: (Color, Color),
    pub string: (Color, Color),
    pub comment: (Color, Color),
    pub keyword: (Color, Color),
    pub stack_op: (Color, Color),
    pub operator: (Color, Color),
    pub sound: (Color, Color),
    pub param: (Color, Color),
    pub context: (Color, Color),
    pub note: (Color, Color),
    pub interval: (Color, Color),
    pub variable: (Color, Color),
    pub vary: (Color, Color),
    pub generator: (Color, Color),
    pub user_defined: (Color, Color),
    pub default: (Color, Color),
}

/// Alternating table row colors.
#[derive(Clone)]
pub struct TableColors {
    pub row_even: Color,
    pub row_odd: Color,
}

/// Value display colors.
#[derive(Clone)]
pub struct ValuesColors {
    pub tempo: Color,
    pub value: Color,
}

/// Keyboard hint key/text colors.
#[derive(Clone)]
pub struct HintColors {
    pub key: Color,
    pub text: Color,
}

/// View badge pill colors.
#[derive(Clone)]
pub struct ViewBadgeColors {
    pub bg: Color,
    pub fg: Color,
}

/// Navigation minimap tile colors.
#[derive(Clone)]
pub struct NavColors {
    pub selected_bg: Color,
    pub selected_fg: Color,
    pub unselected_bg: Color,
    pub unselected_fg: Color,
}

/// Script editor colors.
#[derive(Clone)]
pub struct EditorWidgetColors {
    pub cursor_bg: Color,
    pub cursor_fg: Color,
    pub selection_bg: Color,
    pub completion_bg: Color,
    pub completion_fg: Color,
    pub completion_selected: Color,
    pub completion_example: Color,
}

/// File and sample browser colors.
#[derive(Clone)]
pub struct BrowserColors {
    pub directory: Color,
    pub project_file: Color,
    pub selected: Color,
    pub file: Color,
    pub focused_border: Color,
    pub unfocused_border: Color,
    pub root: Color,
    pub file_icon: Color,
    pub folder_icon: Color,
    pub empty_text: Color,
}

/// Text input field colors.
#[derive(Clone)]
pub struct InputColors {
    pub text: Color,
    pub cursor: Color,
    pub hint: Color,
}

/// Search bar and match highlight colors.
#[derive(Clone)]
pub struct SearchColors {
    pub active: Color,
    pub inactive: Color,
    pub match_bg: Color,
    pub match_fg: Color,
}

/// Markdown renderer colors.
#[derive(Clone)]
pub struct MarkdownColors {
    pub h1: Color,
    pub h2: Color,
    pub h3: Color,
    pub code: Color,
    pub code_border: Color,
    pub link: Color,
    pub link_url: Color,
    pub quote: Color,
    pub text: Color,
    pub list: Color,
}

/// Engine view panel colors.
#[derive(Clone)]
pub struct EngineColors {
    pub header: Color,
    pub header_focused: Color,
    pub divider: Color,
    pub scroll_indicator: Color,
    pub label: Color,
    pub label_focused: Color,
    pub label_dim: Color,
    pub value: Color,
    pub focused: Color,
    pub normal: Color,
    pub dim: Color,
    pub path: Color,
    pub border_magenta: Color,
    pub border_green: Color,
    pub border_cyan: Color,
    pub separator: Color,
    pub hint_active: Color,
    pub hint_inactive: Color,
}

/// Dictionary view colors.
#[derive(Clone)]
pub struct DictColors {
    pub word_name: Color,
    pub word_bg: Color,
    pub alias: Color,
    pub stack_sig: Color,
    pub description: Color,
    pub example: Color,
    pub category_focused: Color,
    pub category_selected: Color,
    pub category_normal: Color,
    pub category_dimmed: Color,
    pub border_focused: Color,
    pub border_normal: Color,
    pub header_desc: Color,
}

/// Title screen colors.
#[derive(Clone)]
pub struct TitleColors {
    pub big_title: Color,
    pub author: Color,
    pub link: Color,
    pub license: Color,
    pub prompt: Color,
    pub subtitle: Color,
}

/// VU meter and spectrum level colors.
#[derive(Clone)]
pub struct MeterColors {
    pub low: Color,
    pub mid: Color,
    pub high: Color,
    pub low_rgb: (u8, u8, u8),
    pub mid_rgb: (u8, u8, u8),
    pub high_rgb: (u8, u8, u8),
}

/// Sparkle particle colors.
#[derive(Clone)]
pub struct SparkleColors {
    pub colors: [(u8, u8, u8); 5],
}

/// Confirm dialog colors.
#[derive(Clone)]
pub struct ConfirmColors {
    pub border: Color,
    pub button_selected_bg: Color,
    pub button_selected_fg: Color,
}

