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.layout.padding
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.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.Repetitions
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 RepetitionView(
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {},
    selectedCalibRepetitionIndex: Int = 0,
    isVoltageSelectedElseReptition: Boolean = false,
    title:String = ""
) {
    Column(modifier = modifier
        .border(
            width = 1.dp,
            color = Color(209, 209, 209),
            shape = RoundedCornerShape(10.px.dp)
        )
    ) {
        // title
        Row(
            Modifier
                .fillMaxWidth()
                .height(22.px.dp)
                .clip(RoundedCornerShape(topStart = 10.px.dp, topEnd = 10.px.dp))
                .background(Color(243, 205, 81)),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = stringResource(R.string.repetition),
                color = Color.Black,
                style = RobotoTypography.labelMedium,
                fontWeight = FontWeight.ExtraLight,
                fontSize = 12.px.sp,
            )
        }

        // value
        Row(
            Modifier
                .fillMaxWidth()
                .height(26.px.dp)
                .clip(RoundedCornerShape(bottomStart = 10.px.dp, bottomEnd = 10.px.dp))
                .padding(1.px.dp)
                .background(if (!isVoltageSelectedElseReptition) Color.Green.copy(alpha = 0.1f) else Color.White)
                .noRippleClickable(onClick = { onClick.invoke() }),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Timber.d("%.1f", Repetitions.get(selectedCalibRepetitionIndex))
            Text(
                text = "%.1f".format(Repetitions.get(selectedCalibRepetitionIndex)),
                color = Color.Black,
                style = RobotoTypography.labelMedium,
                fontWeight = FontWeight.ExtraLight,
                fontSize = 14.px.sp,
            )
        }
    }
}


@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 PreviewRepetitionView() {
    Row {
        RepetitionView(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .height(48.px.dp),
            title = "Temp 1"
        )
    }
}