use anyhow::{Result, bail};

use super::{ChoiceOption, FormKind};

const YES_NO_CHOICES: &[(&str, &str)] = &[("Yes", "yes"), ("No", "no")];

pub(super) fn yes_no(value: bool) -> String {
    if value { "yes" } else { "no" }.into()
}

pub(super) fn parse_yes(value: &str) -> bool {
    matches!(
        value.trim().to_lowercase().as_str(),
        "y" | "yes" | "true" | "1" | "on"
    )
}

pub(super) fn parse_yes_no(value: &str) -> Result<bool> {
    match value.trim().to_lowercase().as_str() {
        "y" | "yes" | "true" | "1" | "on" => Ok(true),
        "n" | "no" | "false" | "0" | "off" => Ok(false),
        _ => bail!("expected yes or no, got {value}"),
    }
}

pub(super) fn display_space(value: &str) -> String {
    if value == " " { "space" } else { value }.into()
}

pub(super) fn decode_space(value: &str) -> String {
    if value.eq_ignore_ascii_case("space") {
        " ".into()
    } else {
        value.into()
    }
}

pub(crate) fn form_choices(kind: &FormKind, field: usize) -> Option<Vec<ChoiceOption>> {
    let entries: &[(&str, &str)] = match kind {
        FormKind::Preferences => match field {
            0 => &[
                ("Classic — Lotus-inspired blue", "classic"),
                ("Mono — grayscale", "mono"),
                ("Amber — warm dark", "amber"),
                ("Greenscreen — muted phosphor green", "greenscreen"),
                ("Nord — arctic blue dark", "nord"),
                ("Catppuccin Mocha — cozy pastel dark", "catppuccin-mocha"),
            ],
            1..=3 | 6 | 8 => YES_NO_CHOICES,
            7 => &[
                ("ISO — 2026-08-16", "iso"),
                ("US — 08/16/2026", "us"),
                ("European — 16/08/2026", "european"),
                ("Long — 16 Aug 2026", "long"),
            ],
            _ => return None,
        },
        FormKind::DocumentSettings => match field {
            1 | 4 | 12 => YES_NO_CHOICES,
            2 => &[
                ("On demand", "on-demand"),
                ("On close", "on-close"),
                ("End of day", "end-of-day"),
                ("Immediately", "immediate"),
            ],
            3 => &[
                ("Keep completed items", "keep"),
                ("Move them to Trash", "trash"),
            ],
            5 => &[
                ("Year / month / day", "ymd"),
                ("Month / day / year", "mdy"),
                ("Day / month / year", "dmy"),
            ],
            6 => &[("Monday", "monday"), ("Sunday", "sunday")],
            _ => return None,
        },
        FormKind::Category(_) | FormKind::View(_) => return None,
    };
    Some(
        entries
            .iter()
            .map(|(label, value)| ChoiceOption {
                label: (*label).into(),
                value: (*value).into(),
            })
            .collect(),
    )
}

pub(super) fn form_prompt(kind: &FormKind, field: usize) -> &'static str {
    let prompts: &[&str] = match kind {
        FormKind::Category(_) => &[
            "Name",
            "Parent category",
            "Kind",
            "Match phrases",
            "Mutually exclusive",
            "Rule condition",
            "Rule action",
        ],
        FormKind::View(_) => &[
            "Name",
            "Kind",
            "Filter value",
            "Boolean filter",
            "Sort key",
            "Show done",
            "Columns",
            "Sections",
        ],
        FormKind::Preferences => &[
            "Theme",
            "Show command bar",
            "Show rule info",
            "Show return markers",
            "Item marker",
            "Autosave minutes",
            "Confirm destructive actions",
            "Date format",
            "24-hour clock",
            "Decimal separator",
            "Thousands separator",
        ],
        FormKind::DocumentSettings => &[
            "Description",
            "Backup on open",
            "Trash",
            "Completed items",
            "Automatic filing",
            "Numeric date order",
            "Week starts",
            "Default time",
            "Morning time",
            "Afternoon time",
            "Evening time",
            "Note tab width",
            "Report headers and footers",
        ],
    };
    prompts.get(field).copied().unwrap_or("")
}
