package plus.rua.project.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectVerticalDragGestures
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.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.datetime.LocalDate
import kotlinx.datetime.number
import plus.rua.project.LunarCache
import plus.rua.project.ShiftKind

/**
 * 底部卡片，折叠状态下支持垂直拖拽触发折叠动画。
 *
 * 卡片顶部显示拖拽把手，下方展示选中日期信息：
 * 左侧为相对今天的天数描述（A）和公历日期（B），
 * 右侧为农历日期（C）。
 *
 * @param isCollapsed 当前是否处于折叠状态
 * @param selectedDate 当前选中的日期
 * @param today 今天的日期
 * @param shiftKind 当前选中日期的个人轮班类型
 * @param onDrag 展开状态下拖拽回调，delta 正值推动折叠
 * @param onDragEnd 展开状态拖拽结束回调
 * @param onExpandDrag 折叠状态下拖拽回调，delta 负值推动展开
 * @param onExpandDragEnd 折叠状态拖拽结束回调
 * @param dragRangePx 拖拽手势映射范围（像素），progress 从 0→1 对应手指移动此距离。
 *   应设为折叠时日历实际高度变化量 (weeks-1)×rowHeight，使拖拽跟手。
 * @param modifier 外部布局修饰符
 */
@Composable
fun BottomCard(
    isCollapsed: Boolean,
    selectedDate: LocalDate,
    today: LocalDate,
    shiftKind: ShiftKind?,
    onDrag: (Float) -> Unit,
    onDragEnd: () -> Unit,
    onExpandDrag: (Float) -> Unit,
    onExpandDragEnd: () -> Unit,
    dragRangePx: Float,
    modifier: Modifier = Modifier
) {
    val relativeDesc = relativeDayDescription(selectedDate, today)

    val solarDesc = "${selectedDate.month.number}月${selectedDate.day}日"
    val lunarDesc by produceState(
        initialValue = "",
        key1 = selectedDate
    ) {
        value = LunarCache.default.formatLunarDate(selectedDate)
    }
    val shiftMessage = when (shiftKind) {
        ShiftKind.WORK -> "小小上班，轻松拿下！"
        ShiftKind.OFF -> "耶耶耶，美美休息！"
        null -> null
    }

    Surface(
        modifier = modifier
            .fillMaxWidth()
            .testTag("bottom_card")
            .pointerInput(isCollapsed) {
                if (isCollapsed) {
                    // 折叠状态：下拉恢复到月视图
                    detectVerticalDragGestures(
                        onDragEnd = { onExpandDragEnd() },
                        onDragCancel = { onExpandDragEnd() }
                    ) { _, dragAmount ->
                        val delta = -dragAmount / dragRangePx
                        onExpandDrag(delta)
                    }
                } else {
                    // 展开状态：上拉折叠到周视图
                    detectVerticalDragGestures(
                        onDragEnd = { onDragEnd() },
                        onDragCancel = { onDragEnd() }
                    ) { _, dragAmount ->
                        val delta = -dragAmount / dragRangePx
                        onDrag(delta)
                    }
                }
            },
        shape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp),
        color = MaterialTheme.colorScheme.surfaceVariant,
        shadowElevation = 4.dp
    ) {
        Column(modifier = Modifier.fillMaxSize()) {
            // 拖拽把手
            Box(
                modifier = Modifier
                    .padding(top = 8.dp, bottom = 8.dp)
                    .clip(RoundedCornerShape(2.dp))
                    .background(MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.4f))
                    .fillMaxWidth(0.15f)
                    .height(4.dp)
                    .align(Alignment.CenterHorizontally)
                    .semantics {
                        contentDescription = "拖拽以展开或折叠日历"
                    }
            )
            Spacer(modifier = Modifier.height(8.dp))
            // A / B / C 信息行
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 左侧：A（相对天数）和 B（公历日期）在同一行
                Row(
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = relativeDesc,
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        fontSize = 14.sp
                    )
                    Spacer(modifier = Modifier.width(6.dp))
                    Text(
                        text = solarDesc,
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        fontSize = 14.sp
                    )
                }
                // 右侧：C（农历日期）
                Text(
                    text = lunarDesc,
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    fontSize = 14.sp
                )
            }
            if (shiftMessage != null) {
                Text(
                    text = shiftMessage,
                    modifier = Modifier
                        .align(Alignment.CenterHorizontally)
                        .padding(top = 12.dp, bottom = 6.dp),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    fontSize = 14.sp
                )
            }
            Spacer(modifier = Modifier.height(12.dp))
            AnimatedWebp(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(140.dp)
                    .align(Alignment.CenterHorizontally),
                contentDescription = "可爱小狗",
                seed = selectedDate,
            )
        }
    }
}
