package com.hermes.app.feature.selection

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.TouchApp
import androidx.compose.material.icons.outlined.RadioButtonUnchecked
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.hermes.app.core.designsystem.HermesColors
import com.hermes.app.core.designsystem.HermesPrimaryButton

data class SelectionOptionUi(
    val id: String,
    val title: String,
    val subtitle: String,
    val odds: String,
)

@Composable
fun SelectionPanel(
    statusText: String,
    options: List<SelectionOptionUi>,
    selectedOptionId: String?,
    isLocked: Boolean,
    confirmTitle: String,
    onSelect: (SelectionOptionUi) -> Unit,
    onConfirm: () -> Unit,
    modifier: Modifier = Modifier,
) {
    Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(12.dp)) {
        val statusColor = if (isLocked) HermesColors.warning else HermesColors.accent
        Row(
            modifier = Modifier
                .clip(RoundedCornerShape(999.dp))
                .background(statusColor.copy(alpha = 0.16f))
                .padding(horizontal = 12.dp, vertical = 8.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(8.dp),
        ) {
            Icon(
                imageVector = if (isLocked) Icons.Filled.Lock else Icons.Filled.TouchApp,
                contentDescription = null,
                tint = statusColor,
            )
            Text(
                text = statusText,
                color = statusColor,
                style = MaterialTheme.typography.labelMedium,
                fontWeight = FontWeight.SemiBold,
            )
        }

        Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
            options.forEach { option ->
                SelectionOptionButton(
                    option = option,
                    selected = selectedOptionId == option.id,
                    isLocked = isLocked,
                    onClick = { onSelect(option) },
                )
            }
        }

        HermesPrimaryButton(
            text = confirmTitle,
            onClick = onConfirm,
            enabled = !isLocked && selectedOptionId != null,
        )
    }
}

@Composable
private fun SelectionOptionButton(
    option: SelectionOptionUi,
    selected: Boolean,
    isLocked: Boolean,
    onClick: () -> Unit,
) {
    val containerColor = if (selected) HermesColors.accent else HermesColors.surfaceElevated
    val contentColor = if (selected) HermesColors.background else HermesColors.textPrimary
    val subtitleColor = if (selected) HermesColors.background.copy(alpha = 0.7f) else HermesColors.textSecondary

    Button(
        onClick = onClick,
        enabled = !isLocked,
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(18.dp),
        colors = ButtonDefaults.buttonColors(
            containerColor = containerColor,
            contentColor = contentColor,
            disabledContainerColor = HermesColors.surfaceElevated.copy(alpha = 0.65f),
            disabledContentColor = HermesColors.textTertiary,
        ),
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Column(modifier = Modifier.fillMaxWidth(0.72f), verticalArrangement = Arrangement.spacedBy(4.dp)) {
                Text(text = option.title, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
                Text(text = option.subtitle, style = MaterialTheme.typography.bodySmall, color = subtitleColor)
            }

            Column(horizontalAlignment = Alignment.End, verticalArrangement = Arrangement.spacedBy(4.dp)) {
                Text(text = option.odds, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.Bold)
                Icon(
                    imageVector = if (selected) Icons.Filled.CheckCircle else Icons.Outlined.RadioButtonUnchecked,
                    contentDescription = null,
                    tint = contentColor,
                )
            }
        }
    }
}
