package com.jishisongfu.duobibi.ui

import androidx.annotation.DrawableRes
import com.jishisongfu.duobibi.R

/**
 * 商品 title → 真图 drawable 反查表。
 *
 * **场景**:Arena 比价表格 / Alert 雷达卡片 / Records 商品组里显示的商品 title 是**动态**的
 * (用户输入或浮窗自动捕获),不像 Discover SEED 直接挂 imageRes。这里做关键词模糊匹配,
 * 命中就用对应真图,没命中就让上层走 vector icon fallback。
 *
 * **匹配规则**(命中即返回,顺序很重要 — 把更具体的关键词放前面):
 *  - title 经 lowercase 后包含任一 keyword(小写) → 命中
 *  - 同一商品多写几个 keyword(品牌名 / 英文 / 中文 / 系列名) 提高命中率
 *  - **同品牌多产品**:必须把更细的子型号写在品牌名之前,否则品牌名先命中导致错图
 *    (例:戴森吸尘器 / 戴森吹风机;飞利浦剃须刀 / 飞利浦电动牙刷)
 *
 * **添加新映射**:在 [MAPPINGS] 适当位置追加 `listOf("kw1", "kw2", ...) to R.drawable.img_xxx`。
 */
object ProductImageMap {

    private val MAPPINGS: List<Pair<List<String>, Int>> = listOf(
        // ===== 数码家电 =====
        // 手机两分:iPhone 系列全走苹果图(img_iphone_blue 当前是 iPhone 16 蓝,
        // 后续 mentor 给 iPhone 17 图就替换 img_iphone_blue 这一个文件,代码不动)
        // 注意:iPhone 关键词必须在"手机"通用关键词之前,否则 iphone 被通用规则先吃掉
        listOf("iphone", "苹果手机", "苹果17", "苹果15", "苹果16", "苹果手机")
            to R.drawable.img_iphone_blue,
        listOf("airpods", "苹果耳机") to R.drawable.img_airpods,
        listOf("macbook", "苹果笔记本") to R.drawable.img_macbook,
        listOf("ipad") to R.drawable.img_ipad_air,
        // Android 手机统一用小米 14 Pro 图作为视觉代表 — 覆盖国内国际主流品牌全系
        listOf(
            "小米", "xiaomi", "redmi", "红米",
            "华为", "huawei", "mate", "honor", "荣耀",
            "oppo", "vivo", "魅族", "meizu",
            "三星", "samsung", "galaxy",
            "realme", "oneplus", "一加",
            "android 手机", "安卓手机", "手机", "phone",
        ) to R.drawable.img_xiaomi14pro,

        // 同品牌多产品 — 子品类必须在品牌名前面,先 match 子品类
        listOf("吸尘", "vacuum") to R.drawable.img_dyson_vacuum,
        listOf("戴森", "dyson", "supersonic", "吹风机", "电吹风") to R.drawable.img_dyson_hd,
        listOf("电动牙刷", "牙刷", "toothbrush") to R.drawable.img_philips_toothbrush,
        listOf("剃须刀", "shaver") to R.drawable.img_philips_shaver,

        // ===== 餐饮外卖 =====
        listOf("海底捞") to R.drawable.img_haidilao,
        listOf("吉野家", "肥牛饭", "红烧肥牛") to R.drawable.img_beef_rice,
        // KFC 独立成图 — 从 hamburger 移出来,因为 KFC 自家图不是汉堡而是炸鸡桶
        listOf("肯德基", "kfc", "疯狂星期四", "9.9 元桶", "9.9元桶") to R.drawable.img_kfc,
        // 麦当劳/汉堡保持 hamburger 图
        listOf("汉堡", "鸡腿堡", "麦当劳", "麦辣", "burger", "巨无霸") to R.drawable.img_hamburger,
        listOf("喜茶", "heytea", "多肉葡萄", "奶茶") to R.drawable.img_heytea,

        // ===== 同城买菜 =====
        listOf("五花肉", "猪肉") to R.drawable.img_pork_belly,
        listOf("鸡蛋", "土鸡蛋") to R.drawable.img_eggs,
        listOf("特仑苏", "蒙牛", "牛奶") to R.drawable.img_telunsu,
        listOf("叶菜", "青菜", "蔬菜礼包", "绿色叶菜") to R.drawable.img_greens,

        // ===== 电商 =====
        listOf("雀巢", "nestle", "nescafe", "多趣酷思", "咖啡胶囊") to R.drawable.img_nescafe,
        listOf("三只松鼠", "每日坚果", "坚果") to R.drawable.img_squirrel,
        listOf("自嗨锅", "自热米饭", "自热饭") to R.drawable.img_zhxg,
        listOf("加多宝") to R.drawable.img_jdb,
        // 酒水 — 茅台关键词放前(避免被"白酒"等通用词误匹配)
        listOf("茅台", "飞天茅台", "飞天", "moutai") to R.drawable.img_moutai,

        // ===== 美妆 =====
        listOf("sk-ii", "skii", "神仙水") to R.drawable.img_skii,
        listOf("雅诗兰黛", "小棕瓶", "estee", "lauder") to R.drawable.img_estee_lauder,
        listOf("兰蔻", "lancome", "lancôme", "absolue", "菁纯") to R.drawable.img_lancome_eye,
        listOf("资生堂", "红腰子", "shiseido") to R.drawable.img_shiseido_red,

        // ===== 鞋服 =====
        listOf("nike", "air force", "af1", "空军一号") to R.drawable.img_nike_af1,
        listOf("lululemon", "瑜伽裤", "align") to R.drawable.img_lululemon,

        // ===== 日用 / 母婴 =====
        listOf("花王", "妙而舒", "merries", "纸尿裤") to R.drawable.img_merries,
        listOf("维达", "抽纸") to R.drawable.img_vinda_tissue,
        listOf("海尔", "保温杯") to R.drawable.img_haier_cup,
        listOf("美的", "电热水壶", "热水壶", "烧水壶", "midea") to R.drawable.img_midea_kettle,
        listOf("苏泊尔", "不粘锅", "炒锅", "supor") to R.drawable.img_supor_pan,
    )

    /**
     * 给定商品 title,返回匹配到的 drawable id;没命中返回 null。
     * caller 一般用法:`ProductImageMap.findImage(title) ?: fallbackVectorIcon`
     */
    @DrawableRes
    fun findImage(title: String): Int? {
        if (title.isBlank()) return null
        val lower = title.lowercase()
        return MAPPINGS.firstOrNull { (kws, _) ->
            kws.any { lower.contains(it.lowercase()) }
        }?.second
    }
}
