package club.clubk.ktag.apps.mine.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import club.clubk.ktag.apps.core.ui.theme.KTagBlue
import club.clubk.ktag.apps.core.ui.theme.KTagPurple
import club.clubk.ktag.apps.core.ui.theme.KTagRed
import club.clubk.ktag.apps.core.DeviceModel
import club.clubk.ktag.apps.core.ui.RenameDeviceDialog
import club.clubk.ktag.apps.mine.MineState
import club.clubk.ktag.apps.mine.MineViewModel
import club.clubk.ktag.apps.mine.TargetTeamSetting

@Composable
fun MineScreen(
    viewModel: MineViewModel,
    modifier: Modifier = Modifier
) {
    val devices = viewModel.devices
    val targetTeam = viewModel.targetTeamSetting
    val damage = viewModel.damage
    val minRssi = viewModel.minimumRssi
    val mineState = viewModel.mineState

    // Rename dialog state
    var deviceToRename by remember { mutableStateOf<DeviceModel?>(null) }

    val teamColor = when (targetTeam) {
        TargetTeamSetting.RED -> KTagRed
        TargetTeamSetting.BLUE -> KTagBlue
        TargetTeamSetting.ALL -> KTagPurple
    }

    // Rename dialog
    deviceToRename?.let { device ->
        RenameDeviceDialog(
            macAddress = device.bleAddress,
            currentName = device.name,
            onDismiss = { deviceToRename = null },
            onSave = { newName ->
                viewModel.renameDevice(device.bleAddress, newName)
                deviceToRename = null
            }
        )
    }

    Box(modifier = modifier.fillMaxSize()) {
        // Main content
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.Black)
        ) {
            // Status bar showing current settings
            MineStatusBar(
                targetTeam = targetTeam,
                damage = damage,
                minRssi = minRssi
            )

            // Device list area with "ARMED" watermark
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .weight(1f)
                    .background(Color.Black)
            ) {
                // Watermark text based on mine state
                when (mineState) {
                    is MineState.Armed -> {
                        Text(
                            text = "ARMED",
                            fontSize = 64.sp,
                            fontWeight = FontWeight.ExtraBold,
                            color = teamColor.copy(alpha = 0.3f),
                            modifier = Modifier.align(Alignment.Center)
                        )
                    }
                    is MineState.Rearming -> {
                        Text(
                            text = mineState.countdownSeconds.toString(),
                            fontSize = 120.sp,
                            fontWeight = FontWeight.ExtraBold,
                            color = teamColor.copy(alpha = 0.3f),
                            modifier = Modifier.align(Alignment.Center)
                        )
                    }
                    is MineState.Tagging -> {
                        // No watermark during explosion
                    }
                }

                // Device list
                LazyColumn(
                    modifier = Modifier.fillMaxSize()
                ) {
                    items(
                        items = devices,
                        key = { it.bleAddress }
                    ) { device ->
                        DeviceCard(
                            device = device,
                            mineState = mineState,
                            onClick = { viewModel.onDeviceClicked(device) },
                            onLongClick = { deviceToRename = device }
                        )
                    }
                }
            }
        }

        // Explosion animation overlay
        if (mineState is MineState.Tagging) {
            MineExplosionAnimation(
                onAnimationComplete = { viewModel.onExplosionAnimationComplete() }
            )
        }
    }
}

@Composable
fun MineStatusBar(
    targetTeam: TargetTeamSetting,
    damage: Int,
    minRssi: Int,
    modifier: Modifier = Modifier
) {
    val teamText = when (targetTeam) {
        TargetTeamSetting.RED -> "Red Team"
        TargetTeamSetting.BLUE -> "Blue Team"
        TargetTeamSetting.ALL -> "All Teams"
    }

    val teamColor = when (targetTeam) {
        TargetTeamSetting.RED -> KTagRed
        TargetTeamSetting.BLUE -> KTagBlue
        TargetTeamSetting.ALL -> KTagPurple
    }

    Column(
        modifier = modifier
            .fillMaxWidth()
            .background(Color(0xFF1A1A1A))
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Targeting: $teamText",
            style = MaterialTheme.typography.titleMedium,
            fontWeight = FontWeight.Bold,
            color = teamColor
        )
        Text(
            text = "Damage: $damage | Min RSSI: $minRssi dBm",
            style = MaterialTheme.typography.bodyMedium,
            color = Color.Gray
        )
    }
}
