package com.laseroptek.raman.ui.screens.home.preset

import android.content.res.Configuration
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
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.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.dropShadow
import com.laseroptek.raman.utils.ext.innerShadow
import com.laseroptek.raman.utils.ext.px
import timber.log.Timber

@Composable
fun PresetButton(
    modifier: Modifier = Modifier, // Default size set here
    fontSize: Int = 18,
    no: Int = 0,
    selected: Int = 0,
    onClick: (Int) -> Unit = {},
    onLongClick: (Int) -> Unit = {},
) {
    Row(
        modifier = modifier
            .clip(RoundedCornerShape(50.px.dp)) // Note: This fixed radius might need adjustment if size changes significantly
            .combinedClickable(
                interactionSource = remember { MutableInteractionSource() },
                indication = null, // This removes the ripple effect, same as noRippleClickable
                onClick = {
                    Timber.d("click on PresetButton: $no")
                    onClick.invoke(no)
                },
                onLongClick = {
                    Timber.d("Long click on PresetButton: $no")
                    onLongClick.invoke(no)
                }
            )
            .border(
                border = BorderStroke(
                    width = 2.dp,
                    brush = SolidColor(
                        Color(131, 131, 131)
                    )
                ),
                shape = RoundedCornerShape(50.dp) // Note: This shape's radius might also need adjustment.
            )
            .dropShadow(
                shape = RoundedCornerShape(50.px.dp), // Note: Fixed radius
                color = if (selected == no) {
                    Color(213, 167, 15)
                } else {
                    Color(0, 0, 0).copy(alpha = 0.27f)
                },
                blur = 5.px.dp,
                offsetY = 2.px.dp,
                offsetX = 0.px.dp,
                spread = 0.px.dp
            )
            .innerShadow(
                shape = RoundedCornerShape(50.px.dp), // Note: Fixed radius
                color = if (selected == no) {
                    Color(213, 167, 15)
                } else {
                    Color(0, 0, 0).copy(alpha = 0.15f)
                },
                blur = 5.px.dp,
                offsetY = -2.px.dp,
                offsetX = 0.px.dp,
                spread = 0.px.dp
            )
            .innerShadow(
                shape = RoundedCornerShape(50.px.dp), // Note: Fixed radius
                color = if (selected == no) {
                    Color(213, 167, 15)
                } else {
                    Color(0, 0, 0).copy(alpha = 0.5f)
                },
                blur = 1.px.dp,
                offsetY = -1.px.dp,
                offsetX = 0.px.dp,
                spread = 0.px.dp
            )
            .background(
                if (selected == no) {
                    Brush.verticalGradient(
                        colors = listOf(
                            Color(255, 239, 207),
                            Color(255, 211, 124),
                        )
                    )
                } else {
                    Brush.verticalGradient(
                        colors = listOf(
                            Color(192, 192, 192),
                            Color(246, 246, 246),
                        )
                    )
                },
                shape = RoundedCornerShape(50.px.dp) // Note: Fixed radius
            ),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            text = no.toString(),
            style = RobotoTypography.bodySmall,
            fontWeight = FontWeight.ExtraLight,
            fontSize = fontSize.px.sp, // Note: Fixed font size might need adjustment if button size changes significantly
            color = Color(49,46,38),
            textAlign = TextAlign.Center,
        )
    }
}


@Preview(
    apiLevel = 34,
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=1920dp,height=1080dp,dpi=150,isRound=false,chinSize=0dp,orientation=portrait"
)
@Composable
fun PreviewPresetButton() {
    PresetButton(
        modifier = Modifier.size(50.dp)
    )
}
