package com.wisp.app.ui.component

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
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.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.automirrored.outlined.Message

import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.wisp.app.R
import com.wisp.app.repo.FiatPreferences
import com.wisp.app.repo.ZapPreferences
import com.wisp.app.repo.ZapPreset
import com.wisp.app.ui.theme.WispThemeColors
import com.wisp.app.ui.util.AmountFormatter
import androidx.compose.runtime.collectAsState
import kotlin.math.sin
import kotlin.random.Random

private val LightningYellow: Color
    @Composable get() = WispThemeColors.zapColor

private val LightningOrange: Color
    @Composable get() = WispThemeColors.zapColor

private val LightningAmber: Color
    @Composable get() = WispThemeColors.zapColor.copy(alpha = 0.7f)

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun ZapDialog(
    isWalletConnected: Boolean,
    onDismiss: () -> Unit,
    onZap: (amountMsats: Long, message: String, isAnonymous: Boolean, isPrivate: Boolean) -> Unit,
    onGoToWallet: () -> Unit,
    canPrivateZap: Boolean = false,
    /** When opening from a quick preset (e.g. chat actions sheet), pre-select that amount in sats. */
    initialSatsHint: Int? = null
) {
    if (!isWalletConnected) {
        AlertDialog(
            onDismissRequest = onDismiss,
            title = { Text(stringResource(R.string.zap_wallet_not_connected)) },
            text = { Text(stringResource(R.string.zap_connect_wallet)) },
            confirmButton = {
                TextButton(onClick = {
                    onDismiss()
                    onGoToWallet()
                }) {
                    Text(stringResource(R.string.btn_go_to_wallet))
                }
            },
            dismissButton = {
                TextButton(onClick = onDismiss) { Text(stringResource(R.string.btn_cancel)) }
            }
        )
        return
    }

    val context = LocalContext.current
    val fiatPrefs = remember { FiatPreferences.get(context) }
    val fiatMode by fiatPrefs.fiatMode.collectAsState()
    @Suppress("unused_variable") val fiatCurrency by fiatPrefs.currency.collectAsState()
    var presets by remember { mutableStateOf(ZapPreferences(context).getPresets().sortedBy { it.amountSats }) }
    var selectedPreset by remember { mutableStateOf<ZapPreset?>(presets.firstOrNull()) }
    var isCustom by remember { mutableStateOf(false) }
    var customAmount by remember { mutableStateOf("") }
    var message by remember { mutableStateOf("") }
    var isAnonymous by remember { mutableStateOf(false) }
    var isPrivate by remember { mutableStateOf(false) }
    var editMode by remember { mutableStateOf(false) }

    LaunchedEffect(initialSatsHint) {
        val hint = initialSatsHint ?: return@LaunchedEffect
        val h = hint.toLong().coerceAtLeast(1L)
        val fromPrefs = ZapPreferences(context).getPresets().sortedBy { it.amountSats }
        val match = fromPrefs.find { it.amountSats == h }
        if (match != null) {
            selectedPreset = match
            isCustom = false
            message = match.message
        } else {
            isCustom = true
            customAmount = h.toString()
            message = ""
        }
    }

    val effectiveAmount = if (isCustom) {
        customAmount.toLongOrNull() ?: 0L
    } else {
        selectedPreset?.amountSats ?: 0L
    }

    val effectiveMessage = if (isCustom) message else (selectedPreset?.message ?: "")

    Dialog(
        onDismissRequest = onDismiss,
        properties = DialogProperties(usePlatformDefaultWidth = false)
    ) {
        Surface(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 24.dp),
            shape = RoundedCornerShape(28.dp),
            color = WispThemeColors.backgroundColor,
            tonalElevation = 8.dp
        ) {
            Box {
                // Background lightning effect
                LightningBackground(
                    modifier = Modifier
                        .matchParentSize()
                        .clip(RoundedCornerShape(28.dp))
                )

                Column(
                    modifier = Modifier.padding(24.dp),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    // Header with animated bolt
                    AnimatedBoltHeader()

                    Spacer(Modifier.height(8.dp))

                    Text(
                        text = stringResource(
                            if (fiatMode) R.string.zap_send_money else R.string.zap_send
                        ),
                        style = MaterialTheme.typography.headlineSmall,
                        fontWeight = FontWeight.Bold,
                        color = MaterialTheme.colorScheme.onSurface
                    )

                    Spacer(Modifier.height(4.dp))

                    // Amount display — tap to edit directly
                    if (isCustom) {
                        BasicTextField(
                            value = customAmount,
                            onValueChange = { customAmount = it.filter { c -> c.isDigit() } },
                            textStyle = MaterialTheme.typography.displaySmall.copy(
                                fontWeight = FontWeight.Bold,
                                color = LightningOrange,
                                textAlign = TextAlign.Center
                            ),
                            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                            singleLine = true,
                            cursorBrush = SolidColor(LightningOrange),
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(vertical = 4.dp),
                            decorationBox = { inner ->
                                Box(contentAlignment = Alignment.Center) {
                                    if (customAmount.isEmpty()) {
                                        Text(
                                            text = "0",
                                            style = MaterialTheme.typography.displaySmall,
                                            fontWeight = FontWeight.Bold,
                                            color = LightningOrange.copy(alpha = 0.3f)
                                        )
                                    }
                                    inner()
                                }
                            }
                        )
                        if (!fiatMode) {
                            Text(
                                text = stringResource(R.string.zap_sats),
                                style = MaterialTheme.typography.labelLarge,
                                color = LightningOrange.copy(alpha = 0.7f)
                            )
                        }
                    } else if (effectiveAmount > 0) {
                        Text(
                            text = AmountFormatter.formatShort(effectiveAmount, context),
                            style = MaterialTheme.typography.displaySmall,
                            fontWeight = FontWeight.Bold,
                            color = LightningOrange,
                            modifier = Modifier
                                .clickable {
                                    customAmount = effectiveAmount.toString()
                                    isCustom = true
                                }
                                .padding(vertical = 4.dp)
                        )
                        if (!fiatMode) {
                            Text(
                                text = stringResource(R.string.zap_sats),
                                style = MaterialTheme.typography.labelLarge,
                                color = LightningOrange.copy(alpha = 0.7f)
                            )
                        }
                    }

                    Spacer(Modifier.height(16.dp))

                    // Preset chips header
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text(
                            text = stringResource(R.string.zap_quick_amounts),
                            style = MaterialTheme.typography.labelMedium,
                            color = MaterialTheme.colorScheme.onSurfaceVariant
                        )
                        Row {
                            if (editMode) {
                                TextButton(onClick = { editMode = false }) {
                                    Text(stringResource(R.string.btn_done), color = LightningOrange, fontSize = 12.sp)
                                }
                            } else {
                                IconButton(
                                    onClick = { editMode = true },
                                    modifier = Modifier.size(32.dp)
                                ) {
                                    Text(
                                        stringResource(R.string.btn_edit),
                                        style = MaterialTheme.typography.labelSmall,
                                        color = MaterialTheme.colorScheme.onSurfaceVariant
                                    )
                                }
                            }
                        }
                    }

                    Spacer(Modifier.height(8.dp))

                    // Preset amount chips
                    FlowRow(
                        horizontalArrangement = Arrangement.spacedBy(8.dp),
                        verticalArrangement = Arrangement.spacedBy(8.dp),
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        presets.forEach { preset ->
                            ZapPresetChip(
                                preset = preset,
                                isSelected = !isCustom && selectedPreset == preset,
                                editMode = editMode,
                                onClick = {
                                    if (!editMode) {
                                        selectedPreset = preset
                                        isCustom = false
                                        message = preset.message
                                    }
                                },
                                onRemove = {
                                    presets = ZapPreferences(context).removePreset(preset)
                                    if (selectedPreset == preset) {
                                        selectedPreset = presets.firstOrNull()
                                    }
                                }
                            )
                        }

                        // Custom amount chip
                        ZapChipButton(
                            label = stringResource(R.string.btn_custom),
                            isSelected = isCustom,
                            onClick = { isCustom = true }
                        )
                    }

                    Spacer(Modifier.height(16.dp))

                    // Custom amount input
                    AnimatedVisibility(
                        visible = isCustom,
                        enter = expandVertically() + fadeIn(),
                        exit = shrinkVertically() + fadeOut()
                    ) {
                        Column {
                            OutlinedTextField(
                                value = customAmount,
                                onValueChange = { customAmount = it.filter { c -> c.isDigit() } },
                                label = { Text(stringResource(R.string.placeholder_amount_sats)) },
                                keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                                modifier = Modifier.fillMaxWidth(),
                                singleLine = true,
                                shape = RoundedCornerShape(16.dp),
                                colors = OutlinedTextFieldDefaults.colors(
                                    focusedBorderColor = LightningOrange,
                                    focusedLabelColor = LightningOrange,
                                    cursorColor = LightningOrange
                                )
                            )
                            val saveAmount = customAmount.toLongOrNull() ?: 0L
                            if (saveAmount > 0) {
                                Spacer(Modifier.height(6.dp))
                                TextButton(
                                    onClick = {
                                        val preset = ZapPreset(saveAmount, message.trim())
                                        presets = ZapPreferences(context).addPreset(preset)
                                        selectedPreset = presets.firstOrNull { it.amountSats == saveAmount }
                                        isCustom = false
                                        customAmount = ""
                                    }
                                ) {
                                    Icon(
                                        Icons.Filled.Add,
                                        contentDescription = null,
                                        modifier = Modifier.size(16.dp)
                                    )
                                    Spacer(Modifier.width(4.dp))
                                    Text(stringResource(R.string.btn_save))
                                }
                            }
                            Spacer(Modifier.height(8.dp))
                        }
                    }

                    // Message input
                    OutlinedTextField(
                        value = message,
                        onValueChange = { message = it },
                        label = { Text(stringResource(R.string.placeholder_message_optional)) },
                        modifier = Modifier.fillMaxWidth(),
                        singleLine = true,
                        shape = RoundedCornerShape(16.dp),
                        colors = OutlinedTextFieldDefaults.colors(
                            focusedBorderColor = LightningOrange,
                            focusedLabelColor = LightningOrange,
                            cursorColor = LightningOrange
                        )
                    )

                    // Clear message when switching away from a preset with a saved message
                    // (message is pre-filled when selecting a preset with a message)

                    Spacer(Modifier.height(16.dp))

                    // Anonymous toggle
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text(
                            text = stringResource(R.string.btn_anonymous),
                            style = MaterialTheme.typography.bodyMedium,
                            color = MaterialTheme.colorScheme.onSurface
                        )
                        Switch(
                            checked = isAnonymous,
                            onCheckedChange = {
                                isAnonymous = it
                                if (it) isPrivate = false
                            },
                            colors = SwitchDefaults.colors(
                                checkedThumbColor = LightningOrange,
                                checkedTrackColor = LightningOrange.copy(alpha = 0.5f),
                                uncheckedThumbColor = MaterialTheme.colorScheme.onSurfaceVariant,
                                uncheckedTrackColor = MaterialTheme.colorScheme.surfaceVariant,
                                uncheckedBorderColor = MaterialTheme.colorScheme.outline
                            )
                        )
                    }

                    // Private toggle
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Column {
                            Text(
                                text = stringResource(R.string.btn_private),
                                style = MaterialTheme.typography.bodyMedium,
                                color = if (canPrivateZap) MaterialTheme.colorScheme.onSurface
                                else MaterialTheme.colorScheme.onSurface.copy(alpha = 0.4f)
                            )
                            if (!canPrivateZap) {
                                Text(
                                    text = stringResource(R.string.zap_both_parties_need_dm_relays),
                                    style = MaterialTheme.typography.labelSmall,
                                    color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.5f)
                                )
                            }
                        }
                        Switch(
                            checked = isPrivate,
                            onCheckedChange = {
                                isPrivate = it
                                if (it) isAnonymous = false
                            },
                            enabled = canPrivateZap,
                            colors = SwitchDefaults.colors(
                                checkedThumbColor = LightningOrange,
                                checkedTrackColor = LightningOrange.copy(alpha = 0.5f),
                                uncheckedThumbColor = MaterialTheme.colorScheme.onSurfaceVariant,
                                uncheckedTrackColor = MaterialTheme.colorScheme.surfaceVariant,
                                uncheckedBorderColor = MaterialTheme.colorScheme.outline
                            )
                        )
                    }

                    Spacer(Modifier.height(16.dp))

                    // Action buttons
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.spacedBy(12.dp)
                    ) {
                        TextButton(
                            onClick = onDismiss,
                            modifier = Modifier.weight(1f)
                        ) {
                            Text(stringResource(R.string.btn_cancel))
                        }

                        Button(
                            onClick = {
                                onZap(effectiveAmount * 1000, effectiveMessage.ifEmpty { message }, isAnonymous, isPrivate)
                            },
                            enabled = effectiveAmount > 0,
                            modifier = Modifier.weight(2f),
                            colors = ButtonDefaults.buttonColors(
                                containerColor = LightningOrange,
                                contentColor = Color.White
                            ),
                            shape = RoundedCornerShape(16.dp)
                        ) {
                            Icon(
                                painter = painterResource(R.drawable.ic_bolt),
                                contentDescription = null,
                                modifier = Modifier.size(15.dp)
                            )
                            Spacer(Modifier.width(6.dp))
                            Text(
                                if (fiatMode) {
                                    stringResource(
                                        R.string.zap_x_amount,
                                        AmountFormatter.formatShort(effectiveAmount, context)
                                    )
                                } else {
                                    stringResource(R.string.zap_x_sats, effectiveAmount)
                                },
                                fontWeight = FontWeight.Bold
                            )
                        }
                    }
                }
            }
        }
    }

}

