package com.safecheckin.app.ui.components

import androidx.compose.animation.core.*
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun CircularCheckInButton(
    isCheckedIn: Boolean,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    size: Dp = 220.dp,
    primaryColor: Color = MaterialTheme.colorScheme.primary,
    backgroundColor: Color = MaterialTheme.colorScheme.surfaceVariant
) {
    val progress by animateFloatAsState(
        targetValue = if (isCheckedIn) 1f else 0f,
        animationSpec = tween(durationMillis = 800, easing = EaseOutCubic),
        label = "progress"
    )
    
    val scale by animateFloatAsState(
        targetValue = if (isCheckedIn) 0.95f else 1f,
        animationSpec = spring(dampingRatio = Spring.DampingRatioMediumBouncy),
        label = "scale"
    )

    Box(
        contentAlignment = Alignment.Center,
        modifier = modifier.size(size)
    ) {
        // Glow effect when checked in
        if (isCheckedIn) {
            Canvas(modifier = Modifier.size(size + 20.dp)) {
                drawCircle(
                    color = primaryColor.copy(alpha = 0.2f),
                    radius = (size.toPx() + 20.dp.toPx()) / 2
                )
            }
        }

        // Background circle with gradient
        Canvas(modifier = Modifier.size(size)) {
            val strokeWidth = 8.dp.toPx()
            val radius = (size.toPx() - strokeWidth) / 2

            // Draw background circle
            drawCircle(
                color = backgroundColor.copy(alpha = 0.3f),
                radius = radius,
                style = Stroke(width = strokeWidth)
            )

            // Draw progress arc with gradient effect
            if (progress > 0) {
                drawArc(
                    color = primaryColor,
                    startAngle = -90f,
                    sweepAngle = 360f * progress,
                    useCenter = false,
                    topLeft = Offset(strokeWidth / 2, strokeWidth / 2),
                    size = Size(size.toPx() - strokeWidth, size.toPx() - strokeWidth),
                    style = Stroke(width = strokeWidth, cap = StrokeCap.Round)
                )
            }
        }

        // Main button
        Button(
            onClick = onClick,
            modifier = Modifier
                .size(size - 40.dp)
                .scale(scale),
            colors = ButtonDefaults.buttonColors(
                containerColor = if (isCheckedIn) primaryColor else backgroundColor,
                contentColor = if (isCheckedIn) Color.White else primaryColor
            ),
            shape = CircleShape,
            elevation = ButtonDefaults.buttonElevation(
                defaultElevation = if (isCheckedIn) 8.dp else 4.dp,
                pressedElevation = 0.dp
            )
        ) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = if (isCheckedIn) "已确认" else "确认存活",
                    fontSize = 28.sp,
                    fontWeight = FontWeight.Bold
                )
                if (!isCheckedIn) {
                    Spacer(modifier = Modifier.height(4.dp))
                    Text(
                        text = "点击确认",
                        fontSize = 14.sp,
                        fontWeight = FontWeight.Normal,
                        color = primaryColor.copy(alpha = 0.8f)
                    )
                }
            }
        }
    }
}