package moe.lava.neon.ui.screens.navigator

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.collectIsHoveredAsState
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.MaterialShapes
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.draw.scale
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.graphics.shapes.Morph
import coil3.compose.AsyncImage
import moe.lava.neon.ui.util.MorphPolygonShape

@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
fun GuildButton(
    model: NavigatorModel.Guild,
    morph: Morph = Morph(MaterialShapes.Companion.Diamond, MaterialShapes.Companion.Cookie9Sided),
    selected: Boolean,
    onSelected: (NavigatorModel.Guild) -> Unit,
) {
    val interactionSource = remember {
        MutableInteractionSource()
    }
    val isHovered by interactionSource.collectIsHoveredAsState()
    val isPressed by interactionSource.collectIsPressedAsState()
    val target = when {
        isPressed -> 0.85f
        selected -> 1f
        isHovered -> 0.8f
        else -> 0f
    }
    val fadingIn = isPressed && !selected || !isPressed && selected

    val fastProgress by animateFloatAsState(
        targetValue = target,
        label = "progress",
        animationSpec = MaterialTheme.motionScheme.fastSpatialSpec()
    )
    val defaultProgress by animateFloatAsState(
        targetValue = target,
        label = "progress",
        animationSpec = MaterialTheme.motionScheme.defaultSpatialSpec()
    )
    val slowProgress by animateFloatAsState(
        targetValue = target,
        label = "progress",
        animationSpec = MaterialTheme.motionScheme.slowSpatialSpec()
    )
    val progress = if (fadingIn) fastProgress else slowProgress
    val rotation = if (fadingIn) defaultProgress * 360f else (1 - defaultProgress) * 360f
    Box(
        Modifier
            .size(guildIconSize + 24.dp)
            .clickable(
                interactionSource = interactionSource,
                indication = null,
            ) { onSelected(model) }
    ) {
        Box(
            Modifier
                .fillMaxSize()
                .scale(progress)
                .rotate(rotation)
                .alpha(progress)
        ) {
            Box(
                Modifier
                    .fillMaxSize()
                    .clip(MorphPolygonShape(morph, progress))
                    .background(MaterialTheme.colorScheme.primary)
                    .fillMaxSize()
                    .align(Alignment.Companion.Center)
            )
        }
        Box(Modifier.fillMaxSize().padding(12.dp)) {
            AsyncImage(
                model = model.url,
                contentDescription = model.name,
                contentScale = ContentScale.Companion.Crop,
                modifier = Modifier.clip(CircleShape),
            )
        }
    }
}
