package com.example.godeye.ui.components

import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import com.example.godeye.ui.theme.GodEyeColors

@Composable
fun AnimatedFloatingPanel(
    visible: Boolean,
    modifier: Modifier = Modifier,
    slideDirection: SlideDirection = SlideDirection.FromBottom,
    content: @Composable () -> Unit
) {
    AnimatedVisibility(
        visible = visible,
        enter = slideInVertically(
            animationSpec = spring(
                dampingRatio = Spring.DampingRatioMediumBouncy,
                stiffness = Spring.StiffnessLow
            )
        ) { fullHeight ->
            when (slideDirection) {
                SlideDirection.FromTop -> -fullHeight
                SlideDirection.FromBottom -> fullHeight
            }
        } + fadeIn(animationSpec = tween(300)),
        exit = slideOutVertically(
            animationSpec = spring(
                dampingRatio = Spring.DampingRatioNoBouncy,
                stiffness = Spring.StiffnessMedium
            )
        ) { fullHeight ->
            when (slideDirection) {
                SlideDirection.FromTop -> -fullHeight
                SlideDirection.FromBottom -> fullHeight
            }
        } + fadeOut(animationSpec = tween(200)),
        modifier = modifier
    ) {
        Card(
            modifier = Modifier.fillMaxWidth(),
            colors = CardDefaults.cardColors(
                containerColor = GodEyeColors.BlackSoft.copy(alpha = 0.9f)
            ),
            shape = RoundedCornerShape(16.dp),
            elevation = CardDefaults.cardElevation(defaultElevation = 8.dp)
        ) {
            content()
        }
    }
}

@Composable
fun PulsingRecordIndicator(
    isRecording: Boolean,
    modifier: Modifier = Modifier
) {
    if (isRecording) {
        val infiniteTransition = rememberInfiniteTransition(label = "recording")
        val scale by infiniteTransition.animateFloat(
            initialValue = 0.8f,
            targetValue = 1.2f,
            animationSpec = infiniteRepeatable(
                animation = tween(1000),
                repeatMode = RepeatMode.Reverse
            ),
            label = "scale"
        )

        Row(
            modifier = modifier,
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            Box(
                modifier = Modifier
                    .size(12.dp)
                    .scale(scale)
                    .background(
                        GodEyeColors.RecordRed,
                        RoundedCornerShape(6.dp)
                    )
            )
            Text(
                text = "REC",
                color = GodEyeColors.RecordRed,
                style = MaterialTheme.typography.labelSmall
            )
        }
    }
}

@Composable
fun AnimatedControlButton(
    onClick: () -> Unit,
    icon: ImageVector,
    contentDescription: String,
    modifier: Modifier = Modifier,
    isActive: Boolean = false,
    activeColor: Color = GodEyeColors.NavyLight,
    size: ButtonSize = ButtonSize.Medium
) {
    val scale by animateFloatAsState(
        targetValue = if (isActive) 1.1f else 1.0f,
        animationSpec = spring(dampingRatio = Spring.DampingRatioMediumBouncy),
        label = "button_scale"
    )

    val backgroundColor by animateColorAsState(
        targetValue = if (isActive) activeColor else GodEyeColors.BlackSoft.copy(alpha = 0.8f),
        animationSpec = tween(300),
        label = "button_color"
    )

    val buttonSize = when (size) {
        ButtonSize.Small -> 40.dp
        ButtonSize.Medium -> 56.dp
        ButtonSize.Large -> 72.dp
    }

    IconButton(
        onClick = onClick,
        modifier = modifier
            .size(buttonSize)
            .scale(scale)
            .background(
                backgroundColor,
                RoundedCornerShape(buttonSize / 2)
            )
    ) {
        Icon(
            imageVector = icon,
            contentDescription = contentDescription,
            tint = if (isActive) Color.White else GodEyeColors.IvoryPure,
            modifier = Modifier.size(buttonSize * 0.4f)
        )
    }
}

@Composable
fun SlideInErrorSnackbar(
    error: String?,
    onDismiss: () -> Unit,
    modifier: Modifier = Modifier
) {
    AnimatedVisibility(
        visible = error != null,
        enter = slideInVertically(
            initialOffsetY = { -it },
            animationSpec = spring(dampingRatio = Spring.DampingRatioMediumBouncy)
        ) + fadeIn(),
        exit = slideOutVertically(
            targetOffsetY = { -it },
            animationSpec = tween(300)
        ) + fadeOut(),
        modifier = modifier
    ) {
        if (error != null) {
            Card(
                colors = CardDefaults.cardColors(
                    containerColor = GodEyeColors.RecordRed
                ),
                shape = RoundedCornerShape(8.dp),
                modifier = Modifier.fillMaxWidth()
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(
                        text = error,
                        color = Color.White,
                        style = MaterialTheme.typography.bodyMedium,
                        modifier = Modifier.weight(1f)
                    )
                    IconButton(onClick = onDismiss) {
                        Icon(
                            Icons.Default.Close,
                            contentDescription = "Закрыть",
                            tint = Color.White
                        )
                    }
                }
            }

            LaunchedEffect(error) {
                kotlinx.coroutines.delay(5000)
                onDismiss()
            }
        }
    }
}

enum class SlideDirection {
    FromTop,
    FromBottom
}

enum class ButtonSize {
    Small,
    Medium,
    Large
}
