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

import android.content.res.Configuration
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.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
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.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
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.const.MAX_Q_SWITCH_VALUE
import com.laseroptek.raman.const.MIN_Q_SWITCH_VALUE
import com.laseroptek.raman.data.model.serial.QSwitch
import com.laseroptek.raman.data.model.serial.Q_SWITCH_TOGGLE
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.noRippleClickable
import com.laseroptek.raman.utils.ext.px
import timber.log.Timber

@Composable
fun QSwitchView(
    modifier: Modifier = Modifier,
    qSwitch: QSwitch = QSwitch(),
    onClick: (QSwitch) -> Unit = {},
    title:String = "Q-S/W(main)"
) {
    var qSwitchToggle by remember { mutableStateOf(0) }  // 0: +0.5, 1: +10, 2: -10, 3: -0.5
    //val qSwitchSubToggle = mutableStateOf(0)   // 0: +0.5, 1: +10, 2: -10, 3: -0.5

    fun onValueChange(currentValue: Float, createNewQSwitch: (Float) -> QSwitch) {
        val newValue = when (qSwitchToggle) {
            0 -> currentValue + 0.5f
            1 -> currentValue + 10.0f
            2 -> currentValue - 10.0f
            3 -> currentValue - 0.5f
            else -> currentValue
        }

        // Clamp the value to ensure it stays within the valid range (e.g., 0 to 250)
        val clampedValue = newValue.coerceIn(MIN_Q_SWITCH_VALUE, MAX_Q_SWITCH_VALUE)

        // Only call onClick if the value has actually changed
        if (clampedValue != currentValue) {
            onClick(createNewQSwitch(clampedValue))
        }
    }

    Row(modifier = modifier,
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        // Q-S/W  - title
        Column(modifier = Modifier
            .fillMaxWidth()
            .weight(1f)
            .height(30.px.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.Start
        ) {
            Text(
                text = title,
                style = RobotoTypography.labelMedium,
                fontWeight = FontWeight.Normal,
                fontSize = 12.px.sp,
                color = Color.Black
            )
        }

        Spacer(modifier = Modifier.width(10.px.dp))

        // Q-S/W - Delay
        Column(
            modifier = Modifier
                .noRippleClickable(onClick = {
                    onValueChange(qSwitch.delayTime) { newDelay ->
                        qSwitch.copy(delayTime = newDelay)
                    }
                })
                .fillMaxWidth()
                .weight(1f)
                .height(30.px.dp)
                .border(width = 1.px.dp, color = Color.Gray, RoundedCornerShape(5.px.dp)),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Delay: %.1f μs".format(qSwitch.delayTime), //"DELAY : -.- μs",
                style = RobotoTypography.bodyMedium,
                fontWeight = FontWeight.Normal,
                fontSize = 11.px.sp,
                color = Color.Black
            )
        }

        Spacer(modifier = Modifier.width(10.px.dp))

        // Q-S/W - Interval
        Column(
            modifier = Modifier
                .noRippleClickable(onClick = {
                    onValueChange(qSwitch.intervalTime) { newDelay ->
                        qSwitch.copy(intervalTime = newDelay)
                    }
                })
                .fillMaxWidth()
                .weight(1f)
                .height(30.px.dp)
                .border(width = 1.px.dp, color = Color.Gray, RoundedCornerShape(5.px.dp)),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Interval: %.1f μs".format(qSwitch.intervalTime), //"INTERVAL : -.- μs",
                style = RobotoTypography.bodyMedium,
                fontWeight = FontWeight.Normal,
                fontSize = 11.px.sp,
                color = Color.Black
            )
        }

        Spacer(modifier = Modifier.width(10.px.dp))

        // Q-S/W toggle
        Column(
            modifier = Modifier
                .noRippleClickable(onClick = {
                    qSwitchToggle = (qSwitchToggle+1) % 4
                    Timber.d("onClick > qSwitchToggle.value : ${qSwitchToggle}")
                })
                .size(40.px.dp, 30.px.dp)
                .border(width = 1.px.dp, color = Color.Gray, RoundedCornerShape(5.px.dp)),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = Q_SWITCH_TOGGLE.get(qSwitchToggle),
                style = RobotoTypography.bodyMedium,
                fontWeight = FontWeight.ExtraLight,
                fontSize = 11.px.sp,
                color = Color.Black
            )
        }
    }
}


@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=640dp,height=60dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)
@Composable
fun PreviewQSwitchView() {
    Row {
        QSwitchView(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f),
            title = "Q-S/W (main)"
        )
    }
}