package com.isidroid.compose_components

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout

@Composable
fun ButtonWithProgress(
    isLoading: Boolean,
    text: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    isEnabled: Boolean = true,
) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = modifier
    ) {
        Button(
            modifier = Modifier.defaultMinSize(minHeight = 44.dp),
            enabled = isEnabled && !isLoading,
            onClick = onClick,
            shape = RoundedCornerShape(8.dp),
        ) {
            ConstraintLayout(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight()
            ) {
                val (textView, progressView) = createRefs()

                Text(
                    text = text,
                    modifier = Modifier
                        .constrainAs(textView) {
                            linkTo(start = parent.start, end = parent.end)
                            linkTo(top = parent.top, bottom = parent.bottom)
                        },
                    fontSize = 16.sp,
                    letterSpacing = .1.sp
                )

                if (isLoading)
                    CircularProgressIndicator(
                        strokeWidth = 3.dp,
                        modifier = Modifier
                            .padding(start = 12.dp)
                            .size(24.dp)
                            .constrainAs(progressView) {
                                start.linkTo(textView.end)
                                linkTo(top = parent.top, bottom = parent.bottom)
                            },
                    )
            }
        }
    }
}

@Composable
@Preview(showBackground = true, backgroundColor = 0xFFFFFF)
private fun ButtonWithProgressPreview() {
    ButtonWithProgress(
        isLoading = true,
        text = "Send code",
        onClick = {},
        modifier = Modifier.fillMaxWidth(),
    )
}


@Composable
@Preview(showBackground = true, backgroundColor = 0xFFFFFF)
private fun ButtonWithProgressNoLoadingPreview() {
    ButtonWithProgress(
        modifier = Modifier.fillMaxWidth(),
        isLoading = false,
        text = "Send code",
        onClick = {}
    )
}