using YMhut.Box.Core.Tools;

namespace YMhut.Box.WinUI.Views.Tools;

public sealed record ToolPageProfile(
    string InputTitle,
    string InputDescription,
    string InputHeader,
    string OutputHeader,
    string RuleDescription,
    IReadOnlyList<string> Rules,
    bool ShowOption = false,
    string OptionHeader = "保留原始顺序",
    bool ShowAuxiliary = false,
    string AuxiliaryHeader = "附加参数",
    string AuxiliaryPlaceholder = "按工具需要填写，例如分隔符、关键字、前缀或密钥",
    bool ShowNumber = false,
    string NumberHeader = "数量 / 精度",
    double NumberDefault = 5);

public static class ToolPageProfiles
{
    public static ToolPageProfile For(IToolModule module)
    {
        return module.Id switch
        {
            "json_formatter" => Structured("JSON 输入", "粘贴 JSON 后选择美化、压缩或校验；结果区会保持结构化缩进。", "JSON", ["美化 JSON", "压缩 JSON", "校验结构", "提取键路径"]),
            "xml_formatter" => Structured("XML 输入", "粘贴 XML 片段后格式化和校验，适合接口响应和配置文件排查。", "XML", ["格式化 XML", "压缩 XML", "校验标签闭合"]),
            "sql_formatter" => Structured("SQL 输入", "粘贴 SQL 查询语句，按关键字和子句整理为可读格式。", "SQL", ["标准格式化", "压缩单行", "关键字大写"]),
            "csv_json_converter" => Structured("CSV / JSON 输入", "输入 CSV 表格或 JSON 数组，在两种结构之间转换。", "表格 / JSON", ["CSV 转 JSON", "JSON 转 CSV", "带表头解析", "TSV 兼容"]),
            "csv_tsv_converter" => Structured("CSV / TSV 输入", "输入逗号或制表符分隔内容，转换为目标分隔格式。", "表格文本", ["CSV 转 TSV", "TSV 转 CSV", "保留引号"]),
            "yaml_json_converter" => Structured("YAML / JSON 输入", "输入轻量 YAML 或键值结构，转换为 JSON。", "YAML / 键值", ["YAML 转 JSON", "键值转 JSON", "压缩输出"]),
            "markdown_table_normalizer" => Text("Markdown 表格", "粘贴 Markdown 表格，自动补齐列宽、分隔线和空缺单元。", ["对齐表格", "补齐分隔线", "去除多余空格"]),
            "json_path_helper" => Structured("JSON 输入", "输入 JSON 后生成常见 JSONPath 路径，便于接口字段定位。", "JSON", ["列出叶子路径", "只列数组路径", "只列对象路径"]),
            "base64_codec" => Codec("Base64 文本", "输入普通文本或 Base64 内容，页面会同时给出编码和解码结果。", ["编码", "解码", "自动识别"]),
            "url_codec" or "query_builder" => Codec("URL / Query 输入", "粘贴 URL、查询参数或普通文本，执行百分号编码、解码和参数拆解。", ["URL 编码", "URL 解码", "Query 拆解", "Query 构建"]),
            "html_entity" => Codec("HTML 文本", "输入 HTML 或普通文本，处理实体转义和反转义。", ["实体编码", "实体解码", "保留换行"]),
            "unicode_codec" => Codec("Unicode 文本", "输入文本或 Unicode 转义序列，在可读文本和转义之间转换。", ["转义编码", "转义解码", "混合识别"]),
            "punycode_codec" => Codec("域名输入", "输入国际化域名或 ASCII 域名，执行 IDN/Punycode 转换。", ["Unicode 域名转 ASCII", "ASCII 转 Unicode"]),
            "jwt_decoder" => Codec("JWT Token", "粘贴 JWT，解析 Header 和 Payload，不在本地保存令牌内容。", ["解码头部", "解码载荷", "显示过期时间"]),
            "regex_tool" => Text("正则与测试文本", "第一行输入正则表达式，后续输入待匹配文本，结果显示命中内容。", ["全局匹配", "忽略大小写", "多行模式", "只输出捕获组"], showAux: true, auxHeader: "替换文本 / 说明", auxPlaceholder: "可选：替换文本或测试说明"),
            "html_minifier" or "js_obfuscator" => Structured("代码片段", "输入 HTML、CSS 或 JavaScript 片段，生成压缩或轻量混淆结果。", "代码", ["去注释", "压缩空白", "轻量混淆"]),
            "key_value_converter" or "form_urlencoded_builder" => Structured("键值输入", "输入 ENV、Query、JSON 或表单键值，转换为目标结构。", "键值 / JSON", ["键值转 JSON", "JSON 转 Query", "表单编码"]),
            "column_extractor" => Text("表格文本", "输入 CSV/TSV/空格分隔内容，按列号提取字段。", ["提取第 1 列", "提取多列", "去空值"], showAux: true, auxHeader: "列号", auxPlaceholder: "例如：1 或 1,3,5"),
            "cron_helper" => Structured("Cron 表达式", "输入五段 Cron 表达式，校验并解释执行计划。", "Cron", ["解释表达式", "校验字段", "下次运行提示"]),
            "hash_generator" => Security("待哈希文本", "输入文本后同时计算 MD5、SHA1、SHA256、SHA512。", ["文本哈希", "十六进制输出", "大写输出"]),
            "hmac_generator" => Security("密钥与正文", "附加参数填写密钥，输入区填写正文，生成 HMAC 摘要。", ["HMAC-SHA256", "HMAC-SHA1", "十六进制输出"], showAux: true, auxHeader: "密钥", auxPlaceholder: "例如：ymhut-secret"),
            "password_generator" => Generator("密码生成", "填写长度和字符策略，生成本地随机强密码。", ["字母数字符号", "易读字符", "仅数字", "仅字母"], showNumber: true, numberHeader: "密码长度", numberDefault: 24),
            "uuid_generator" or "ulid_generator" => Generator("ID 生成", "本地生成唯一 ID，适合接口调试、数据库测试和文件命名。", ["生成 1 个", "生成 5 个", "复制友好格式"]),
            "random_generator" or "random_picker" => Generator("随机规则", "填写长度、范围或列表，生成随机字符串、数字或抽样结果。", ["随机字符串", "随机数字", "随机颜色", "列表抽取"], showNumber: true, numberHeader: "生成数量 / 长度", numberDefault: 32),
            "timestamp_converter" => Calculator("时间输入", "输入 Unix 秒/毫秒时间戳或留空获取当前时间。", ["时间戳转本地时间", "当前时间戳", "毫秒识别"]),
            "number_base" or "number_base_converter" => Calculator("数字输入", "输入十进制或 0x 开头十六进制，输出二/八/十/十六进制。", ["自动识别", "十进制输入", "十六进制输入"]),
            "calculator_tool" => Calculator("表达式", "输入四则表达式或数字参数，计算结果会在结果卡中呈现。", ["表达式计算", "安全模式", "保留小数"]),
            "unit_converter" or "length_converter" or "weight_converter" or "temperature_converter" or "speed_converter" or "area_converter" or "volume_converter" or "data_size_converter" or "time_duration_converter" => Calculator("数值", "输入基础数值，按工具名称输出相关单位换算表。", ["全部单位", "常用单位", "保留四位小数"]),
            "bmi_calculator" => Calculator("体重和身高", "输入体重 kg 与身高 m，例如 70 1.75。", ["BMI", "健康区间", "保留一位小数"]),
            "loan_emi_calculator" => Calculator("贷款参数", "输入本金、年利率、月数，例如 100000 4.5 360。", ["月供", "总利息", "总还款"]),
            "simple_interest_calculator" or "compound_interest_calculator" => Calculator("利息参数", "输入本金、利率和周期，计算利息或复利终值。", ["单利/复利", "年化", "详细步骤"]),
            "percentage_calculator" or "percentage_change_calculator" or "discount_calculator" or "tax_calculator" => Calculator("价格 / 比例参数", "输入相关数值，计算百分比、涨跌幅、折扣或税额。", ["标准计算", "反向计算", "保留两位小数"]),
            "ratio_simplifier" or "average_calculator" or "median_calculator" or "gcd_lcm_calculator" or "permutation_calculator" or "combination_calculator" => Calculator("数字列表", "输入一个或多个数字，输出统计、比例或组合计算结果。", ["标准计算", "详细步骤", "校验输入"]),
            "date_difference_calculator" or "workday_calculator" or "age_calculator" or "shelf_life" => Calculator("日期参数", "输入日期或日期区间，计算天数、工作日、年龄或保质期。", ["日期差", "包含结束日", "自然日 / 工作日"]),
            "case_converter" => Text("文本", "输入文本后转换大小写、标题格式和命名风格。", ["大写", "小写", "标题格式", "camelCase", "snake_case"]),
            "chinese_converter" => Text("中文文本", "输入简体或繁体中文，执行本地转换。", ["简转繁", "繁转简", "保留标点"]),
            "text_diff" => Text("两段文本", "使用 --- 分隔左右文本，输出新增和删除行。", ["按行对比", "忽略空白", "只看差异"]),
            "text_deduplicator" or "manifest_deduplicator" => Text("多行文本", "按行去重，可保留首次出现顺序。", ["保留首次出现", "忽略大小写", "去除空行"], showOption: true),
            "text_sorter" or "manifest_sorter" => Text("多行文本", "按行排序，适合清单、路径和普通列表。", ["升序", "降序", "自然排序", "忽略大小写"]),
            "text_filter" => Text("多行文本", "按关键字或正则筛选行。", ["包含关键字", "排除关键字", "正则筛选"], showAux: true, auxHeader: "关键字 / 正则", auxPlaceholder: "例如：error 或 ^WARN"),
            "delimiter_converter" or "line_column_transformer" or "text_splitter" => Text("分隔文本", "在换行、逗号、制表符和自定义分隔符之间转换。", ["按换行", "按逗号", "按制表符", "自定义分隔符"], showAux: true, auxHeader: "自定义分隔符", auxPlaceholder: "例如：|"),
            "text_merger" => Text("多行文本", "将多行内容合并为一行或自定义分隔列表。", ["空格合并", "逗号合并", "自定义分隔符"], showAux: true, auxHeader: "合并分隔符", auxPlaceholder: "例如：, "),
            "text_numbering" => Text("多行文本", "为每行添加递增编号。", ["1. 样式", "001 样式", "自定义前缀"], showNumber: true, numberHeader: "起始编号", numberDefault: 1),
            "blank_line_cleaner" or "whitespace_normalizer" or "text_trimmer" => Text("文本", "清理空行、首尾空白和连续空白。", ["移除空行", "压缩空白", "逐行 Trim"]),
            "prefix_suffix_batch" => Text("多行文本", "为每行批量添加前缀和后缀。", ["添加前缀", "添加后缀", "前后同时添加"], showAux: true, auxHeader: "前缀|后缀", auxPlaceholder: "例如：prefix_|_suffix"),
            "list_sampler" => Text("列表", "从列表中随机抽样。", ["随机抽样", "去重后抽样", "保持顺序"], showNumber: true, numberHeader: "抽样数量", numberDefault: 5),
            "list_chunker" => Text("列表", "按固定大小把列表切分为多个块。", ["按行分块", "按字符分块"], showNumber: true, numberHeader: "分块大小", numberDefault: 10),
            "list_intersection" or "list_union" or "list_difference" => Text("两组列表", "使用 --- 分隔左右列表，计算交集、并集或差集。", ["交集", "并集", "差集", "忽略大小写"], showOption: true),
            "list_group_statistics" or "duplicate_detector" or "line_length_analyzer" or "keyword_counter" or "text_statistics" => Text("文本 / 列表", "统计字符、行、关键词、重复项或分布信息。", ["基础统计", "分组计数", "输出 Top 项"], showAux: true, auxHeader: "关键词", auxPlaceholder: "可选：要计数的关键词"),
            "template_filler" => Text("模板与数据", "输入模板和键值对，用变量替换占位符。", ["{{name}}", "${name}", "严格模式"], showAux: true, auxHeader: "键值对", auxPlaceholder: "name=YMhut\nversion=2.0"),
            "filename_formatter" or "path_normalizer" or "path_breakdown" or "extension_extractor" or "extension_statistics" or "file_manifest_builder" or "batch_rename_preview" or "indexed_rename_preview" or "manifest_template_export" => Text("文件名 / 路径列表", "输入路径或文件名列表，生成清洗、拆解、统计或重命名预览。", ["路径规范化", "扩展名统计", "重命名预览"], showAux: true, auxHeader: "命名模板 / 前缀", auxPlaceholder: "例如：prefix=photo_"),
            "text_reverser" => Text("文本", "反转字符、单词或行顺序。", ["反转字符", "反转单词", "反转行"]),
            "markdown_list_cleaner" => Text("Markdown 列表", "规范 Markdown 列表符号、缩进和空白。", ["统一 - 列表", "清理缩进", "去空项"]),
            "line_pair_zipper" => Text("两列文本", "使用 --- 分隔两组列表，按顺序合并为配对行。", ["用空格合并", "用逗号合并", "自定义分隔符"], showAux: true, auxHeader: "列间分隔符", auxPlaceholder: "例如： => "),
            "qr_generator" => File("二维码内容", "输入文本后生成 SVG 二维码，可复制或保存。", ["生成 SVG", "低纠错", "高纠错"]),
            "qrcode_scanner" => File("二维码图片路径", "选择或输入图片路径，识别其中的二维码内容。", ["识别图片", "多格式", "失败提示"]),
            "image_processor" => File("图片路径", "选择图片后查看信息或进行基础处理。", ["查看信息", "缩放", "格式转换"]),
            "archive_tool" => File("文件 / 归档路径", "选择文件或归档，创建 zip/tar 或查看归档内容。", ["创建 Zip", "查看归档", "解包提示"]),
            "media_player" => File("媒体文件", "使用系统 MediaPlayerElement 播放本地媒体。", ["播放", "暂停", "音量"]),
            "safe_browser" => Remote("网址", "使用 WebView2 打开隔离浏览器页面。", ["打开网址", "隔离用户数据", "外部链接保护"]),
            "random_cinema" => Remote("远程媒体源", "进入页面读取远程配置，动态展示随机图和随机视频。", ["随机图", "随机视频", "重新加载媒体"]),
            "weather" or "train_query" or "dns_query" or "ip_lookup" or "ip_info" or "rdap_ip_lookup" or "rdap_domain_lookup" or "domain_price" or "http_diagnostic" or "wx_domain_check" => Remote("查询条件", "输入城市、域名、IP、URL 或车站信息，联网读取官方/公开来源。", ["进入即加载", "短缓存", "手动重新加载"]),
            "hotboard" => Remote("榜单 / 资讯源", "选择平台后读取远程榜单数据，结果以列表和摘要呈现。", ["平台选择", "加载榜单", "点击卡片打开"]),
            "baidu_hot" or "bili_hot" or "zhihu_hot" or "cctv_news" or "tech_news" or "football_news" or "movie_box_office" or "earthquake_info" or "gold_price" or "oil_price" or "history_today" => Remote("榜单 / 资讯源", "打开页面自动读取远程数据源，结果以列表和摘要呈现。", ["今日榜单", "重新加载", "显示摘要"]),
            "car_info" or "sanguosha_skin" or "mime_lookup" or "http_status_lookup" or "port_lookup" or "dns_record_lookup" or "unicode_block_lookup" or "charset_lookup" or "timezone_abbr_lookup" or "regex_preset_lookup" or "magic_number_lookup" => Reference("查询关键字", "读取随包权威参考数据，支持关键词命中和空查询浏览。", ["关键词查询", "显示前 40 条", "数据源说明"]),
            "system_info" or "system_tool" or "pc_benchmark" => SystemTool("系统命令", "读取系统状态、执行轻量维护或跑分，结果只显示本机摘要。", ["状态", "进程", "临时目录", "轻量跑分"]),
            "qq_avatar" => Generator("QQ 号码", "输入 QQ 号码后展示头像与可用公开资料。", ["头像预览", "资料字段", "来源状态"]),
            "ai_translation" => Remote("待翻译文本", "AI Provider 尚作为后续配置项，当前保留入口和错误提示。", ["翻译", "语言自动识别", "Provider 配置"]),
            "ymhut_uutool_suite" or "smart_search" => Remote("搜索关键字", "聚合工具目录和常用入口，便于快速跳转。", ["工具搜索", "入口聚合", "关键词匹配"]),
            "color_picker" => Generator("颜色值", "输入 HEX/RGB/HSL 颜色，解析并输出多格式结果。", ["HEX 转 RGB", "RGB 转 HEX", "显示预览"]),
            "ascii_art" => Generator("短文本", "输入短文本，生成 ASCII 艺术文本。", ["块状字", "紧凑输出", "复制友好"]),
            "slug_generator" => Text("标题文本", "把标题转换成 URL 友好的 slug。", ["小写", "连字符", "移除特殊符号"]),
            "html_text_extractor" => Text("HTML 内容", "移除 HTML 标签，提取纯文本正文。", ["移除标签", "保留换行", "压缩空白"]),
            "profanity_check" => Security("敏感词与正文", "第一段输入词表，--- 后输入正文，结果显示命中和脱敏文本。", ["自定义词表", "脱敏输出", "命中列表"], showAux: true, auxHeader: "额外词", auxPlaceholder: "每行一个敏感词"),
            "hash_manifest_builder" or "hash_manifest_verify" => Security("文件清单 / 校验清单", "输入路径或清单文本，生成或校验 SHA256 清单。", ["生成清单", "校验清单", "路径模式"]),
            _ => module.Metadata.Category switch
            {
                ToolCategory.Calculator => Calculator("参数", "按工具名称填写参数，输出对应计算结果。", ["标准计算", "详细结果", "输入校验"]),
                ToolCategory.Text => Text("文本", "输入文本后按工具名称执行对应处理。", ["默认规则", "严格模式", "宽松模式"]),
                ToolCategory.Network or ToolCategory.Data => Remote("查询条件", "打开页面会读取真实远程或随包数据源。", ["进入即加载", "短缓存", "手动重新加载"]),
                ToolCategory.Image => File("文件或媒体参数", "选择本地文件或输入路径执行处理。", ["选择文件", "预览", "保存结果"]),
                ToolCategory.Security => Security("输入", "执行本地安全校验或摘要生成。", ["标准", "详细", "复制友好"]),
                ToolCategory.System => SystemTool("命令", "读取本机系统状态或执行维护入口。", ["状态", "诊断", "摘要"]),
                _ => Text("输入", "输入内容并执行工具。", ["默认规则"])
            }
        };
    }

