package eu.gaudian.translator.view.exercises

import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import eu.gaudian.translator.R
import eu.gaudian.translator.view.composable.AppIcons
import eu.gaudian.translator.viewmodel.AiGenerationState
import kotlin.math.cos
import kotlin.math.sin

@Composable
fun AiGenerationScreen(state: AiGenerationState) {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Black.copy(alpha = 0.8f))
            .padding(16.dp),
        contentAlignment = Alignment.Center
    ) {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(24.dp)
        ) {
            when (state) {
                is AiGenerationState.Generating -> {
                    GeneratingAnimation()
                    Text(
                        text = state.statusMessage,
                        color = Color.White,
                        style = MaterialTheme.typography.titleMedium
                    )
                }
                is AiGenerationState.Success -> {
                    Icon(
                        imageVector = AppIcons.CheckCircle,
                        contentDescription = stringResource(R.string.cd_success),
                        tint = Color.Green,
                        modifier = Modifier.size(80.dp)
                    )
                    Text(
                        text = state.generatedExerciseTitle,
                        color = Color.White,
                        style = MaterialTheme.typography.titleLarge,
                        fontWeight = FontWeight.Bold
                    )
                }
                is AiGenerationState.Error -> {
                    Icon(
                        imageVector = AppIcons.Error,
                        contentDescription = stringResource(R.string.cd_error),
                        tint = MaterialTheme.colorScheme.error,
                        modifier = Modifier.size(80.dp)
                    )
                    Text(
                        text = state.errorMessage,
                        color = Color.White,
                        style = MaterialTheme.typography.titleLarge,
                        fontWeight = FontWeight.Bold
                    )
                }
                else -> {}
            }
        }
    }
}

@Composable
private fun GeneratingAnimation() {
    @Suppress("HardCodedStringLiteral") val transition = rememberInfiniteTransition(label = "generating_anim")

    val angle1 by transition.animateFloat(
        initialValue = 0f,
        targetValue = 360f,
        animationSpec = infiniteRepeatable(tween(2000, easing = LinearEasing), RepeatMode.Restart),
        label = ""
    )
    val angle2 by transition.animateFloat(
        initialValue = 180f,
        targetValue = 540f,
        animationSpec = infiniteRepeatable(tween(3000, easing = FastOutSlowInEasing), RepeatMode.Restart),
        label = ""
    )
    val scale by transition.animateFloat(
        initialValue = 1f,
        targetValue = 1.2f,
        animationSpec = infiniteRepeatable(tween(1500), RepeatMode.Reverse),
        label = ""
    )
    val color = MaterialTheme.colorScheme.primary

    Canvas(modifier = Modifier.size(120.dp)) {
        val radius = size.minDimension / 4
        val center = this.center

        drawCircle(
            color = color,
            radius = radius * scale,
            style = Stroke(width = 8f)
        )

        fun getOffset(angle: Float, r: Float) = Offset(
            x = center.x + r * cos(angle * (Math.PI / 180f)).toFloat(),
            y = center.y + r * sin(angle * (Math.PI / 180f)).toFloat()
        )

        drawCircle(
            color = color,
            radius = 20f,
            center = getOffset(angle1, radius * 2.5f)
        )

        drawCircle(
            color = color.copy(alpha = 0.7f),
            radius = 15f,
            center = getOffset(angle2, radius * 2.5f)
        )
    }
}