package com.hermes.app.feature.round

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
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.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
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.graphics.Color
import androidx.compose.ui.unit.dp
import com.hermes.app.core.designsystem.HermesColors
import com.hermes.app.core.designsystem.HermesCountdownBadge
import com.hermes.app.core.designsystem.HermesCard
import com.hermes.app.core.designsystem.HermesMetricChip
import com.hermes.app.core.media.HermesPlayerCoordinator
import com.hermes.app.core.media.HermesVideoPlayerView
import com.hermes.app.feature.reveal.RevealPanel
import com.hermes.app.feature.result.ResultPanel
import com.hermes.app.feature.selection.SelectionPanel
import com.hermes.app.core.designsystem.HermesPrimaryButton

@Composable
fun RoundScreen(
    uiState: RoundUiState,
    playerCoordinator: HermesPlayerCoordinator,
    onSelectOutcome: (String) -> Unit,
    onConfirmSelection: () -> Unit,
    onRevealComplete: () -> Unit,
    onNextRound: () -> Unit,
    onRetry: () -> Unit,
    modifier: Modifier = Modifier,
) {
    HermesCard(modifier = modifier, elevated = true) {
        Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {
            com.hermes.app.core.designsystem.HermesSectionHeader(title = uiState.title, subtitle = uiState.subtitle)

            when {
                uiState.isLoading && !uiState.hasRound -> RoundLoadingState(
                    title = uiState.phaseLabel,
                    subtitle = uiState.subtitle,
                )

                !uiState.hasRound && uiState.bannerMessage != null -> RoundErrorState(
                    message = uiState.bannerMessage,
                    retryText = uiState.retryText,
                    onRetry = onRetry,
                )

                else -> {
                    if (uiState.bannerMessage != null) {
                        RoundBanner(message = uiState.bannerMessage)
                    }

                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(220.dp),
                    ) {
                        if (uiState.hasRound) {
                            HermesVideoPlayerView(
                                coordinator = playerCoordinator,
                                modifier = Modifier.fillMaxSize(),
                            )
                        } else {
                            RoundLoadingState(
                                title = uiState.phaseLabel,
                                subtitle = uiState.subtitle,
                            )
                        }

                        if (uiState.hasRound) {
                            Column(
                                modifier = Modifier
                                    .align(Alignment.TopEnd)
                                    .padding(12.dp),
                                verticalArrangement = Arrangement.spacedBy(10.dp),
                            ) {
                                HermesCountdownBadge(
                                    label = uiState.countdownLabel,
                                    value = uiState.countdownText,
                                    warning = uiState.countdownWarning,
                                )

                                HermesMetricChip(
                                    label = uiState.oddsLabel,
                                    value = uiState.oddsValue,
                                    modifier = Modifier.widthIn(min = 128.dp),
                                )
                            }
                        }

                        Text(
                            text = uiState.phaseLabel,
                            modifier = Modifier
                                .align(Alignment.BottomStart)
                                .padding(12.dp)
                                .background(
                                    color = Color.Black.copy(alpha = 0.35f),
                                    shape = RoundedCornerShape(999.dp),
                                )
                                .padding(horizontal = 12.dp, vertical = 8.dp),
                            style = MaterialTheme.typography.labelMedium,
                            color = HermesColors.textPrimary,
                        )
                    }

                    when (uiState.phase) {
                        RoundPhase.PREVIEW, RoundPhase.LOCKED -> SelectionPanel(
                            statusText = uiState.selectionStatusText,
                            options = uiState.selectionOptions,
                            selectedOptionId = uiState.selectedOutcomeId,
                            isLocked = uiState.selectionLocked,
                            confirmTitle = uiState.confirmTitle,
                            onSelect = { onSelectOutcome(it.id) },
                            onConfirm = onConfirmSelection,
                        )

                        RoundPhase.REVEAL -> RevealPanel(
                            title = uiState.revealTitle,
                            subtitle = uiState.revealSubtitle,
                            statusText = uiState.revealStatusText,
                            selectionLabel = uiState.revealSelectionLabel,
                            selectionValue = uiState.selectedOutcomeTitle,
                            continueTitle = uiState.revealContinueTitle,
                            onContinue = onRevealComplete,
                        )

                        RoundPhase.RESULT -> ResultPanel(
                            title = uiState.resultTitle,
                            subtitle = uiState.resultSubtitle,
                            selectionLabel = uiState.resultSelectionLabel,
                            selectionValue = uiState.selectedOutcomeTitle,
                            outcomeLabel = uiState.resultOutcomeLabel,
                            outcomeValue = uiState.winningOutcomeTitle,
                            didWin = uiState.didWin,
                            winLabel = uiState.resultWinLabel,
                            loseLabel = uiState.resultLoseLabel,
                            nextRoundTitle = uiState.resultNextRoundTitle,
                            onNextRound = onNextRound,
                        )
                    }
                }
            }
        }
    }
}

@Composable
private fun RoundLoadingState(
    title: String,
    subtitle: String,
) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(220.dp)
            .clip(RoundedCornerShape(28.dp))
            .background(HermesColors.surfaceElevated)
            .padding(20.dp),
        contentAlignment = Alignment.CenterStart,
    ) {
        Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
            Text(
                text = title,
                style = MaterialTheme.typography.headlineSmall,
                color = HermesColors.textPrimary,
            )
            Text(
                text = subtitle,
                style = MaterialTheme.typography.bodyMedium,
                color = HermesColors.textSecondary,
            )
        }
    }
}

@Composable
private fun RoundErrorState(
    message: String,
    retryText: String,
    onRetry: () -> Unit,
) {
    Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
        Text(
            text = message,
            style = MaterialTheme.typography.bodyMedium,
            color = HermesColors.warning,
        )

        HermesPrimaryButton(text = retryText, onClick = onRetry)
    }
}

@Composable
private fun RoundBanner(message: String) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(16.dp))
            .background(HermesColors.warning.copy(alpha = 0.16f))
            .padding(horizontal = 14.dp, vertical = 10.dp),
    ) {
        Text(
            text = message,
            style = MaterialTheme.typography.bodySmall,
            color = HermesColors.warning,
        )
    }
}
