package com.laseroptek.raman.ui.common.button

import android.content.res.Configuration
import androidx.annotation.DrawableRes
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.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
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.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.laseroptek.raman.R
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

@Composable
fun GradientImageButton(
    externalModifier: Modifier = Modifier,
    @DrawableRes id: Int = R.drawable.ic_arrow_down,
    buttonWidth: Dp = 24.px.dp,
    buttonHeight: Dp = 24.px.dp,
    iconWidth: Dp = 16.px.dp,
    iconHeight: Dp = 16.px.dp,
    onClick: () -> Unit = {}
) {
    val localModifier = Modifier
        .noRippleClickable {
            onClick.invoke()
        }
        .size(buttonWidth, buttonHeight)
        .innerShadow(
            color = Color(255,255,255),
            blur = 1.1.px.dp,
            offsetX = 0.px.dp,
            offsetY = 1.1.px.dp,
            spread = 0.px.dp,
            shape = RoundedCornerShape(6.px.dp),
        )
        .dropShadow(
            color = Color(0,0,0).copy(alpha = 0.1f),
            blur = 3.43.px.dp,
            offsetX = 0.px.dp,
            offsetY = 0.68.px.dp,
            spread = 0.px.dp,
            shape = RoundedCornerShape(6.px.dp),
        )
        .border(
            width = 1.5.px.dp,
            brush = Brush.linearGradient(
                colors = listOf(
                    Color(222, 173, 11),
                    Color(75, 63, 23),
                )
            ),
            shape = RoundedCornerShape(6.px.dp)
        )
        .background(
            brush = Brush.verticalGradient(
                colors = listOf(
                    Color(255, 255, 255),
                    Color(248, 245, 238),
                )
            ),
            shape = RoundedCornerShape(6.px.dp)
        )
        .clip(RoundedCornerShape(6.px.dp))
    Row(
        modifier = localModifier.then(externalModifier),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            modifier = Modifier.size(iconWidth, iconHeight),
            painter = painterResource(id = id),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            //colorFilter = ColorFilter.tint(Color.White)
        )
    }

}

@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false
)
@Composable
fun PreviewGradientImageButton() {
    Row {
        GradientImageButton(
            externalModifier = Modifier,
            id = R.drawable.ic_arrow_up,
            /*
            buttonWidth = 32.px.dp,
            buttonHeight = 32.px.dp,
            iconWidth = 16.px.dp,
            iconHeight = 16.px.dp,
            */
            onClick = {
                // Perform action when button is clicked
            }
        )

        Spacer(modifier = Modifier.width(10.px.dp))

        GradientImageButton(
            externalModifier = Modifier,
            id = R.drawable.ic_arrow_down,
            /*
            buttonWidth = 32.px.dp,
            buttonHeight = 32.px.dp,
            iconWidth = 16.px.dp,
            iconHeight = 16.px.dp,
            */
            onClick = {
                // Perform action when button is clicked
            }
        )
    }

}
