package com.jishisongfu.duobibi.ui

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.jishisongfu.duobibi.R
import com.jishisongfu.duobibi.ui.theme.DbbColors

/** 价格格式化:整数不带小数,否则两位小数。全 UI 共用。 */
fun fmtYuan(p: Double): String =
    if (p == p.toLong().toDouble()) p.toLong().toString() else "%.2f".format(p)

/** 比价擂台默认对比的 4 大电商(同一实体商品可跨平台买)。 */
val ARENA_PLATFORMS: List<String> = listOf("淘宝", "京东", "拼多多", "抖音")

/** 浮窗 / 记录覆盖的全部 7 个平台。 */
val ALL_PLATFORMS: List<String> = listOf("淘宝", "京东", "拼多多", "抖音", "美团", "美团外卖", "饿了么")

// ==================== 商品略缩图 ====================
/**
 * 商品品类略缩图(线条剪影)。**故意不放真实产品图** —— 商店审核同样禁止未授权使用
 * 他人产品图/包装,且占坑期没有联盟素材渠道。改成按标题关键字映射到 6 个内置剪影:
 * 手机 / 吹风机 / 掌机 / 胶囊 / 耳机盒 / 通用购物袋。每个剪影是自绘的几何线条,
 * 不是任一具体产品的复刻。
 */
enum class ProductCategory(@DrawableRes val drawableRes: Int) {
    Phone(R.drawable.ic_prod_phone),
    Hairdryer(R.drawable.ic_prod_hairdryer),
    Console(R.drawable.ic_prod_console),
    Capsule(R.drawable.ic_prod_capsule),
    Earbuds(R.drawable.ic_prod_earbuds),
    Default(R.drawable.ic_prod_default),
}

/** 按商品标题里的关键字推断品类。命中多组时取首个;无命中走通用购物袋。 */
fun categoryFor(title: String): ProductCategory {
    val t = title.lowercase()
    return when {
        listOf("iphone", "手机", "phone", "mate", "redmi", "小米", "pixel", "vivo", "oppo").any { t.contains(it) } -> ProductCategory.Phone
        listOf("戴森", "dyson", "吹风", "卷发", "直发器", "美发").any { t.contains(it) } -> ProductCategory.Hairdryer
        listOf("switch", "ps5", "xbox", "掌机", "游戏机", "steam deck").any { t.contains(it) } -> ProductCategory.Console
        listOf("胶囊", "咖啡", "nespresso", "多趣酷思", "espresso").any { t.contains(it) } -> ProductCategory.Capsule
        listOf("airpods", "耳机", "earbuds", "buds", "降噪").any { t.contains(it) } -> ProductCategory.Earbuds
        else -> ProductCategory.Default
    }
}

/**
 * 商品略缩图卡:圆角方块底 + 居中商品图。
 *
 * **渲染优先级**(从高到低):
 *  1. [ProductImageMap.findImage] 命中 → 渲染**真图**(白底容器 + Fit 缩放 + 不染色)
 *  2. 否则 → 走 [categoryFor] 出品类 vector icon(Teal 染色,跟主品牌色一致)
 *
 * Arena / Alert / Records 这种动态 title 场景自动得到"有图就用真图,没图也不丑"的体验。
 *
 * @param containerColor 容器底色 — vector icon 时用,真图固定用白底(产品图本就白底)。
 */
@Composable
fun ProductThumb(
    title: String,
    modifier: Modifier = Modifier,
    size: Dp = 54.dp,
    containerColor: Color = DbbColors.Fill,
) {
    val realImage = ProductImageMap.findImage(title)
    Box(
        modifier = modifier
            .size(size)
            .clip(RoundedCornerShape(size * 0.22f))
            .background(if (realImage != null) Color.White else containerColor),
        contentAlignment = Alignment.Center,
    ) {
        if (realImage != null) {
            Image(
                painter = painterResource(realImage),
                contentDescription = title,
                modifier = Modifier.size(size).clip(RoundedCornerShape(size * 0.22f)),
                contentScale = ContentScale.Fit,
            )
        } else {
            val cat = categoryFor(title)
            Image(
                painter = painterResource(cat.drawableRes),
                contentDescription = null,
                modifier = Modifier.size(size * 0.66f),
                colorFilter = ColorFilter.tint(DbbColors.TealDark),
            )
        }
    }
}
