package com.hermes.app.feature.feed

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.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
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.Brush
import androidx.compose.ui.unit.dp
import com.hermes.app.core.designsystem.HermesCard
import com.hermes.app.core.designsystem.HermesColors
import com.hermes.app.core.designsystem.HermesMetricChip
import com.hermes.app.core.designsystem.HermesPrimaryButton
import com.hermes.app.core.designsystem.HermesSectionHeader

@Composable
fun FeedScreen(
    uiState: FeedUiState,
    onWatchPreview: () -> Unit,
    onRetry: () -> Unit,
    modifier: Modifier = Modifier,
) {
    HermesCard(modifier = modifier, elevated = true) {
        Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {
            HermesSectionHeader(title = uiState.title, subtitle = uiState.subtitle)

            when {
                uiState.isLoading && !uiState.hasRound -> FeedLoadingState(
                    title = uiState.heroTitle,
                    subtitle = uiState.heroSubtitle,
                )
                !uiState.hasRound && uiState.bannerMessage != null -> FeedErrorState(
                    message = uiState.bannerMessage,
                    onRetry = onRetry,
                    retryText = uiState.retryText,
                )
                else -> {
                    if (uiState.bannerMessage != null) {
                        FeedBanner(message = uiState.bannerMessage)
                    }

                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(220.dp)
                            .clip(RoundedCornerShape(28.dp))
                            .background(
                                Brush.linearGradient(
                                    colors = listOf(HermesColors.surfaceElevated, HermesColors.background),
                                ),
                            )
                            .padding(20.dp),
                        contentAlignment = Alignment.BottomStart,
                    ) {
                        Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
                            Text(
                                text = uiState.heroTitle,
                                style = MaterialTheme.typography.headlineSmall,
                                color = HermesColors.textPrimary,
                            )
                            Text(
                                text = uiState.heroSubtitle,
                                style = MaterialTheme.typography.bodyMedium,
                                color = HermesColors.textSecondary,
                            )
                        }
                    }

                    Row(horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.fillMaxWidth()) {
                        HermesMetricChip(label = uiState.lockLabel, value = uiState.lockValue, modifier = Modifier.fillMaxWidth(0.5f))
                        HermesMetricChip(label = uiState.oddsLabel, value = uiState.oddsValue, modifier = Modifier.fillMaxWidth(0.5f))
                    }

                    HermesPrimaryButton(text = uiState.ctaText, onClick = onWatchPreview, enabled = uiState.hasRound)
                }
            }
        }
    }
}

@Composable
private fun FeedLoadingState(
    title: String,
    subtitle: String,
) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(220.dp)
            .clip(RoundedCornerShape(28.dp))
            .background(
                Brush.linearGradient(
                    colors = listOf(HermesColors.surfaceElevated, HermesColors.background),
                ),
            )
            .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 FeedErrorState(
    message: String,
    onRetry: () -> Unit,
    retryText: String,
) {
    Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
        Text(
            text = message,
            style = MaterialTheme.typography.bodyMedium,
            color = HermesColors.warning,
        )

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

@Composable
private fun FeedBanner(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,
        )
    }
}