@Composable
private fun AnimatedBoltHeader() {
    val infiniteTransition = rememberInfiniteTransition(label = "bolt")

    val glowAlpha by infiniteTransition.animateFloat(
        initialValue = 0.3f,
        targetValue = 0.8f,
        animationSpec = infiniteRepeatable(
            animation = tween(1200, easing = LinearEasing),
            repeatMode = RepeatMode.Reverse
        ),
        label = "glow"
    )

    val boltScale by animateFloatAsState(
        targetValue = 1f,
        animationSpec = spring(
            dampingRatio = Spring.DampingRatioMediumBouncy,
            stiffness = Spring.StiffnessLow
        ),
        label = "boltScale"
    )

    val zapColor = WispThemeColors.zapColor

    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier.size(64.dp)
    ) {
        // Glow circle behind bolt
        Box(
            modifier = Modifier
                .size(56.dp)
                .alpha(glowAlpha)
                .background(
                    brush = Brush.radialGradient(
                        colors = listOf(
                            zapColor.copy(alpha = 0.4f),
                            zapColor.copy(alpha = 0.1f),
                            Color.Transparent
                        )
                    ),
                    shape = CircleShape
                )
        )

        // Bolt icon
        Icon(
            painter = painterResource(R.drawable.ic_bolt),
            contentDescription = null,
            tint = zapColor,
            modifier = Modifier
                .size(30.dp)
                .scale(boltScale)
        )
    }
}

