package club.clubk.ktag.apps.mine.ui

import android.content.Context
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
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.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.sp
import club.clubk.ktag.apps.core.ui.theme.KTagRed
import club.clubk.ktag.apps.core.ui.theme.KTagYellow
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun MineExplosionAnimation(
    onAnimationComplete: () -> Unit,
    modifier: Modifier = Modifier
) {
    val context = LocalContext.current
    val flashAlpha = remember { Animatable(0f) }
    val ring1Progress = remember { Animatable(0f) }
    val ring2Progress = remember { Animatable(0f) }
    val ring3Progress = remember { Animatable(0f) }
    val textScale = remember { Animatable(0f) }
    val textAlpha = remember { Animatable(0f) }

    LaunchedEffect(Unit) {
        // Haptic feedback for explosion
        triggerExplosionHaptic(context)

        // Initial flash
        launch {
            flashAlpha.animateTo(0.8f, tween(100))
            flashAlpha.animateTo(0f, tween(400))
        }

        // Expanding rings with staggered start
        launch {
            ring1Progress.animateTo(1f, tween(500, easing = FastOutSlowInEasing))
        }
        launch {
            delay(100)
            ring2Progress.animateTo(1f, tween(500, easing = FastOutSlowInEasing))
        }
        launch {
            delay(200)
            ring3Progress.animateTo(1f, tween(500, easing = FastOutSlowInEasing))
        }

        // Text animation
        launch {
            delay(150)
            textAlpha.snapTo(1f)
            textScale.animateTo(1.2f, tween(200, easing = FastOutSlowInEasing))
            textScale.animateTo(1f, tween(100))
        }

        // Wait for animations to complete
        delay(1200)

        // Fade out
        textAlpha.animateTo(0f, tween(300))

        onAnimationComplete()
    }

    Box(
        modifier = modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        // Flash overlay
        if (flashAlpha.value > 0f) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(KTagYellow.copy(alpha = flashAlpha.value))
            )
        }

        // Explosion rings
        Canvas(modifier = Modifier.fillMaxSize()) {
            val center = Offset(size.width / 2, size.height / 2)
            val maxRadius = size.maxDimension * 0.7f

            // Ring 1 - Yellow/Orange
            if (ring1Progress.value > 0f) {
                val radius = maxRadius * ring1Progress.value
                val alpha = (1f - ring1Progress.value).coerceIn(0f, 1f)
                val strokeWidth = 40f * (1f - ring1Progress.value * 0.5f)
                drawCircle(
                    color = KTagYellow.copy(alpha = alpha),
                    radius = radius,
                    center = center,
                    style = Stroke(width = strokeWidth, cap = StrokeCap.Round)
                )
            }

            // Ring 2 - Red/Orange
            if (ring2Progress.value > 0f) {
                val radius = maxRadius * ring2Progress.value * 0.85f
                val alpha = (1f - ring2Progress.value).coerceIn(0f, 1f)
                val strokeWidth = 50f * (1f - ring2Progress.value * 0.5f)
                drawCircle(
                    color = KTagRed.copy(alpha = alpha),
                    radius = radius,
                    center = center,
                    style = Stroke(width = strokeWidth, cap = StrokeCap.Round)
                )
            }

            // Ring 3 - Dark Red
            if (ring3Progress.value > 0f) {
                val radius = maxRadius * ring3Progress.value * 0.7f
                val alpha = (1f - ring3Progress.value).coerceIn(0f, 1f)
                val strokeWidth = 60f * (1f - ring3Progress.value * 0.5f)
                drawCircle(
                    color = Color(0xFFAA0000).copy(alpha = alpha),
                    radius = radius,
                    center = center,
                    style = Stroke(width = strokeWidth, cap = StrokeCap.Round)
                )
            }
        }

        // "BOOM!" text
        if (textAlpha.value > 0f) {
            Text(
                text = "BOOM!",
                fontSize = (72 * textScale.value).sp,
                fontWeight = FontWeight.ExtraBold,
                color = KTagRed.copy(alpha = textAlpha.value),
                textAlign = TextAlign.Center
            )
        }
    }
}

private fun triggerExplosionHaptic(context: Context) {
    val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
        vibratorManager.defaultVibrator
    } else {
        @Suppress("DEPRECATION")
        context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Strong explosion vibration pattern: initial burst + aftershock
        val timings = longArrayOf(0, 100, 50, 150)
        val amplitudes = intArrayOf(0, 255, 0, 180)
        vibrator.vibrate(VibrationEffect.createWaveform(timings, amplitudes, -1))
    } else {
        @Suppress("DEPRECATION")
        vibrator.vibrate(longArrayOf(0, 100, 50, 150), -1)
    }
}
