package com.aiosman.ravenow.ui.composables

//import androidx.compose.foundation.layout.ColumnScopeInstance.weight
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
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.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.ui.modifiers.noRippleClickable

@Composable
fun ActionButton(
    modifier: Modifier = Modifier,
    text: String,
    color: Color? = null,
    backgroundColor: Color? = null,
    backgroundBrush: Brush? = null,
    leading: @Composable (() -> Unit)? = null,
    expandText: Boolean = false,
    contentPadding: PaddingValues = PaddingValues(vertical = 16.dp),
    isLoading: Boolean = false,
    loadingTextColor: Color? = null,
    loadingText: String = "Loading",
    loadingBackgroundColor: Color? = null,
    disabledBackgroundColor: Color? = null,
    enabled: Boolean = true,
    fullWidth: Boolean = false,
    roundCorner: Float = 24f,
    fontSize: TextUnit = 17.sp,
    fontWeight: FontWeight = FontWeight.W900,
    click: () -> Unit = {}
) {
    val AppColors = LocalAppTheme.current
    val animatedBackgroundColor by animateColorAsState(
        targetValue = run {
            if (enabled) {
                if (isLoading) {
                    loadingBackgroundColor ?: AppColors.loadingMain
                } else {
                    backgroundColor ?: AppColors.basicMain
                }
            } else {
                disabledBackgroundColor ?: AppColors.disabledBackground
            }
        },
        animationSpec = tween(300), label = ""
    )
    Box(
        modifier = modifier
            .clip(RoundedCornerShape(roundCorner.dp))
            .background(
                brush = backgroundBrush ?: Brush.linearGradient(
                    colors = listOf(animatedBackgroundColor, animatedBackgroundColor)
                )
            )
            .noRippleClickable {
                if (enabled && !isLoading) {
                    click()
                }
            }
            .padding(contentPadding),

        contentAlignment = Alignment.CenterStart
    ) {
        if (!isLoading) {
            Box(
                modifier = Modifier
                    .align(Alignment.Center)
                    .let {
                        if (fullWidth) {
                            it.fillMaxWidth()
                        } else {
                            it
                        }
                    },
                contentAlignment = Alignment.Center
            ) {
                Box(
                    modifier = Modifier.fillMaxWidth(),
                    contentAlignment = Alignment.CenterStart
                ) {
                    Box(modifier = Modifier.align(Alignment.CenterStart)) {
                        leading?.invoke()
                    }
                }

                Text(
                    text,
                    fontSize = fontSize,
                    color = color ?: AppColors.text,
                    fontWeight = fontWeight,
                    textAlign = if (expandText) TextAlign.Center else TextAlign.Start
                )
            }
        } else {
            Box(
                modifier = Modifier
                    .let {
                        if (fullWidth) {
                            it.fillMaxWidth()
                        } else {
                            it
                        }
                    }
                    .padding(horizontal = 16.dp),
                contentAlignment = Alignment.Center
            ) {
                Box(
                    modifier = Modifier.fillMaxWidth(),
                    contentAlignment = Alignment.CenterStart
                ) {
                    CircularProgressIndicator(
                        modifier = Modifier.size(24.dp),
                        color = AppColors.text
                    )
                    Text(
                        loadingText,
                        modifier = Modifier.fillMaxWidth(),
                        textAlign = TextAlign.Center,
                        color = loadingTextColor ?: AppColors.loadingText,
                    )
                }
            }
        }


    }
}