    private static ToolPageProfile Text(
        string inputTitle,
        string description,
        IReadOnlyList<string> rules,
        bool showOption = false,
        bool showAux = false,
        string auxHeader = "附加参数",
        string auxPlaceholder = "可选参数",
        bool showNumber = false,
        string numberHeader = "数量 / 精度",
        double numberDefault = 5)
    {
        return new ToolPageProfile(inputTitle, description, "文本输入", "处理结果", "文本类工具按行、分隔符、正则或模板规则处理。", rules, showOption, "保留原始顺序", showAux, auxHeader, auxPlaceholder, showNumber, numberHeader, numberDefault);
    }

    private static ToolPageProfile Structured(string title, string description, string header, IReadOnlyList<string> rules)
        => new(title, description, header, "结构化输出", "结构化工具关注格式、校验和转换方向。", rules);

    private static ToolPageProfile Codec(string title, string description, IReadOnlyList<string> rules)
        => new(title, description, "编码输入", "编码 / 解码结果", "编码类工具支持自动识别、正向转换和反向转换。", rules);

    private static ToolPageProfile Calculator(string title, string description, IReadOnlyList<string> rules)
        => new(title, description, "参数", "计算结果", "计算类工具使用表单化参数语义，输出结果摘要和明细。", rules, ShowNumber: false);

