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

import android.content.res.Configuration
import androidx.compose.foundation.background
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.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.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
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.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import com.laseroptek.raman.R
import com.laseroptek.raman.const.dcdCanTypeFormatedLists
import com.laseroptek.raman.const.dcdCanTypeLists
import com.laseroptek.raman.const.dcdLifeSpanAdjustLists
import com.laseroptek.raman.const.dcdLifeSpanAdjustStringLists
import com.laseroptek.raman.data.model.DcdType
import com.laseroptek.raman.ui.common.core.WheelPickerDefaults
import com.laseroptek.raman.ui.common.core.WheelTextPicker
import com.laseroptek.raman.ui.common.fullscreen.FullScreen
import com.laseroptek.raman.ui.screens.main.MainViewModel
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.dropShadow
import com.laseroptek.raman.utils.ext.px
import timber.log.Timber

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DcdConfigPopup(
    mainViewModel: MainViewModel = hiltViewModel<MainViewModel>(),
    //showDcdConfigPopup: MutableState<Boolean> = mutableStateOf(false),
    title: String = stringResource(R.string.dcd_title),
    //dcdType: MutableStateFlow<DcdType> = MutableStateFlow(DcdType()),
    onClick: (Boolean) -> Unit = {},
) {
    val dcdType by mainViewModel.dcdType.collectAsState()

    FullScreen(contentAlignment= Alignment.Center) {
        Column(
            modifier = Modifier
                .size(384.px.dp, 336.px.dp)
                .clip(RoundedCornerShape(28.px.dp))
                .background(Color.White)
                .dropShadow(
                    shape = RoundedCornerShape(16.px.dp),
                    color = Color.Black.copy(alpha = 0.2f),
                    blur = 48.px.dp,
                    offsetX = 0.px.dp,
                    offsetY = 24.px.dp,
                    spread = 0.px.dp
                )
                .dropShadow(
                    shape = RoundedCornerShape(16.px.dp),
                    color = Color.Black.copy(alpha = 0.1f),
                    blur = 6.px.dp,
                    offsetX = 0.px.dp,
                    offsetY = 3.px.dp,
                    spread = 0.px.dp
                )
                .padding(5.px.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Top
        ) {
            // Title
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(60.px.dp)
                    .padding(start = 24.px.dp, end = 24.px.dp),
                horizontalArrangement = Arrangement.Start,
                verticalAlignment = Alignment.CenterVertically
            ){
                Text(
                    text = title,
                    style = RobotoTypography.headlineLarge,
                    fontWeight = FontWeight.ExtraLight,
                    fontSize = 24.px.sp,
                    color = Color.Black,
                    textAlign = TextAlign.Center,
                )
            }

            // Sub Title (108 + x (=36) + 192) = 336
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(176.px.dp)
                    .padding(start = 24.px.dp, end = 24.px.dp),
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ){
                // Can Type
                Column(
                    modifier = Modifier
                        .size(108.px.dp, 176.px.dp)
                        .clip(RoundedCornerShape(28.px.dp))
                        .padding(5.px.dp),
                    horizontalAlignment = Alignment.Start,
                    verticalArrangement = Arrangement.Center
                ) {
                    // SubTitle - CAN Type
                    Text(
                        text = stringResource(R.string.can_type_title),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.ExtraLight,
                        fontSize = 14.px.sp,
                        color = Color(73,69,79),
                        textAlign = TextAlign.Center,
                    )

                    Spacer(Modifier.weight(1f))

                    // Wheel Picker
                    WheelTextPicker(
                        modifier = Modifier.size(108.px.dp, 148.px.dp),
                        startIndex = dcdCanTypeLists.indexOf(dcdType.canType).takeIf {it != -1} ?: 0,
                        size = DpSize(108.px.dp, 148.px.dp),
                        texts = dcdCanTypeFormatedLists,
                        rowCount = 3,
                        style = RobotoTypography.bodyMedium.copy(fontSize = 14.px.sp),
                        color = Color.Black,
                        selectorProperties = WheelPickerDefaults.selectorProperties(
                            enabled = true,
                            color = Color(222,173,11),
                            width = 1.0f
                        ),
                        onScrollFinished = { snappedIndex ->
                            Timber.d("snappedIndex: $snappedIndex")
                            if (snappedIndex > 0 && snappedIndex <= dcdCanTypeLists.size) {
                                val newDcdType = DcdType(
                                    canType = dcdCanTypeLists[snappedIndex-1],
                                    lifeSpan = dcdType.lifeSpan
                                )
                                mainViewModel.setDcdType(newDcdType.copy())
                                Timber.d("dcdType.value.canType : $dcdType.value.canType ")
                            }
                            return@WheelTextPicker snappedIndex
                        }
                    )
                }

                Spacer(Modifier.weight(1f))

                // Divider
                VerticalDivider(
                    modifier = Modifier
                        .padding(top = 18.px.dp, bottom =18.px.dp),
                    thickness = 0.2.px.dp,
                    color = Color.LightGray
                )

                Spacer(Modifier.weight(1f))

                // DCD Lifespan Adjust
                Column(
                    modifier = Modifier
                        .size(192.px.dp, 176.px.dp)
                        .clip(RoundedCornerShape(28.px.dp))
                        .padding(5.px.dp),
                    horizontalAlignment = Alignment.Start,
                    verticalArrangement = Arrangement.Center
                ) {
                    // SubTitle - Lifespan Adjust
                    Text(
                        text = stringResource(R.string.dcd_liftspan_adjust_title),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.ExtraLight,
                        fontSize = 14.px.sp,
                        color = Color(73,69,79),
                        textAlign = TextAlign.Center,
                    )

                    Spacer(Modifier.weight(1f))

                    // Wheel Picker - DCD_Lifespan_Adjust_Layer
                    WheelTextPicker(
                        modifier = Modifier.size(108.px.dp, 148.px.dp),
                        startIndex = dcdLifeSpanAdjustLists.indexOf(dcdType.lifeSpan).takeIf { it != -1 } ?: 0,
                        size = DpSize(108.px.dp, 148.px.dp),
                        texts = dcdLifeSpanAdjustStringLists,
                        rowCount = 3,
                        style = RobotoTypography.bodyMedium.copy(fontSize = 14.px.sp),
                        color = Color.Black,
                        selectorProperties = WheelPickerDefaults.selectorProperties(
                            enabled = true,
                            color = Color(222,173,11),
                            width = 1.0f
                        ),
                        onScrollFinished = { snappedIndex ->
                            Timber.d("snappedIndex: $snappedIndex")
                            if (snappedIndex > 0 && snappedIndex <= dcdLifeSpanAdjustLists.size) {
                                Timber.d("snappedIndex: $snappedIndex")
                                val newDcdType = DcdType(
                                    canType = dcdType.canType,
                                    lifeSpan = dcdLifeSpanAdjustLists[snappedIndex-1]
                                )
                                mainViewModel.setDcdType(newDcdType.copy())
                            }

                            return@WheelTextPicker snappedIndex
                        }
                    )
                }
            }

            Spacer(Modifier.weight(1f))

            // Cancel & OK
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(88.px.dp)
                    .padding(start = 10.px.dp, end = 10.px.dp),
                horizontalArrangement = Arrangement.End,
                verticalAlignment = Alignment.CenterVertically
            ) {
                // Cancel
                TextButton(
                    modifier = Modifier
                        .size(68.px.dp, 40.px.dp),
                    onClick = {
                        Timber.d("onLoadClick")
                        onClick(false)
                    }
                ) {
                    Text(
                        text = stringResource(R.string.cancel),
                        style = RobotoTypography.labelLarge,
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.px.sp,
                        color = Color.Black
                    )
                }

                // Ok
                TextButton(
                    modifier = Modifier
                        .size(68.px.dp, 40.px.dp),
                    onClick = {
                        Timber.d("Ok Clicked")
                        onClick(true)
                    }
                ) {
                    Text(
                        text = stringResource(R.string.ok),
                        style = RobotoTypography.labelLarge,
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.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 PreviewDCDConfigView() {
    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        DcdConfigPopup()
    }
}
