package com.aiosman.riderpro.ui.composables

import androidx.annotation.DrawableRes
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.ColumnScopeInstance.weight
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
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.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.riderpro.ui.modifiers.noRippleClickable

@Composable
fun ActionButton(
    modifier: Modifier = Modifier,
    text: String,
    color: Color = Color.Black,
    @DrawableRes backgroundImage: Int? = null,
    backgroundColor: Color = Color(0xfff0f0f0),
    leading: @Composable (() -> Unit)? = null,
    expandText: Boolean = false,
    contentPadding: PaddingValues = PaddingValues(vertical = 16.dp),
    isLoading: Boolean = false,
    loadingTextColor: Color = Color.White,
    loadingText: String = "Loading",
    loadingBackgroundColor: Color = Color(0xFFD95757),
    disabledBackgroundColor: Color = Color(0xFFD0D0D0),
    enabled: Boolean = true,
    fullWidth:Boolean = false,
    click: () -> Unit = {}
) {
    val animatedBackgroundColor by animateColorAsState(
        targetValue = run{
            if (enabled) {
                if (isLoading) {
                    loadingBackgroundColor
                } else {
                    backgroundColor
                }
            } else {
                disabledBackgroundColor
            }
        },
        animationSpec = tween(300), label = ""
    )
    Box(
        modifier = modifier
            .clip(RoundedCornerShape(24.dp))
            .background(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 = 17.sp,
                    color = color,
                    fontWeight = FontWeight.W900,
                    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 = Color.White
                    )
                    Text(
                        loadingText,
                        modifier = Modifier.fillMaxWidth(),
                        textAlign = TextAlign.Center,
                        color = loadingTextColor
                    )
                }
            }
        }


    }
}