//! Localized strings for stdout (tabular output only). Code and stderr stay English.

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UiLang {
    ZhHans,
    En,
}

impl UiLang {
    pub fn detect() -> Self {
        let mut zh = false;
        if let Some(loc) = sys_locale::get_locale()
            && locale_implies_zh_hans(&loc)
        {
            zh = true;
        }
        #[cfg(unix)]
        {
            if let Ok(v) = std::env::var("LC_ALL")
                .or_else(|_| std::env::var("LC_MESSAGES"))
                .or_else(|_| std::env::var("LANG"))
                && env_implies_zh_cn(&v)
            {
                zh = true;
            }
        }
        if zh {
            UiLang::ZhHans
        } else {
            UiLang::En
        }
    }

    pub fn param_line(self, max_files: usize) -> String {
        match self {
            UiLang::ZhHans => format!("参数: max-files={max_files}"),
            UiLang::En => format!("args: max-files={max_files}"),
        }
    }

    pub fn root_label(self) -> &'static str {
        match self {
            UiLang::ZhHans => "根目录",
            UiLang::En => "root",
        }
    }

    pub fn units_note(self) -> &'static str {
        match self {
            UiLang::ZhHans => "大小与占比: 二进制 KiB/MiB/GiB (1024)。占比为百分数。",
            UiLang::En => "Sizes and ratios: binary KiB/MiB/GiB (1024). Percentages are %.",
        }
    }

    pub fn section_summary(self) -> &'static str {
        match self {
            UiLang::ZhHans => "========== 汇总（五类之和 = 合计）==========",
            UiLang::En => "========== summary (five categories = total) ==========",
        }
    }

    pub fn hdr_name(self) -> &'static str {
        match self {
            UiLang::ZhHans => "名称",
            UiLang::En => "Name",
        }
    }

    pub fn hdr_size(self) -> &'static str {
        match self {
            UiLang::ZhHans => "大小",
            UiLang::En => "Size",
        }
    }

    pub fn hdr_of_total(self) -> &'static str {
        match self {
            UiLang::ZhHans => "占合计",
            UiLang::En => "of total",
        }
    }

    pub fn label_total(self) -> &'static str {
        match self {
            UiLang::ZhHans => "合计",
            UiLang::En => "Total",
        }
    }

    pub fn category_label(self, cat: crate::Category) -> &'static str {
        match self {
            UiLang::ZhHans => match cat {
                crate::Category::Image => "图片",
                crate::Category::Audio => "音频",
                crate::Category::Video => "视频",
                crate::Category::Code => "代码",
                crate::Category::Other => "其它",
            },
            UiLang::En => match cat {
                crate::Category::Image => "Images",
                crate::Category::Audio => "Audio",
                crate::Category::Video => "Video",
                crate::Category::Code => "Code",
                crate::Category::Other => "Other",
            },
        }
    }

    pub fn section_global_files(self, max_files: usize) -> String {
        match self {
            UiLang::ZhHans => format!("========== 文件（全局 Top {max_files}，按大小降序）=========="),
            UiLang::En => format!("========== files (global top {max_files}, by size desc) =========="),
        }
    }

    pub fn hdr_path(self) -> &'static str {
        match self {
            UiLang::ZhHans => "路径",
            UiLang::En => "Path",
        }
    }

    pub fn hdr_pct_global(self) -> &'static str {
        match self {
            UiLang::ZhHans => "占全局",
            UiLang::En => "All%",
        }
    }

    pub fn hdr_pct_cat(self) -> &'static str {
        match self {
            UiLang::ZhHans => "占分类",
            UiLang::En => "Cat%",
        }
    }

    pub fn hdr_dim(self) -> &'static str {
        match self {
            UiLang::ZhHans => "尺寸",
            UiLang::En => "Dim",
        }
    }

    pub fn section_category_top(self, cat: crate::Category, max_files: usize) -> String {
        let c = self.category_label(cat);
        match (self, max_files) {
            (UiLang::ZhHans, 0) => format!("========== {c}（类内全部文件，按大小降序）=========="),
            (UiLang::En, 0) => format!(
                "========== {c} (all files in category, by size desc) =========="
            ),
            (UiLang::ZhHans, n) => format!("========== {c}（类内 Top {n}，按大小降序）=========="),
            (UiLang::En, n) => format!("========== {c} (top {n} in category, by size desc) =========="),
        }
    }

    pub fn no_files(self) -> &'static str {
        match self {
            UiLang::ZhHans => "目录中没有任何文件。",
            UiLang::En => "No files under this directory.",
        }
    }
}

fn locale_implies_zh_hans(s: &str) -> bool {
    let s = s.to_ascii_lowercase();
    s.starts_with("zh-cn")
        || s.starts_with("zh-hans")
        || s.starts_with("zh-sg")
        || s == "zh_cn"
}

fn env_implies_zh_cn(s: &str) -> bool {
    let s = s.to_ascii_lowercase();
    let base = s.split('.').next().unwrap_or(&s);
    let base = base.split('@').next().unwrap_or(base);
    base.starts_with("zh_cn") || base == "zh-cn"
}
