package id.ac.ubharajaya.panicbutton

import androidx.compose.animation.core.tween
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
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.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.Dp

@Composable
fun PanicButton(onClick: () -> Unit, buttonSize: Dp = 170.dp, shadowSize: Dp = 210.dp) {
    val scaleFactor = buttonSize.value / 170f
    val panicColor = Color(0xFFB71C1C)
    val darkShade = Color(0xFF7F0F0F)
    val lightAccent = Color(0xFFFF8A80)
    val interactionSource = remember { MutableInteractionSource() }
    val isPressedState = interactionSource.collectIsPressedAsState()
    val isPressed = isPressedState.value
    val scaleAnim = animateFloatAsState(targetValue = if (isPressed) 0.96f else 1f, animationSpec = tween(120)).value
    val elevationAnim = animateDpAsState(targetValue = if (isPressed) 6.dp else 18.dp, animationSpec = tween(120)).value
    val gradient = Brush.verticalGradient(listOf(lightAccent, panicColor, darkShade))

    Box(contentAlignment = Alignment.Center) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                .size(buttonSize)
                .scale(scaleAnim)
                .shadow(elevation = elevationAnim * scaleFactor, shape = CircleShape)
                .background(brush = gradient, shape = CircleShape)
                .clickable(indication = null, interactionSource = interactionSource) { onClick() }
        ) {
            // highlight bubble (scaled)
            val highlightSize = 70.dp * scaleFactor
            val highlightOffsetX = (-24).dp * scaleFactor
            val highlightOffsetY = (-28).dp * scaleFactor

            Box(modifier = Modifier.size(highlightSize).offset(x = highlightOffsetX, y = highlightOffsetY).background(color = Color.White.copy(alpha = 0.16f), shape = CircleShape))
            val fontSize = (72f * scaleFactor).sp
            Text("!", color = Color.White, fontSize = fontSize)
        }
    }
}