@Composable
private fun LightningBackground(modifier: Modifier = Modifier) {
    val infiniteTransition = rememberInfiniteTransition(label = "lightning_bg")

    val phase by infiniteTransition.animateFloat(
        initialValue = 0f,
        targetValue = 1f,
        animationSpec = infiniteRepeatable(
            animation = tween(4000, easing = LinearEasing),
            repeatMode = RepeatMode.Restart
        ),
        label = "phase"
    )

    val zapColor = WispThemeColors.zapColor

    // Stable random values for bolt positions
    val boltData = remember {
        List(5) { i ->
            Triple(
                Random(i * 42).nextFloat(),       // x position (0..1)
                Random(i * 42 + 1).nextFloat(),    // y position (0..1)
                Random(i * 42 + 2).nextFloat() * 0.3f + 0.1f  // size scale
            )
        }
    }

    Canvas(modifier = modifier) {
        val w = size.width
        val h = size.height

        // Subtle gradient overlay at the top
        drawRect(
            brush = Brush.verticalGradient(
                colors = listOf(
                    zapColor.copy(alpha = 0.03f),
                    Color.Transparent
                ),
                startY = 0f,
                endY = h * 0.4f
            )
        )

        // Animated mini lightning bolts scattered in background
        boltData.forEach { (xFrac, yFrac, scale) ->
            val animatedAlpha = (sin((phase + xFrac) * Math.PI * 2).toFloat() * 0.5f + 0.5f) * 0.06f
            val cx = xFrac * w
            val cy = yFrac * h
            val boltSize = 20f * scale

            drawMiniBolt(
                center = Offset(cx, cy),
                size = boltSize,
                color = zapColor.copy(alpha = animatedAlpha)
            )
        }
    }
}

