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.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
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.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
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.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.Energies
import com.laseroptek.raman.const.PulseDurations
import com.laseroptek.raman.const.VoltageTable
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.noRippleClickable
import com.laseroptek.raman.utils.ext.px
import com.laseroptek.raman.utils.ext.toVoltageMutableMap
import kotlinx.coroutines.flow.MutableStateFlow
import timber.log.Timber


@Composable
fun EnergyVoltageView(
    voltageTable: MutableMap<Pair<Float, Float>, MutableStateFlow<Int>> = VoltageTable.toVoltageMutableMap(),
    selectedCalibPulseWidthIndex: Int = 0,
    isVoltageSelectedElseReptition: Boolean = true,
    selectedCalibVoltageIndex: Int = 0,
    onClick: (Int) -> Unit = {}
) {
    Row {
        // L: Energy (Fluence) (J)
        Column(modifier = Modifier
            .fillMaxWidth()
            .weight(1f)
            .height(350.px.dp)
        ) {
            // Energy (Fluence) (J) - 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 = "Energy (J)",
                    style = RobotoTypography.bodyMedium,
                    fontWeight = FontWeight.Normal,
                    fontSize = 12.px.sp,
                    color = Color.White
                )
            }

            // Energy (= Fluences, J) - value
            for (i in 0..Energies.size -1) {
                Column(
                    Modifier
                        .fillMaxWidth()
                        .fillMaxHeight()
                        .weight(1f)
                        .border(0.5.px.dp, color = Color(209, 209, 209), shape = RectangleShape)
                        .background(Color.White),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Text(
                        text = Energies[i].toString(),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 12.px.sp,
                        color = Color.Black
                    )
                }
            }
        }

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

        // R: Voltage (V)
        Column(modifier = Modifier
            .fillMaxWidth()
            .weight(1f)
            .height(350.px.dp)
        ) {
            // Voltage - 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 = "Voltage (V)",
                    style = RobotoTypography.bodyMedium,
                    fontWeight = FontWeight.Normal,
                    fontSize = 12.px.sp,
                    color = Color.White
                )
            }

            // V - Voltage - value
            for (i in 0..Energies.size -1 ) {
                Column(
                    Modifier
                        .fillMaxWidth()
                        .fillMaxHeight()
                        .weight(1f)
                        .border(0.5.px.dp, color = Color(209, 209, 209), shape = RectangleShape)
                        .padding(1.px.dp)
                        .background(
                            if (isVoltageSelectedElseReptition && i == selectedCalibVoltageIndex)
                                Color(255, 204, 0)
                            else
                                Color.White
                        )
                        .noRippleClickable(onClick = { onClick.invoke(i) }),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Timber.d("-----------------------------------------------------------------")
                    Timber.d("selectedCalibPulseWidthIndex: ${selectedCalibPulseWidthIndex}")
                    Timber.d("PulseDurations(index): ${PulseDurations.get(selectedCalibPulseWidthIndex)}")
                    Timber.d("Energies(i): ${Energies.get(i)}")

                    val key = Pair(
                        PulseDurations.get(selectedCalibPulseWidthIndex),
                        Energies.get(i)
                    )

                    val innerStateFlow = remember(key) { voltageTable[key] ?: MutableStateFlow(0) }
                    val voltage by innerStateFlow.collectAsState()

                    Timber.d("-----------------------------------------------------------------")
                    Timber.d("voltage: ${voltage}")
                    Timber.d("")

                    Text(
                        text = voltage.toString(),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 12.px.sp,
                        color = Color.Black
                    )
                }
            }
        }
    }
}


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