package com.jishisongfu.duobibi.ui.discover

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.outlined.FavoriteBorder
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
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.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jishisongfu.duobibi.ui.fmtYuan
import com.jishisongfu.duobibi.ui.rememberHaptics
import com.jishisongfu.duobibi.ui.theme.DbbColors

/**
 * 单张"比价好物"卡片(双列瀑布流 cell)。
 *
 * **结构性约束**(避免跟傻瓜比价 CommunityScreen 视觉撞):
 * - 不用大图,卡片头是 24dp emoji + 品类色条
 * - 卡片重心在"价格对比"(全网最低大字 + ≥2 平台 mini 行),不是图文笔记
 * - 单列宽度,文字密度高(信息卡风),而不是图卡风
 *
 * heightHint 0/1/2 控制顶部 emoji 区高度(56/72/96dp),让瀑布流错落。
 */
@Composable
fun PriceCompareCard(
    item: DiscoverItem,
    onClick: () -> Unit,
    isWatched: Boolean = false,
    onToggleWatch: (() -> Unit)? = null,
) {
    val cat = item.category
    // 实图条目的头部块抬高一档 —— emoji 32sp 在 56-96dp 够大,
    // 真图按 Fit 缩放,在 56dp 高里看起来太挤,给 96-144dp 更接近主流商品卡。
    val headerHeight = if (item.imageRes != null) {
        when (item.heightHint) { 0 -> 96.dp; 1 -> 120.dp; else -> 144.dp }
    } else {
        when (item.heightHint) { 0 -> 56.dp; 1 -> 72.dp; else -> 96.dp }
    }
    val sorted = item.platformPrices.entries.sortedBy { it.value }
    val mini = sorted.drop(1).take(3)  // 除最低外再展示最多 3 个
    val haptics = rememberHaptics()

    Card(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick),
        colors = CardDefaults.cardColors(containerColor = DbbColors.Card),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
        shape = RoundedCornerShape(14.dp),
    ) {
        Column {
            // 顶部头部:imageRes 优先(真图),否则 emoji 兜底。
            // 真图统一白底,因为产品图基本都是白底拍的;emoji 用 cat.tintBg 保留品类色码。
            Box(
                Modifier
                    .fillMaxWidth()
                    .height(headerHeight)
                    .background(if (item.imageRes != null) Color.White else cat.tintBg),
                contentAlignment = Alignment.Center,
            ) {
                if (item.imageRes != null) {
                    Image(
                        painter = painterResource(item.imageRes),
                        contentDescription = item.title,
                        modifier = Modifier.fillMaxSize().padding(8.dp),
                        contentScale = ContentScale.Fit,
                    )
                } else {
                    Text(item.emoji, fontSize = 32.sp)
                }
                // 右上角品类小标签
                Box(
                    Modifier.align(Alignment.TopEnd).padding(6.dp)
                        .clip(RoundedCornerShape(4.dp)).background(Color.White.copy(alpha = 0.85f))
                        .padding(horizontal = 5.dp, vertical = 1.dp),
                ) {
                    Text(cat.title, color = cat.tintFg, fontSize = 9.sp, fontWeight = FontWeight.Bold)
                }
                // 左上角 ❤️ toggle:已关注=实心红心,未关注=白色圆底空心
                if (onToggleWatch != null) {
                    Box(
                        Modifier
                            .align(Alignment.TopStart)
                            .padding(6.dp)
                            .size(28.dp)
                            .clip(CircleShape)
                            .background(Color.White.copy(alpha = 0.92f))
                            .clickable { haptics.toggle(); onToggleWatch() },
                        contentAlignment = Alignment.Center,
                    ) {
                        Icon(
                            if (isWatched) Icons.Filled.Favorite else Icons.Outlined.FavoriteBorder,
                            contentDescription = if (isWatched) "取消关注" else "关注",
                            tint = if (isWatched) DbbColors.Rose else DbbColors.InkSec,
                            modifier = Modifier.size(16.dp),
                        )
                    }
                }
            }

            Column(Modifier.padding(10.dp)) {
                // 商品名(最多 2 行)
                Text(
                    item.title,
                    fontSize = 12.sp,
                    fontWeight = FontWeight.Medium,
                    color = DbbColors.Ink,
                    maxLines = 2,
                    lineHeight = 16.sp,
                )
                Spacer(Modifier.height(8.dp))
                // 全网最低大字 — letterSpacing 收紧 + 加大,跟普通文字拉开层级
                Row(verticalAlignment = Alignment.Bottom) {
                    Text(
                        "¥", color = DbbColors.Teal,
                        fontSize = 12.sp, fontWeight = FontWeight.ExtraBold,
                        letterSpacing = (-0.2).sp,
                    )
                    Text(
                        fmtYuan(item.minPrice),
                        color = DbbColors.Teal,
                        fontSize = 24.sp,
                        fontWeight = FontWeight.ExtraBold,
                        letterSpacing = (-0.8).sp,
                    )
                }
                Text(
                    "${item.minPlatform} 最低",
                    color = DbbColors.InkSec,
                    fontSize = 10.sp,
                )
                if (item.savings > 0) {
                    Text(
                        "省 ¥${fmtYuan(item.savings)}",
                        color = DbbColors.Save,
                        fontSize = 10.sp,
                        fontWeight = FontWeight.SemiBold,
                    )
                }
                Spacer(Modifier.height(8.dp))
                // 其它平台 mini 对比行(≤3 行,每行:平台 + ¥价)
                Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
                    mini.forEach { (plat, price) ->
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            Box(
                                Modifier
                                    .size(6.dp)
                                    .clip(RoundedCornerShape(3.dp))
                                    .background(DbbColors.InkTer),
                            )
                            Spacer(Modifier.size(5.dp))
                            Text(plat, color = DbbColors.InkSec, fontSize = 10.sp)
                            Spacer(Modifier.size(5.dp))
                            Text(
                                "¥${fmtYuan(price)}",
                                color = DbbColors.Ink,
                                fontSize = 10.sp,
                                fontWeight = FontWeight.Medium,
                            )
                        }
                    }
                }
                if (item.highlight != null) {
                    Spacer(Modifier.height(6.dp))
                    Box(
                        Modifier
                            .clip(RoundedCornerShape(4.dp))
                            .background(DbbColors.GoldBg)
                            .padding(horizontal = 5.dp, vertical = 1.dp),
                    ) {
                        Text(item.highlight, color = DbbColors.Gold, fontSize = 9.sp, fontWeight = FontWeight.Bold)
                    }
                }
            }
        }
    }
}
