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

import android.content.res.Configuration
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.runtime.Composable
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.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.laseroptek.raman.R
import com.laseroptek.raman.const.PresetButtonType
import com.laseroptek.raman.utils.ext.dropShadow
import com.laseroptek.raman.utils.ext.innerShadow
import com.laseroptek.raman.utils.ext.noRippleClickable
import com.laseroptek.raman.utils.ext.px
import timber.log.Timber

@Composable
fun PresetIconButton(
    modifier: Modifier = Modifier, // Default size set here
    type: PresetButtonType = PresetButtonType.SAVE,
    selected: Boolean = false,
    onClick: () -> Unit = {},
) {
    Row(
        modifier = modifier
            .clip(RoundedCornerShape(50.px.dp)) // Note: This fixed radius might need adjustment if size changes significantly
            .noRippleClickable {
                Timber.d("onClick")
                onClick.invoke()
            }
            .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.
                                                 // Consider using 50.px.dp like other shapes for consistency.
            )
            .dropShadow(
                shape = RoundedCornerShape(50.px.dp), // Note: Fixed radius
                color = if (selected) {
                    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) {
                    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 = 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) {
                    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
    ) {
        Image(
            painter = painterResource(id =
                if (type == PresetButtonType.SAVE) {
                    R.drawable.ic_preset_save2
                } else {
                    R.drawable.ic_preset_load
                }
            ),
            contentDescription = "",
            modifier = Modifier
                .size(30.px.dp),
            contentScale = ContentScale.Fit
        )
    }
}


@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 PreviewPresetIconButton() {
    PresetIconButton(
        modifier = Modifier.size(50.dp),
        type = PresetButtonType.SAVE,
        onClick = {}
    )
}
