package com.tsmobile.app.ui.components

import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.NewReleases
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.tsmobile.app.ui.theme.UiTokens
import com.tsmobile.app.ui.theme.semanticColors

/**
 * 更新横幅 — 显示在服务器配置页顶部。
 *
 * 当检测到新版本时显示，提供"下载更新"和"忽略此版本"两个操作。
 */
@Composable
fun UpdateBanner(
    versionName: String,
    releaseUrl: String,
    onDownload: (String) -> Unit,
    onDismiss: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val semanticColors = MaterialTheme.semanticColors
    Surface(
        modifier = modifier.fillMaxWidth(),
        color = semanticColors.infoContainer,
        contentColor = semanticColors.onInfoContainer,
        tonalElevation = UiTokens.Elevation.Subtle,
        shape = MaterialTheme.shapes.medium,
    ) {
        Column(
            modifier = Modifier.padding(
                horizontal = UiTokens.Spacing.Large,
                vertical = UiTokens.Spacing.Medium,
            ),
        ) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Icon(
                    imageVector = Icons.Default.NewReleases,
                    contentDescription = null,
                    tint = semanticColors.info,
                    modifier = Modifier.size(UiTokens.Size.IconMedium),
                )
                Spacer(Modifier.width(UiTokens.Spacing.Small))
                Text(
                    text = "发现新版本",
                    style = MaterialTheme.typography.labelLarge,
                )
            }

            Spacer(Modifier.height(UiTokens.Spacing.ExtraSmall))
            Text(
                text = "新版本 $versionName 已发布，点击下载更新",
                style = MaterialTheme.typography.bodySmall,
            )

            Spacer(Modifier.height(UiTokens.Spacing.Small))
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.End,
            ) {
                TextButton(
                    onClick = onDismiss,
                    colors = ButtonDefaults.textButtonColors(
                        contentColor = semanticColors.onInfoContainer.copy(alpha = 0.7f),
                    ),
                ) {
                    Text("忽略此版本")
                }
                Spacer(Modifier.width(UiTokens.Spacing.Small))
                TextButton(onClick = { onDownload(releaseUrl) }) {
                    Text("下载更新")
                }
            }
        }
    }
}
