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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.HorizontalDivider
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.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.laseroptek.raman.const.temperatureTypes
import com.laseroptek.raman.data.model.serial.Temperature
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.px
import timber.log.Timber

@Composable
fun TemperatureView(
    temperatureRead: Temperature = Temperature(),
    temperatureWrite: Temperature = Temperature(),
    onClick: (Int) -> Unit = {_ -> },
) {
    Column(modifier = Modifier
        //.noRippleClickable(onClick = onClick)
        .size(388.px.dp, 276.px.dp)
        .clip(RoundedCornerShape(12.px.dp))
        .border(width = 1.px.dp, color = Color(209, 209, 209), shape = RoundedCornerShape(10.px.dp))
        .background(Color.White)
        .padding(16.px.dp),
        verticalArrangement = Arrangement.SpaceEvenly,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        //Spacer(modifier = Modifier.height(10.px.dp))

        // Temp (Title)
        SectionTitle(
            title = "Temp",
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .padding(
                    start = 20.px.dp,
                    end = 20.px.dp,
                    //bottom = 10.px.dp
                ),
        )

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 20.px.dp, end = 20.px.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Spacer(modifier = Modifier.weight(1.5f))
            Text(
                modifier = Modifier.weight(1f),
                text = "Read",
                style = RobotoTypography.labelMedium,
                fontSize = 12.px.sp,
                color = Color.Black,
                textAlign = TextAlign.End
            )
            Text(
                modifier = Modifier.weight(1f),
                text = "Write",
                style = RobotoTypography.labelMedium,
                fontSize = 12.px.sp,
                color = Color.Black,
                textAlign = TextAlign.End
            )
            Spacer(modifier = Modifier.weight(1f))
        }

        Spacer(modifier = Modifier.height(2.px.dp))

        // Tempearture
        for (i in 0..temperatureTypes.size -1) {
            val count1Value = when (i) {
                0 -> temperatureRead.ktp
                1 -> temperatureRead.chamber1
                2 -> temperatureRead.chamber2
                3 -> temperatureRead.basePlate
                4 -> temperatureRead.water
                else -> 0.0f
            }
            val count2Value = when (i) {
                0 -> temperatureWrite.ktp
                1 -> temperatureWrite.chamber1
                2 -> temperatureWrite.chamber2
                3 -> temperatureWrite.basePlate
                4 -> temperatureWrite.water
                else -> 0.0f
            }
            TwoCountItemView(
                modifier = Modifier
                    .fillMaxSize()
                    .weight(1f)
                    .padding(
                        start = 20.px.dp,
                        end = 20.px.dp,
                        //bottom = 10.px.dp
                    ),
                index = i,
                count1 = count1Value,
                count2 = count2Value,
                title = temperatureTypes[i],
                onClick = { idx ->
                    Timber.d("onClick > Temp ${temperatureTypes[i]}")
                    onClick(idx)
                }
            )

            if (i < temperatureTypes.size -1) {
                HorizontalDivider(
                    modifier = Modifier
                        .padding(start = 18.px.dp, end=18.px.dp),
                    thickness = 0.2.px.dp,
                    color = Color.LightGray
                )
            }
        }
    }
}


@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 PreviewSerialNumber() {
    TemperatureView()
}
