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

import android.content.res.Configuration
import androidx.compose.foundation.Image
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.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.VerticalDivider
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.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
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.dp
import androidx.compose.ui.unit.sp
import com.laseroptek.raman.R
import com.laseroptek.raman.const.dcdDelayValues
import com.laseroptek.raman.const.dcdSprayValues
import com.laseroptek.raman.data.datasource.db.RamanDatabase
import com.laseroptek.raman.data.model.serial.SprayDcd
import com.laseroptek.raman.data.source.db.RamanDatabaseService
import com.laseroptek.raman.repository.DatabaseRepository
import com.laseroptek.raman.repository.PreferenceRepository
import com.laseroptek.raman.repository.SerialPortRepository
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.DefaultDispatcherProvider
import com.laseroptek.raman.utils.ext.dropShadow
import com.laseroptek.raman.utils.ext.noRippleClickable
import com.laseroptek.raman.utils.ext.px
import timber.log.Timber

@Composable
fun DcdOptionDialog(
    optionValue: SprayDcd = SprayDcd(),
    onClick: (Boolean) -> Unit= {},
    onClickSprayTime: (Int) -> Unit = {},
    onClickSprayDelay: (Int) -> Unit = {}
) {
    FullScreen {
        Column(
            modifier = Modifier
                .size(392.px.dp, 184.px.dp)
                .clip(RoundedCornerShape(28.px.dp))
                .background(Color.White)
                .dropShadow(
                    shape = RoundedCornerShape(28.px.dp),
                    color = Color.Black.copy(alpha = 0.1f),
                    blur = 48.px.dp,
                    offsetX = 0.px.dp,
                    offsetY = 24.px.dp,
                    spread = 0.px.dp
                )
                .dropShadow(
                    shape = RoundedCornerShape(28.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(end = 10.px.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // Upper
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(96.px.dp)
                    .padding(start = 10.px.dp, end = 10.px.dp, top = 20.px.dp),
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ) {
                // Spray
                Column(
                    modifier = Modifier
                        .size(148.px.dp, 72.px.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    // title
                    Text(
                        text = stringResource(R.string.spray_title),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.px.sp,
                        color = Color(73,69,79),
                        textAlign = TextAlign.Start
                    )

                    Spacer(modifier = Modifier.weight(1f))

                    // values
                    Row(
                        modifier = Modifier
                            .size(148.px.dp, 36.px.dp),
                        horizontalArrangement = Arrangement.Center,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        // <
                        Row(
                            modifier = Modifier
                                .noRippleClickable {
                                    val idx = dcdSprayValues.indexOf( optionValue.sprayTime.toString() )
                                    Timber.d("idx : ${idx}")
                                    if (idx > 0) {
                                        //optionValue.sprayTime = dcdSprayValues[idx - 1].toInt()
                                        /*
                                        optionValue = optionValue.copy(
                                            sprayTime = dcdSprayValues[idx - 1].toInt()
                                        )
                                        */
                                        val sprayTime = dcdSprayValues[idx - 1].toInt()
                                        onClickSprayTime(sprayTime)
                                    }
                                    Timber.d("onClickSprayTime Down -> optionValue : ${optionValue}")
                                }
                                .size(28.px.dp)
                                .clip(RoundedCornerShape(8.px.dp))
                                .background(
                                    brush = Brush.linearGradient(
                                        colors = listOf(
                                            Color(255, 255, 255),
                                            Color(248, 245, 238),
                                        )
                                    )
                                )
                                .border(
                                    width = 1.4.px.dp,
                                    color = Color(222,173,11),
                                    shape = RoundedCornerShape(8.px.dp)
                                ),
                            horizontalArrangement = Arrangement.Center,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Image(
                                painter = painterResource(id = R.drawable.ic_left_yellow),
                                contentDescription = null,
                                modifier = Modifier.size(16.px.dp),
                                contentScale = ContentScale.Crop
                            )
                        }

                        Spacer(modifier = Modifier.weight(1f))

                        // Text
                        Text(
                            text = "%d".format(optionValue.sprayTime),
                            style = RobotoTypography.bodyMedium,
                            fontWeight = FontWeight.Normal,
                            fontSize = 36.px.sp,
                            color = Color.Black,
                            textAlign = TextAlign.Center
                        )

                        Spacer(modifier = Modifier.weight(1f))

                        // >
                        Row(
                            modifier = Modifier
                                .noRippleClickable {
                                    val idx = dcdSprayValues.indexOf( optionValue.sprayTime.toString() )
                                    Timber.d("idx : ${idx}")
                                    if (idx < dcdSprayValues.size - 1) {
                                        //optionValue.sprayTime = dcdSprayValues[idx + 1].toInt()
                                        /*
                                        optionValue = optionValue.copy(
                                            sprayTime = dcdSprayValues[idx + 1].toInt()
                                        )
                                        */
                                        val sprayTime = dcdSprayValues[idx + 1].toInt()
                                        onClickSprayTime(sprayTime)
                                        Timber.d("onClickSprayTime UP -> optionValue : ${optionValue}")
                                    }
                                }
                                .size(28.px.dp)
                                .clip(RoundedCornerShape(8.px.dp))
                                .background(
                                    brush = Brush.linearGradient(
                                        colors = listOf(
                                            Color(255, 255, 255),
                                            Color(248, 245, 238),
                                        )
                                    )
                                )
                                .border(
                                    width = 1.4.px.dp,
                                    color = Color(222,173,11),
                                    shape = RoundedCornerShape(8.px.dp)
                                ),
                            horizontalArrangement = Arrangement.Center,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Image(
                                painter = painterResource(id = R.drawable.ic_right_yellow),
                                contentDescription = null,
                                modifier = Modifier.size(16.px.dp),
                                contentScale = ContentScale.Crop
                            )
                        }
                    }
                }

                // Divider
                Spacer(Modifier.width(25.px.dp))
                VerticalDivider(
                    modifier = Modifier.size(1.px.dp, 72.px.dp),
                    thickness = 1.px.dp,
                    color = Color(209,209,209)
                )
                Spacer(Modifier.width(25.px.dp))

                // Delay
                Column(
                    modifier = Modifier
                        .size(148.px.dp, 72.px.dp)
                        .padding(start = 10.px.dp, end = 10.px.dp),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    // title
                    Text(
                        text = stringResource(R.string.delay_title),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.px.sp,
                        color = Color(73,69,79),
                        textAlign = TextAlign.Start
                    )

                    Spacer(modifier = Modifier.weight(1f))

                    // values
                    Row(
                        modifier = Modifier
                            .size(148.px.dp, 36.px.dp),
                        horizontalArrangement = Arrangement.Center,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        // <
                        Row(
                            modifier = Modifier
                                .noRippleClickable {
                                    val idx = dcdDelayValues.indexOf( optionValue.sprayDelay.toString() )
                                    Timber.d("idx : ${idx}")
                                    if (idx > 0) {
                                        //optionValue.sprayDelay = dcdDelayValues[idx - 1].toInt()
                                        /*
                                        optionValue = optionValue.copy(
                                            sprayDelay = dcdDelayValues[idx - 1].toInt()
                                        )
                                        */
                                        val sprayDelay = dcdDelayValues[idx - 1].toInt()
                                        onClickSprayDelay(sprayDelay)
                                        Timber.d("onClickSprayDelay Down -> optionValue : ${optionValue}")
                                    }
                                }
                                .size(28.px.dp)
                                .clip(RoundedCornerShape(8.px.dp))
                                .background(
                                    brush = Brush.linearGradient(
                                        colors = listOf(
                                            Color(255, 255, 255),
                                            Color(248, 245, 238),
                                        )
                                    )
                                )
                                .border(
                                    width = 1.4.px.dp,
                                    color = Color(222,173,11),
                                    shape = RoundedCornerShape(8.px.dp)
                                ),
                            horizontalArrangement = Arrangement.Center,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Image(
                                painter = painterResource(id = R.drawable.ic_left_yellow),
                                contentDescription = null,
                                modifier = Modifier.size(16.px.dp),
                                contentScale = ContentScale.Crop
                            )
                        }

                        Spacer(modifier = Modifier.weight(1f))

                        // Text
                        Text(
                            text = "%d".format(optionValue.sprayDelay),
                            style = RobotoTypography.bodyMedium,
                            fontWeight = FontWeight.Normal,
                            fontSize = 36.px.sp,
                            color = Color.Black,
                            textAlign = TextAlign.Center
                        )

                        Spacer(modifier = Modifier.weight(1f))

                        // >
                        Row(
                            modifier = Modifier
                                .noRippleClickable {
                                    val idx = dcdDelayValues.indexOf( optionValue.sprayDelay.toString() )
                                    Timber.d("idx : ${idx}")
                                    if (idx < dcdDelayValues.size - 1) {
                                        //optionValue.sprayDelay = dcdDelayValues[idx + 1].toInt()
                                        /*
                                        optionValue = optionValue.copy(
                                            sprayDelay = dcdDelayValues[idx + 1].toInt()
                                        )
                                        */
                                        val sprayDelay = dcdDelayValues[idx + 1].toInt()
                                        onClickSprayDelay(sprayDelay)
                                        Timber.d("onClickSprayDelay Up -> optionValue : ${optionValue}")
                                    }
                                }
                                .size(28.px.dp)
                                .clip(RoundedCornerShape(8.px.dp))
                                .background(
                                    brush = Brush.linearGradient(
                                        colors = listOf(
                                            Color(255, 255, 255),
                                            Color(248, 245, 238),
                                        )
                                    )
                                )
                                .border(
                                    width = 1.4.px.dp,
                                    color = Color(222,173,11),
                                    shape = RoundedCornerShape(8.px.dp)
                                ),
                            horizontalArrangement = Arrangement.Center,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Image(
                                painter = painterResource(id = R.drawable.ic_right_yellow),
                                contentDescription = null,
                                modifier = Modifier.size(16.px.dp),
                                contentScale = ContentScale.Crop
                            )
                        }
                    }
                }
            }

            // Two Button
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(66.px.dp)
                    .padding(end = 10.px.dp),
                horizontalArrangement = Arrangement.End,
                verticalAlignment = Alignment.CenterVertically
            ) {
                // Cancel
                TextButton(
                    modifier = Modifier
                        .size(68.px.dp, 40.px.dp),
                    onClick = {
                        Timber.d("Cancel Clicked")
                        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(55.px.dp, 40.px.dp),
                    onClick = {
                        Timber.d("Ok Clicked")
                        onClick.invoke(true)
                    }
                ) {
                    Text(
                        text = stringResource(R.string.ok),
                        style = RobotoTypography.labelLarge,
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.px.sp,
                        color = Color.Black
                    )
                }
            }
        }
    }
}


@Preview(
    apiLevel = 34,
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=1920dp,height=1080dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)
@Composable
fun PreviewDcdOptionDialog(
    mainViewModel:MainViewModel = MainViewModel(
        preferenceRepository = PreferenceRepository(LocalContext.current),
        serialPortRepository = SerialPortRepository.createWithFakeRepository(),
        databaseRepository = DatabaseRepository(RamanDatabaseService(RamanDatabase.getInstance(LocalContext.current))),
        dispatcherProvider = DefaultDispatcherProvider(),
        applicationContext = LocalContext.current
    )
) {
    DcdOptionDialog(
        //mainViewModel = mainViewModel
    )
}