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.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.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.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.laseroptek.raman.const.lifeTimeTypes
import com.laseroptek.raman.data.model.LifeTime
import com.laseroptek.raman.utils.ext.px
import timber.log.Timber

@Composable
fun LifeTimeView(
    lifeTime: LifeTime = LifeTime(),
    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))

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

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

        // Temp 0..7
        for (i in 0..lifeTimeTypes.size -1) {
            val hour = when (i) {
                0 -> lifeTime.lamp
                1 -> lifeTime.hp5x5
                2 -> lifeTime.hp7x7
                3 -> lifeTime.hp10x10
                4 -> lifeTime.hp12x12
                5 -> lifeTime.hp3x15
                6 -> lifeTime.detector
                7 -> lifeTime.water
                else -> 0
            }
            HourItemView(
                modifier = Modifier
                    .fillMaxSize()
                    .weight(1f)
                    .padding(
                        start = 20.px.dp,
                        end = 20.px.dp,
                        //bottom = 10.px.dp
                    ),
                title = lifeTimeTypes[i],
                hour = hour,
                onClick = {
                    Timber.d("onClick > Temp $i (${lifeTimeTypes[i]})")
                    onClick.invoke(i)
                }
            )

            if (i < lifeTimeTypes.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 PreviewLifeTimeView() {
    LifeTimeView()
}