package com.tsmobile.app.ui.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Block
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.Fingerprint
import androidx.compose.material.icons.filled.RemoveCircle
import androidx.compose.material.icons.filled.TouchApp
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.tsmobile.app.data.ClientInfo
import com.tsmobile.app.ui.theme.UiTokens
import com.tsmobile.app.ui.theme.semanticColors

/**
 * 成员操作菜单。
 *
 * 基础操作：Poke、复制昵称、复制 UID
 * 管理员操作：踢出频道、踢出服务器、封禁、移动（需权限）
 */
@Composable
fun ClientActionMenu(
    client: ClientInfo,
    isSelf: Boolean,
    onPoke: () -> Unit,
    onCopyNickname: () -> Unit,
    onCopyUID: () -> Unit,
    onKickFromServer: () -> Unit,
    onKickFromChannel: () -> Unit,
    onDismiss: () -> Unit,
) {
    AlertDialog(
        onDismissRequest = onDismiss,
        shape = MaterialTheme.shapes.extraLarge,
        containerColor = MaterialTheme.colorScheme.surfaceContainerHigh,
        tonalElevation = UiTokens.Elevation.Raised,
        titleContentColor = MaterialTheme.colorScheme.onSurface,
        textContentColor = MaterialTheme.colorScheme.onSurfaceVariant,
        title = {
            Column {
                Text(
                    text = client.nickname,
                    style = MaterialTheme.typography.titleMedium,
                )
                Text(
                    text = "UID: ${client.uid}",
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                )
            }
        },
        confirmButton = {},
        dismissButton = {},
        text = {
            Column {
                // Poke
                if (!isSelf) {
                    ListItem(
                        headlineContent = { Text("Poke") },
                        leadingContent = {
                            Icon(
                                imageVector = Icons.Default.TouchApp,
                                contentDescription = null,
                                tint = MaterialTheme.semanticColors.info,
                            )
                        },
                        modifier = Modifier.clickable {
                            onPoke()
                            onDismiss()
                        },
                    )
                }

                // 复制昵称
                ListItem(
                    headlineContent = { Text("复制昵称") },
                    leadingContent = {
                        Icon(
                            imageVector = Icons.Default.ContentCopy,
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.onSurfaceVariant,
                        )
                    },
                    modifier = Modifier.clickable {
                        onCopyNickname()
                        onDismiss()
                    },
                )

                // 复制 UID
                ListItem(
                    headlineContent = { Text("复制 UID") },
                    leadingContent = {
                        Icon(
                            imageVector = Icons.Default.Fingerprint,
                            contentDescription = null,
                            tint = MaterialTheme.colorScheme.onSurfaceVariant,
                        )
                    },
                    modifier = Modifier.clickable {
                        onCopyUID()
                        onDismiss()
                    },
                )

                // 管理员操作（非自己时显示）
                if (!isSelf) {
                    HorizontalDivider(
                        modifier = Modifier.padding(vertical = UiTokens.Spacing.Small),
                        color = MaterialTheme.colorScheme.outlineVariant,
                    )

                    ListItem(
                        headlineContent = {
                            Text("踢出频道", color = MaterialTheme.colorScheme.error)
                        },
                        leadingContent = {
                            Icon(
                                imageVector = Icons.Default.RemoveCircle,
                                contentDescription = null,
                                tint = MaterialTheme.colorScheme.error,
                            )
                        },
                        modifier = Modifier.clickable {
                            onKickFromChannel()
                            onDismiss()
                        },
                    )

                    ListItem(
                        headlineContent = {
                            Text("踢出服务器", color = MaterialTheme.colorScheme.error)
                        },
                        leadingContent = {
                            Icon(
                                imageVector = Icons.Default.Block,
                                contentDescription = null,
                                tint = MaterialTheme.colorScheme.error,
                            )
                        },
                        modifier = Modifier.clickable {
                            onKickFromServer()
                            onDismiss()
                        },
                    )
                }
            }
        },
    )
}
