package app.closer.ui.components

import app.closer.ui.theme.closerCardColor
import android.provider.Settings
import androidx.compose.animation.core.FastOutSlowInEasing
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.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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.drawscope.clipRect
import androidx.compose.ui.graphics.drawscope.withTransform
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.vector.PathParser
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Composable
fun LoadingState(
    message: String = "Loading…",
    modifier: Modifier = Modifier
) {
    CloserCard(
        modifier = modifier.fillMaxWidth(),
        containerColor = closerCardColor(alpha = 0.8f)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(CloserSpacing.Xxxl),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(CloserSpacing.Md)
        ) {
            CloserHeartLoader()
            Text(
                text = message,
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
                textAlign = TextAlign.Center
            )
            BrandMessageRotator(
                color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.82f),
                style = MaterialTheme.typography.bodySmall
            )
        }
    }
}

@Composable
fun CloserHeartLoader(
    modifier: Modifier = Modifier,
    size: Dp = 76.dp
) {
    val context = LocalContext.current
    val reducedMotion = remember {
        Settings.Global.getFloat(
            context.contentResolver,
            Settings.Global.ANIMATOR_DURATION_SCALE,
            1f
        ) == 0f
    }
    val transition = rememberInfiniteTransition(label = "closerHeartLoader")
    val animatedFill = transition.animateFloat(
        initialValue = 0.08f,
        targetValue = 1f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 1500, easing = FastOutSlowInEasing),
            repeatMode = RepeatMode.Restart
        ),
        label = "closerHeartFill"
    )
    val animatedPulse = transition.animateFloat(
        initialValue = 0.96f,
        targetValue = 1.04f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 900, easing = FastOutSlowInEasing),
            repeatMode = RepeatMode.Reverse
        ),
        label = "closerHeartPulse"
    )

    val fillProgress = if (reducedMotion) 1f else animatedFill.value
    val pulse = if (reducedMotion) 1f else animatedPulse.value
    val shadowPath = remember {
        PathParser().parsePathString(
            "M54,89C48,82 25,65 20,50C15,35 23,22 37,22C45,22 51,26 54,33C57,26 63,22 71,22C85,22 93,35 88,50C83,65 60,82 54,89Z"
        ).toPath()
    }
    val leftPath = remember {
        PathParser().parsePathString(
            "M54,85C49,79 27,62 22,48C17,35 24,24 37,24C45,24 51,28 54,35Z"
        ).toPath()
    }
    val rightPath = remember {
        PathParser().parsePathString(
            "M54,85C59,79 81,62 86,48C91,35 84,24 71,24C63,24 57,28 54,35Z"
        ).toPath()
    }
    val leftHighlight = remember {
        PathParser().parsePathString(
            "M27,42C28,32 34,27 42,27C48,27 52,30 54,35L54,41C47,36 37,36 27,42Z"
        ).toPath()
    }
    val rightHighlight = remember {
        PathParser().parsePathString(
            "M54,35C57,30 62,27 69,27C78,27 84,32 85,42C75,36 65,36 54,41Z"
        ).toPath()
    }

    Canvas(
        modifier = modifier
            .size(size)
            .graphicsLayer {
                scaleX = pulse
                scaleY = pulse
            }
            .clearAndSetSemantics {}
    ) {
        val scaleX = this.size.width / 108f
        val scaleY = this.size.height / 108f

        withTransform({
            scale(scaleX = scaleX, scaleY = scaleY, pivot = Offset.Zero)
        }) {
            drawPath(shadowPath, color = Color(0xFF24122F).copy(alpha = 0.10f))
            drawPath(leftPath, color = Color(0xFFF7C8E4).copy(alpha = 0.22f))
            drawPath(rightPath, color = Color(0xFFD9B8FF).copy(alpha = 0.22f))
            clipRect(top = 108f * (1f - fillProgress), bottom = 108f) {
                drawPath(leftPath, color = Color(0xFFF7C8E4))
                drawPath(rightPath, color = Color(0xFFD9B8FF))
                drawPath(leftHighlight, color = Color(0xFFFFF4FA).copy(alpha = 0.68f))
                drawPath(rightHighlight, color = Color(0xFFF3E8FF).copy(alpha = 0.52f))
            }
        }
    }
}
