package com.sunzones.ui.main.components

import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.spring
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp
import com.sunzones.ui.theme.MoonBlue
import com.sunzones.ui.theme.MoonGlow
import com.sunzones.ui.theme.MoonSilver
import com.sunzones.ui.theme.SunGlow
import com.sunzones.ui.theme.SunGold
import com.sunzones.ui.theme.SunOrange
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin

@Composable
fun SunArc(
    progress: Float,
    isDaytime: Boolean,
    nightProgress: Float = 0f,
    modifier: Modifier = Modifier
) {
    val animatedProgress by animateFloatAsState(
        targetValue = progress,
        animationSpec = spring(
            dampingRatio = Spring.DampingRatioMediumBouncy,
            stiffness = Spring.StiffnessLow
        ),
        label = "sun_progress"
    )

    val animatedNightProgress by animateFloatAsState(
        targetValue = nightProgress,
        animationSpec = spring(
            dampingRatio = Spring.DampingRatioMediumBouncy,
            stiffness = Spring.StiffnessLow
        ),
        label = "night_progress"
    )

    Canvas(
        modifier = modifier
            .fillMaxWidth()
            .height(180.dp)
    ) {
        val padding = 32.dp.toPx()
        val arcWidth = size.width - padding * 2
        val arcHeight = arcWidth / 2
        val centerX = size.width / 2
        val baseY = size.height - 20.dp.toPx()

        val arcRect = Rect(
            left = padding,
            top = baseY - arcHeight,
            right = padding + arcWidth,
            bottom = baseY + arcHeight
        )

        // Horizon line
        drawLine(
            color = Color.White.copy(alpha = 0.3f),
            start = Offset(padding - 8.dp.toPx(), baseY),
            end = Offset(size.width - padding + 8.dp.toPx(), baseY),
            strokeWidth = 1.5.dp.toPx()
        )

        // Track arc (faded)
        drawArc(
            color = Color.White.copy(alpha = 0.15f),
            startAngle = 180f,
            sweepAngle = 180f,
            useCenter = false,
            topLeft = arcRect.topLeft,
            size = arcRect.size,
            style = Stroke(width = 3.dp.toPx(), cap = StrokeCap.Round)
        )

        if (isDaytime && animatedProgress > 0f) {
            // Progress arc (golden) — daytime
            drawArc(
                brush = Brush.horizontalGradient(
                    colors = listOf(SunOrange, SunGold, SunOrange),
                    startX = arcRect.left,
                    endX = arcRect.right
                ),
                startAngle = 180f,
                sweepAngle = 180f * animatedProgress,
                useCenter = false,
                topLeft = arcRect.topLeft,
                size = arcRect.size,
                style = Stroke(width = 3.dp.toPx(), cap = StrokeCap.Round)
            )

            // Sun dot
            val angle = PI - (animatedProgress * PI)
            val rx = arcWidth / 2
            val ry = arcHeight
            val sunX = centerX + (rx * cos(angle)).toFloat()
            val sunY = baseY - (ry * sin(angle)).toFloat()
            drawSunGlow(sunX, sunY)
        } else if (!isDaytime && animatedNightProgress > 0f) {
            // Progress arc (silver) — nighttime
            drawArc(
                brush = Brush.horizontalGradient(
                    colors = listOf(MoonBlue, MoonSilver, MoonBlue),
                    startX = arcRect.left,
                    endX = arcRect.right
                ),
                startAngle = 180f,
                sweepAngle = 180f * animatedNightProgress,
                useCenter = false,
                topLeft = arcRect.topLeft,
                size = arcRect.size,
                style = Stroke(width = 3.dp.toPx(), cap = StrokeCap.Round)
            )

            // Moon dot
            val angle = PI - (animatedNightProgress * PI)
            val rx = arcWidth / 2
            val ry = arcHeight
            val moonX = centerX + (rx * cos(angle)).toFloat()
            val moonY = baseY - (ry * sin(angle)).toFloat()
            drawMoonGlow(moonX, moonY)
        }
    }
}

private fun DrawScope.drawSunGlow(x: Float, y: Float) {
    val glowLayers = listOf(
        24.dp.toPx() to 0.05f,
        18.dp.toPx() to 0.08f,
        12.dp.toPx() to 0.15f,
        8.dp.toPx() to 0.3f
    )

    for ((radius, alpha) in glowLayers) {
        drawCircle(
            color = SunGold.copy(alpha = alpha),
            radius = radius,
            center = Offset(x, y)
        )
    }

    drawCircle(
        brush = Brush.radialGradient(
            colors = listOf(SunGlow, SunGold),
            center = Offset(x, y),
            radius = 6.dp.toPx()
        ),
        radius = 6.dp.toPx(),
        center = Offset(x, y)
    )
}

private fun DrawScope.drawMoonGlow(x: Float, y: Float) {
    val glowLayers = listOf(
        18.dp.toPx() to 0.04f,
        12.dp.toPx() to 0.08f,
        8.dp.toPx() to 0.15f
    )

    for ((radius, alpha) in glowLayers) {
        drawCircle(
            color = MoonSilver.copy(alpha = alpha),
            radius = radius,
            center = Offset(x, y)
        )
    }

    drawCircle(
        brush = Brush.radialGradient(
            colors = listOf(MoonGlow, MoonSilver),
            center = Offset(x, y),
            radius = 5.dp.toPx()
        ),
        radius = 5.dp.toPx(),
        center = Offset(x, y)
    )
}
