package com.laseroptek.raman.ui.screens.engineer

import android.content.res.Configuration
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
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.graphics.RectangleShape
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.laseroptek.raman.R
import com.laseroptek.raman.const.PulseDurations
import com.laseroptek.raman.ui.theme.PretendardTypography
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.noRippleClickable
import com.laseroptek.raman.utils.ext.px


@Composable
fun PulseWidthView(
    onClick: (Int) -> Unit = {},
    selectedCalibPulseWidthIndex: Int = 0,
    title:String = stringResource(R.string.pulse_width_title)
) {
    Column(
        Modifier
            .fillMaxWidth()
            .height(52.px.dp)
    ) {
        // Pulse-width title
        Row(
            Modifier
                .fillMaxWidth()
                .height(22.px.dp)
                .clip(RoundedCornerShape(topStart = 11.px.dp, topEnd = 11.px.dp))
                .background(Color.Black),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = title,
                color = Color.White,
                style = PretendardTypography.titleSmall,
                fontWeight = FontWeight.ExtraLight,
                fontSize = 14.px.sp,
            )
        }

        // Pulse-width value
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(30.px.dp)
                .border(1.dp, color = Color.Gray, shape = RectangleShape),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            for (i in 0..PulseDurations.size -1) {

                val text = if (PulseDurations[i] > 9) {
                    "${PulseDurations[i].toInt()}"
                } else {
                    "${PulseDurations[i]}"
                }

                Column(
                    Modifier
                        .noRippleClickable(onClick = { onClick.invoke(i) })
                        .fillMaxWidth()
                        .weight(1f)
                        .height(31.px.dp)
                        .border(0.5.dp, color = Color.Gray, shape = RectangleShape)
                        .background(
                            if (i == selectedCalibPulseWidthIndex) Color(
                                255,
                                204,
                                0
                            ) else Color.White
                        ),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Text(
                        text = text,
                        color = Color.Black,
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.ExtraLight,
                        fontSize = 14.px.sp,
                    )
                }
            }
        }
    }
}


@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=640dp,height=1080dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)
@Composable
fun PreviewPulseWidthView() {
    Row {
        PulseWidthView()
    }
}