private fun DrawScope.drawMiniBolt(center: Offset, size: Float, color: Color) {
    val path = Path().apply {
        moveTo(center.x, center.y - size)
        lineTo(center.x - size * 0.4f, center.y + size * 0.1f)
        lineTo(center.x + size * 0.1f, center.y + size * 0.1f)
        lineTo(center.x - size * 0.1f, center.y + size)
        lineTo(center.x + size * 0.4f, center.y - size * 0.1f)
        lineTo(center.x - size * 0.1f, center.y - size * 0.1f)
        close()
    }
    drawPath(path, color, style = Fill)
}

@Composable
private fun ZapPresetChip(
    preset: ZapPreset,
    isSelected: Boolean,
    editMode: Boolean,
    onClick: () -> Unit,
    onRemove: () -> Unit
) {
    val scale by animateFloatAsState(
        targetValue = if (isSelected) 1.05f else 1f,
        animationSpec = spring(dampingRatio = Spring.DampingRatioMediumBouncy),
        label = "chipScale"
    )

    Box {
        Surface(
            modifier = Modifier
                .scale(scale)
                .clip(RoundedCornerShape(16.dp))
                .clickable(onClick = onClick),
            shape = RoundedCornerShape(16.dp),
            color = if (isSelected) LightningOrange else MaterialTheme.colorScheme.surfaceVariant,
            border = if (isSelected) {
                androidx.compose.foundation.BorderStroke(
                    1.5.dp,
                    Brush.linearGradient(listOf(LightningYellow, LightningOrange))
                )
            } else {
                null
            },
            shadowElevation = if (isSelected) 4.dp else 0.dp
        ) {
            Row(
                modifier = Modifier.padding(horizontal = 14.dp, vertical = 8.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                if (isSelected) {
                    Icon(
                        painter = painterResource(R.drawable.ic_bolt),
                        contentDescription = null,
                        modifier = Modifier.size(12.dp),
                        tint = Color.White
                    )
                    Spacer(Modifier.width(3.dp))
                }
                val chipContext = LocalContext.current
                Text(
                    text = AmountFormatter.formatShort(preset.amountSats, chipContext),
                    style = MaterialTheme.typography.labelLarge,
                    fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal,
                    color = if (isSelected) Color.White else MaterialTheme.colorScheme.onSurfaceVariant
                )
                if (preset.message.isNotEmpty()) {
                    Spacer(Modifier.width(4.dp))
                    Icon(
                        Icons.AutoMirrored.Outlined.Message,
                        contentDescription = null,
                        modifier = Modifier.size(10.dp),
                        tint = if (isSelected) Color.White.copy(alpha = 0.7f)
                        else MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.5f)
                    )
                }
            }
        }

        // Remove badge in edit mode
        if (editMode) {
            Surface(
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .offset(x = 4.dp, y = (-4).dp)
                    .size(20.dp)
                    .clip(CircleShape)
                    .clickable(onClick = onRemove),
                shape = CircleShape,
                color = MaterialTheme.colorScheme.error
            ) {
                Icon(
                    Icons.Filled.Close,
                    contentDescription = stringResource(R.string.btn_remove),
                    modifier = Modifier
                        .padding(2.dp)
                        .size(16.dp),
                    tint = Color.White
                )
            }
        }
    }
}

