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

import android.content.res.Configuration
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
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.px


@Composable
fun ImageButtonWithProgress(
    onClick: () -> Unit,
    painter: Painter,
    borderWidth: Dp = 0.dp,
    borderColor: Color = Color.Transparent,
    modifier: Modifier = Modifier,

) {
    val pulsingMinAlpha = 0.5f
    val pulsingMaxAlpha = 1.0f
    val pulsingAnimationDurationMillis: Int = 1200

    val infiniteTransition = rememberInfiniteTransition(label = "ImageButtonPulseTransition")

    IconButton(
        onClick = onClick,
        enabled = true,
        modifier = modifier
    ) {
        Image(
            painter = painter,
            contentDescription = null,
            modifier = modifier,
                //.size(width = 90.px.dp, height = 45.px.dp)
                //.alpha(pulsingAlpha)
                /*
                .then(
                    if (borderWidth > 0.dp) {
                        Modifier.border(borderWidth, borderColor, RoundedCornerShape(45.px.dp))
                    } else {
                        Modifier
                    }
                ),
                */
            contentScale = ContentScale.Crop
        )
    }
}


@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false
)
@Composable
fun ImageButtonWithProgressPreviewCompleted() {
    val isErrorState by remember { mutableStateOf(false) }
    val currentBorderColor by animateColorAsState(
        targetValue = if (isErrorState) Color.Red else MaterialTheme.colorScheme.outline,
        label = "BorderColorAnimation"
    )

    ImageButtonWithProgress(
        onClick = {  },
        painter = painterResource(id = R.drawable.ic_purge_btn),
        modifier = Modifier.size(90.px.dp, 45.px.dp)
            .clip(RoundedCornerShape(45.px.dp)),
        borderWidth = 0.5.dp,
        borderColor = currentBorderColor,
    )
}
