package com.tsmobile.app.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ExpandLess
import androidx.compose.material.icons.filled.Mic
import androidx.compose.material.icons.filled.MicOff
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.tsmobile.app.data.VoiceState
import com.tsmobile.app.ui.theme.UiTokens
import com.tsmobile.app.ui.theme.semanticColors
import com.tsmobile.app.viewmodel.VoiceViewModel

/**
 * 底部语音控制栏。
 * 包含：静音按钮 + PTT 按钮 + 展开语音卡按钮。
 *
 * @param leftIndent 左侧内容缩进
 * @param rightIndent 右侧内容缩进
 */
@Composable
fun VoiceControlBar(
    voiceViewModel: VoiceViewModel,
    onExpand: () -> Unit,
    leftIndent: Dp = 0.dp,
    rightIndent: Dp = 0.dp,
) {
    val isMuted by voiceViewModel.isMuted.collectAsState()
    val isTransmitting by voiceViewModel.isTransmitting.collectAsState()
    val isContinuousMicEnabled by voiceViewModel.isContinuousMicEnabled.collectAsState()
    val voiceState by voiceViewModel.voiceState.collectAsState()
    val colors = MaterialTheme.colorScheme
    val semanticColors = MaterialTheme.semanticColors

    val pttEnabled = voiceState is VoiceState.Idle ||
        voiceState is VoiceState.Transmitting

    Surface(
        modifier = Modifier.fillMaxWidth(),
        color = colors.surfaceContainer,
        contentColor = colors.onSurface,
        tonalElevation = UiTokens.Elevation.Subtle,
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .heightIn(min = UiTokens.Size.AppBarHeight)
                .padding(vertical = UiTokens.Spacing.Small),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Spacer(modifier = Modifier.width(leftIndent))
            IconButton(onClick = { voiceViewModel.toggleMute() }) {
                Icon(
                    imageVector = if (isTransmitting || !isMuted) Icons.Default.Mic else Icons.Default.MicOff,
                    contentDescription = when {
                        isContinuousMicEnabled -> "关闭常驻麦克风"
                        isTransmitting -> "PTT 正在发言"
                        else -> "开启常驻麦克风"
                    },
                    tint = when {
                        isTransmitting -> semanticColors.success
                        isMuted -> semanticColors.warning
                        else -> colors.onSurfaceVariant
                    },
                )
            }

            PTTButton(
                isTransmitting = isTransmitting && !isContinuousMicEnabled,
                onStartTransmit = { voiceViewModel.startTransmit() },
                onStopTransmit = { voiceViewModel.stopTransmit() },
                enabled = pttEnabled && !isContinuousMicEnabled,
                modifier = Modifier.weight(1f),
            )

            IconButton(onClick = onExpand) {
                Icon(
                    Icons.Default.ExpandLess,
                    contentDescription = "展开语音卡",
                    tint = colors.onSurfaceVariant,
                )
            }
            Spacer(modifier = Modifier.width(rightIndent))
        }
    }

    if (voiceState is VoiceState.Blocked) {
        Text(
            text = (voiceState as VoiceState.Blocked).reason,
            style = MaterialTheme.typography.bodySmall,
            color = colors.onErrorContainer,
            modifier = Modifier
                .fillMaxWidth()
                .background(colors.errorContainer)
                .padding(horizontal = UiTokens.Spacing.Large, vertical = UiTokens.Spacing.ExtraSmall),
        )
    }
}