@Composable
private fun ZapChipButton(
    label: String,
    isSelected: Boolean,
    onClick: () -> Unit
) {
    Surface(
        modifier = Modifier
            .clip(RoundedCornerShape(16.dp))
            .clickable(onClick = onClick),
        shape = RoundedCornerShape(16.dp),
        color = if (isSelected) LightningOrange else MaterialTheme.colorScheme.surfaceVariant,
        border = if (isSelected) {
            androidx.compose.foundation.BorderStroke(
                1.5.dp,
                Brush.linearGradient(listOf(LightningYellow, LightningOrange))
            )
        } else {
            null
        }
    ) {
        Text(
            text = label,
            modifier = Modifier.padding(horizontal = 14.dp, vertical = 8.dp),
            style = MaterialTheme.typography.labelLarge,
            fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal,
            color = if (isSelected) Color.White else MaterialTheme.colorScheme.onSurfaceVariant
        )
    }
}

@Composable
private fun SaveZapPresetDialog(
    onSave: (ZapPreset) -> Unit,
    onDismiss: () -> Unit
) {
    var amount by remember { mutableStateOf("") }
    var presetMessage by remember { mutableStateOf("") }

    AlertDialog(
        onDismissRequest = onDismiss,
        title = {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Icon(
                    painter = painterResource(R.drawable.ic_bolt),
                    contentDescription = null,
                    tint = LightningOrange,
                    modifier = Modifier.size(20.dp)
                )
                Spacer(Modifier.width(8.dp))
                Text(stringResource(R.string.btn_save))
            }
        },
        text = {
            Column {
                OutlinedTextField(
                    value = amount,
                    onValueChange = { amount = it.filter { c -> c.isDigit() } },
                    label = { Text(stringResource(R.string.placeholder_amount_sats)) },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                    modifier = Modifier.fillMaxWidth(),
                    singleLine = true,
                    shape = RoundedCornerShape(12.dp),
                    colors = OutlinedTextFieldDefaults.colors(
                        focusedBorderColor = LightningOrange,
                        focusedLabelColor = LightningOrange,
                        cursorColor = LightningOrange
                    )
                )
                Spacer(Modifier.height(12.dp))
                OutlinedTextField(
                    value = presetMessage,
                    onValueChange = { presetMessage = it },
                    label = { Text(stringResource(R.string.placeholder_message_optional)) },
                    modifier = Modifier.fillMaxWidth(),
                    singleLine = true,
                    shape = RoundedCornerShape(12.dp),
                    colors = OutlinedTextFieldDefaults.colors(
                        focusedBorderColor = LightningOrange,
                        focusedLabelColor = LightningOrange,
                        cursorColor = LightningOrange
                    )
                )
            }
        },
        confirmButton = {
            Button(
                onClick = {
                    val sats = amount.toLongOrNull() ?: return@Button
                    onSave(ZapPreset(sats, presetMessage.trim()))
                },
                enabled = (amount.toLongOrNull() ?: 0L) > 0,
                colors = ButtonDefaults.buttonColors(containerColor = LightningOrange)
            ) {
                Text(stringResource(R.string.btn_save), fontWeight = FontWeight.Bold)
            }
        },
        dismissButton = {
            TextButton(onClick = onDismiss) { Text(stringResource(R.string.btn_cancel)) }
        }
    )
}

