package de.krisenvorrat.app.ui.warnings

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.Spacer
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.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
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.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import de.krisenvorrat.app.domain.model.ExpiryUrgency
import de.krisenvorrat.app.domain.model.ExpiryWarning
import java.util.Locale

@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun WarningsScreen(
    viewModel: WarningsViewModel = hiltViewModel()
) {
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()

    Column(modifier = Modifier.fillMaxSize()) {
        TopAppBar(title = { Text("Warnungen") })

        if (uiState.isLoading) {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                CircularProgressIndicator()
            }
        } else if (!uiState.hasWarnings) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(16.dp),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "Keine Warnungen vorhanden",
                    style = MaterialTheme.typography.bodyLarge,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
        } else {
            LazyColumn(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = 16.dp),
                verticalArrangement = Arrangement.spacedBy(12.dp)
            ) {
                item { Spacer(modifier = Modifier.height(4.dp)) }

                if (uiState.hasExpiryWarnings) {
                    item {
                        Text(
                            text = "Ablaufdaten (${uiState.expiryWarnings.size})",
                            style = MaterialTheme.typography.titleMedium,
                            modifier = Modifier.padding(top = 4.dp)
                        )
                    }
                    items(uiState.expiryWarnings) { warning ->
                        ExpiryWarningItem(warning)
                    }
                }

                item { Spacer(modifier = Modifier.height(8.dp)) }
            }
        }
    }
}

@Composable
private fun ExpiryWarningItem(warning: ExpiryWarning) {
    val containerColor = when (warning.urgency) {
        ExpiryUrgency.URGENT -> MaterialTheme.colorScheme.errorContainer
        ExpiryUrgency.WARNING -> Color(0xFFFFF3E0)
    }
    val contentColor = when (warning.urgency) {
        ExpiryUrgency.URGENT -> MaterialTheme.colorScheme.onErrorContainer
        ExpiryUrgency.WARNING -> Color(0xFFE65100)
    }
    val daysText = when {
        warning.daysUntilExpiry < 0 -> "abgelaufen"
        warning.daysUntilExpiry == 0L -> "läuft heute ab"
        else -> "noch ${warning.daysUntilExpiry} Tage"
    }

    Card(
        modifier = Modifier.fillMaxWidth(),
        colors = CardDefaults.cardColors(containerColor = containerColor)
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = warning.item.name,
                style = MaterialTheme.typography.bodyMedium,
                color = contentColor
            )
            Text(
                text = daysText,
                style = MaterialTheme.typography.bodyMedium,
                fontWeight = FontWeight.Bold,
                color = contentColor
            )
        }
    }
}


