package com.horrortv.app.presentation.common

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusGroup
import androidx.compose.foundation.focusable
import androidx.compose.foundation.interaction.MutableInteractionSource
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.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
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.clip
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.horrortv.app.domain.model.AppError
import com.horrortv.app.presentation.theme.HorrorColors
import com.horrortv.app.presentation.theme.HorrorTypography

@Composable
fun TvErrorDisplay(
    error: AppError,
    onRetry: () -> Unit,
    modifier: Modifier = Modifier
) {
    var isRetryFocused by remember { mutableStateOf(false) }
    val focusRequester = remember { FocusRequester() }
    val interactionSource = remember { MutableInteractionSource() }
    
    LaunchedEffect(Unit) { focusRequester.requestFocus() }

    val iconRes = when (error) {
        is AppError.NetworkError -> android.R.drawable.ic_menu_close_clear_cancel
        is AppError.ApiError -> android.R.drawable.ic_dialog_alert
        is AppError.CacheError -> android.R.drawable.ic_menu_delete
        is AppError.ValidationError -> android.R.drawable.ic_menu_edit
        is AppError.UnknownError -> android.R.drawable.ic_menu_help
    }

    val titleText = when (error) {
        is AppError.NetworkError -> "ERROR DE CONEXIÓN"
        is AppError.ApiError -> "ERROR DEL SERVICIO"
        is AppError.CacheError -> "ERROR DE DATOS"
        is AppError.ValidationError -> "DATOS INVÁLIDOS"
        is AppError.UnknownError -> "ERROR INESPERADO"
    }

    val retryButtonColor = if (isRetryFocused) HorrorColors.HorrorAccent else HorrorColors.HorrorRed

    Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(32.dp),
            modifier = Modifier.padding(48.dp).focusGroup()
        ) {
            Icon(
                painter = painterResource(id = iconRes),
                contentDescription = "Error",
                tint = HorrorColors.HorrorRed,
                modifier = Modifier.size(80.dp)
            )

            Text(text = titleText, style = HorrorTypography.CategoryTitle, color = HorrorColors.HorrorAccent, textAlign = TextAlign.Center)
            Text(text = error.userMessage, style = HorrorTypography.DetailPlot, color = HorrorColors.HorrorWhite, textAlign = TextAlign.Center, modifier = Modifier.fillMaxWidth(0.6f))

            if (error.isRetryable) {
                Spacer(modifier = Modifier.height(24.dp))

                Box(
                    modifier = Modifier
                        .width(300.dp)
                        .height(56.dp)
                        .clip(RoundedCornerShape(8.dp))
                        .background(retryButtonColor)
                        .border(if (isRetryFocused) 3.dp else 1.dp, if (isRetryFocused) Color.White else HorrorColors.HorrorLightGray, RoundedCornerShape(8.dp))
                        .focusRequester(focusRequester)
                        .focusable(interactionSource = interactionSource)
                        .onFocusChanged { isRetryFocused = it.isFocused }
                        .clickable(interactionSource = interactionSource, indication = null, onClick = onRetry),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "REINTENTAR", style = HorrorTypography.PlayButton, color = HorrorColors.HorrorWhite)
                }
            }
        }
    }
}

@Composable
fun TvSnackbarError(
    error: AppError,
    onDismiss: () -> Unit,
    modifier: Modifier = Modifier
) {
    var isDismissFocused by remember { mutableStateOf(false) }
    val focusRequester = remember { FocusRequester() }
    val interactionSource = remember { MutableInteractionSource() }
    
    LaunchedEffect(Unit) { focusRequester.requestFocus() }

    val dismissColor = if (isDismissFocused) HorrorColors.HorrorAccent else HorrorColors.HorrorRed

    Box(
        modifier = modifier
            .fillMaxWidth()
            .background(HorrorColors.HorrorGray.copy(alpha = 0.9f), RoundedCornerShape(8.dp))
            .padding(16.dp)
            .focusGroup()
    ) {
        Row(
            horizontalArrangement = Arrangement.spacedBy(16.dp),
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.fillMaxWidth()
        ) {
            Icon(
                painter = painterResource(id = android.R.drawable.ic_menu_close_clear_cancel),
                contentDescription = "Error",
                tint = HorrorColors.HorrorRed,
                modifier = Modifier.size(24.dp)
            )
            Text(text = error.userMessage, style = HorrorTypography.DetailInfo, color = HorrorColors.HorrorWhite, modifier = Modifier.weight(1f))
            if (error.isRetryable) {
                Box(
                    modifier = Modifier
                        .width(80.dp)
                        .height(40.dp)
                        .clip(RoundedCornerShape(4.dp))
                        .background(dismissColor)
                        .border(if (isDismissFocused) 2.dp else 1.dp, if (isDismissFocused) Color.White else HorrorColors.HorrorLightGray, RoundedCornerShape(4.dp))
                        .focusRequester(focusRequester)
                        .focusable(interactionSource = interactionSource)
                        .onFocusChanged { isDismissFocused = it.isFocused }
                        .clickable(interactionSource = interactionSource, indication = null, onClick = onDismiss),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "OK", style = HorrorTypography.DetailInfo, color = HorrorColors.HorrorWhite)
                }
            }
        }
    }
}