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

import android.content.res.Configuration
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.laseroptek.raman.R
import com.laseroptek.raman.ui.theme.PretendardTypography
import com.laseroptek.raman.utils.ext.noRippleClickable
import com.laseroptek.raman.utils.ext.px

@Composable
fun ButtonWithVIconAndText(
    text: String,
    textColor: Color = Color.Black,
    backgroundColor: Color = Color(255,204,2),
    roundedCornerShape: RoundedCornerShape = RoundedCornerShape(topStart = 110.px.dp, topEnd = 24.px.dp, bottomStart = 24.px.dp, bottomEnd = 24.px.dp),
    height: Dp = 174.dp,
    width: Dp = 483.dp,
    trailingIcon: @Composable() () -> Unit ={},
    leadingIcon: @Composable() () -> Unit ={},
    onClick: () -> Unit = {}
) {
    val annotatedString = buildAnnotatedString {
        withStyle(SpanStyle(letterSpacing = 0.1.sp)) { // Add letter spacing
            append(text)
        }
    }

    Surface(
        color = backgroundColor,
        shape = roundedCornerShape
    ){
        Box(
            modifier = Modifier
                .height(height)
                .width(width)
                //.padding(horizontal = 10.dp)
                .noRippleClickable(onClick = onClick),
            contentAlignment = Alignment.Center,
        ) {
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ){
                leadingIcon()

                Spacer(Modifier.height(12.px.dp))

                //Text(text, color = textColor, fontWeight = FontWeight.Bold, textAlign = TextAlign.Center)

                Text(
                    text = annotatedString,
                    color = textColor,
                    style = PretendardTypography.titleSmall,
                    fontSize = 24.px.sp,
                    textAlign = TextAlign.Center,
                    lineHeight = 24.px.sp
                )

                trailingIcon()
            }
        }
    }
}

@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false
)

@Composable
fun PreviewButtonWithVIconAndText() {
    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        ButtonWithVIconAndText(
            text = "TREATMENT SUMMARY",
            textColor = Color.White,
            backgroundColor = Color.Black,
            width = 188.px.dp,
            height = 174.px.dp,
            roundedCornerShape = RoundedCornerShape(24.px.dp),
            leadingIcon = {
                //Icon(Icons.Filled.HeartBroken, contentDescription = null, tint = Color.White)
                Image(
                    painter = painterResource(id = R.drawable.ic_heart),
                    contentDescription = null,
                    modifier = Modifier.size(44.px.dp)
                )
            },
            onClick = {
                // Perform action when button is clicked
                //onTreatmentSummaryClick()
            },
        )
    }

}
