package com.tsmobile.app.ui.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp
import com.tsmobile.app.data.ChannelDetailInfo
import com.tsmobile.app.ui.theme.UiTokens

@Composable
fun ChannelDetailCard(
    channelDetail: ChannelDetailInfo?,
    isCurrentChannel: Boolean,
    onSwitchChannel: () -> Unit,
    onDismiss: () -> Unit,
) {
    val maxHeight = (LocalConfiguration.current.screenHeightDp * 0.8f).dp
    DetailSheetScaffold(
        title = channelDetail?.name ?: "频道详情",
        onDismiss = onDismiss,
        modifier = Modifier.padding(UiTokens.Spacing.Large),
        maxHeight = maxHeight,
        content = {
            if (channelDetail != null) ChannelInfoSection(channelDetail)
            else Text("正在加载频道信息...", style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
        },
        footer = {
            Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
                TextButton(onClick = onDismiss) { Text("关闭") }
                if (!isCurrentChannel) {
                    Spacer(Modifier.width(UiTokens.Spacing.Small))
                    Button(onClick = onSwitchChannel) { Text("切换到此频道") }
                }
            }
        },
    )
}

@Composable
private fun ChannelInfoSection(channelDetail: ChannelDetailInfo) {
    Column(verticalArrangement = Arrangement.spacedBy(UiTokens.Spacing.Small)) {
        if (channelDetail.topic.isNotEmpty()) DetailInfoRow("主题", channelDetail.topic)
        DetailInfoRow("人数", if (channelDetail.maxClients >= 0) "${channelDetail.maxClients}" else "无限制")
        if (channelDetail.neededTalkPower > 0) DetailInfoRow("需要说话权限", "${channelDetail.neededTalkPower}")
        if (channelDetail.codec > 0) DetailInfoRow("编解码器", codecName(channelDetail.codec))
        buildList {
            if (channelDetail.isPassword) add("有密码")
            if (channelDetail.isPermanent) add("永久")
            if (channelDetail.isSemiPermanent) add("半永久")
        }.takeIf { it.isNotEmpty() }?.let { DetailInfoRow("标志", it.joinToString(", ")) }
        if (channelDetail.description.isNotEmpty()) {
            Spacer(Modifier.height(UiTokens.Spacing.Small))
            Text("描述", style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
            Text(channelDetail.description, style = MaterialTheme.typography.bodySmall)
        }
    }
}

private fun codecName(codec: Int): String = when (codec) {
    0 -> "Speex Narrowband"; 1 -> "Speex Wideband"; 2 -> "Speex Ultra-Wideband"; 3 -> "CELT Mono"; 4 -> "Opus Voice"; 5 -> "Opus Music"; else -> "Unknown ($codec)"
}