    private static ToolPageProfile Remote(string title, string description, IReadOnlyList<string> rules)
        => new(title, description, "查询条件", "远程数据 / 解析结果", "联网工具按需加载，统一走 ApiManager/HttpService 和短缓存。", rules);

    private static ToolPageProfile Reference(string title, string description, IReadOnlyList<string> rules)
        => new(title, description, "查询关键字", "参考数据结果", "参考数据来自随包 dat 数据包或开发环境原始数据。", rules);

    private static ToolPageProfile File(string title, string description, IReadOnlyList<string> rules)
        => new(title, description, "文件路径 / 参数", "文件处理结果", "文件类工具支持系统文件选择器、预览和明确成功/失败反馈。", rules);

    private static ToolPageProfile Security(
        string title,
        string description,
        IReadOnlyList<string> rules,
        bool showAux = false,
        string auxHeader = "密钥 / 词表",
        string auxPlaceholder = "可选：密钥、词表或清单参数")
    {
        return new ToolPageProfile(title, description, "安全输入", "安全校验结果", "安全类工具默认本地执行，避免记录敏感正文。", rules, ShowAuxiliary: showAux, AuxiliaryHeader: auxHeader, AuxiliaryPlaceholder: auxPlaceholder);
    }

    private static ToolPageProfile Generator(
        string title,
        string description,
        IReadOnlyList<string> rules,
        bool showNumber = false,
        string numberHeader = "数量 / 长度",
        double numberDefault = 5)
    {
        return new ToolPageProfile(title, description, "生成参数", "生成结果", "生成类工具可控制数量、长度或输出格式。", rules, ShowNumber: showNumber, NumberHeader: numberHeader, NumberDefault: numberDefault);
    }

    private static ToolPageProfile SystemTool(string title, string description, IReadOnlyList<string> rules)
        => new(title, description, "系统参数", "系统结果", "系统工具只展示本机摘要，不执行危险操作。", rules);
}
