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

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.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.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.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.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.layout.ContentScale
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.unit.dp
import androidx.compose.ui.unit.sp
import com.laseroptek.raman.R
import com.laseroptek.raman.data.model.serial.LaserStatus
import com.laseroptek.raman.data.model.serial.getLaserStatusString
import com.laseroptek.raman.ui.common.fullscreen.FullScreenPopup
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.px
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow


@Composable
fun EMStandByPopup(
    title: String = stringResource(R.string.stand_by_title),
    description: String= "",
    laserStatus: StateFlow<LaserStatus> = MutableStateFlow(LaserStatus()),
    laserCount: Int = 0,
    onClick: () -> Unit = {}
) {
    FullScreenPopup(
        //showPopup = showEmStandByPopup,
        backgroundColor = Color(120,120,120),
        onDismissRequest = {
            onClick()
        }
    ) {
        val laserStatus by laserStatus.collectAsState()
        //val preset by preset.collectAsState()

        Column(
            modifier = Modifier
                .size(458.px.dp, 488.px.dp)
                .clip(RoundedCornerShape(32.px.dp))
                .background(Color.White),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Top
        ) {
            // EMStandBy (Title)
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(96.px.dp)
                    .clip(RoundedCornerShape(topStart = 32.px.dp, topEnd = 32.px.dp))
                    .background(
                        brush = Brush.linearGradient(
                            colors = listOf(
                                Color(34, 197, 94),
                                Color(22, 163, 74)
                            ),
                            //start = Offset(0f, 0f), // Optional: Start at the top-left
                            //end = Offset(200f, 200f) // Optional: End at the bottom-right
                        )
                    ),
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.ic_round_check),
                        contentDescription = null,
                        modifier = Modifier
                            .size(28.px.dp),
                        contentScale = ContentScale.Crop,
                        colorFilter = ColorFilter.tint(Color.White)
                    )

                    Spacer(Modifier.height(3.px.dp))

                    Text(
                        text = getLaserStatusString(laserStatus.laserStatus),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Light,
                        fontSize = 20.px.sp,
                        color = Color.White,
                        textAlign = TextAlign.Center,
                    )
                }

            }

            // PULSE | FLUENCE | REPETION
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(120.px.dp)
                    .background(Color.White)
                    .padding(
                        start = 20.px.dp, end = 20.px.dp, top = 30.px.dp, bottom = 30.px.dp
                    ),
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ) {
                // PULSE -> (OnTime : xx.x)
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .weight(1f),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    Text(
                        text = stringResource(R.string.pulse_duration),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.px.sp,
                        color = Color.Black.copy(alpha = 0.6f),
                        textAlign = TextAlign.Center,
                    )

                    Text(
                        text = "%.1fms".format(laserStatus.onTime),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 20.px.sp,
                        color = Color.Black,
                        textAlign = TextAlign.Center,
                    )
                }

                // Divider
                Column {
                    VerticalDivider(color = Color.Gray, thickness = 1.dp)
                }

                // FLUENCE -> (voltage: xxx)
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .weight(1f),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    Text(
                        text = stringResource(R.string.fluence),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.px.sp,
                        color = Color.Black.copy(alpha = 0.6f),
                        textAlign = TextAlign.Center,
                    )
                    Text(
                        text = "%.1fJ/cm²".format(laserStatus.frequence),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 20.px.sp,
                        color = Color.Black,
                        textAlign = TextAlign.Center,
                    )
                }

                // Divider
                Column {
                    VerticalDivider(color = Color.Gray, thickness = 1.dp)
                }

                // REPETITION -> (frequence: x.x)
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .weight(1f),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    Text(
                        text = stringResource(R.string.repetition),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.px.sp,
                        color = Color.Black.copy(alpha = 0.6f),
                        textAlign = TextAlign.Center,
                    )
                    Text(
                        text = "%.1fHz".format(laserStatus.frequence),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Normal,
                        fontSize = 20.px.sp,
                        color = Color.Black,
                        textAlign = TextAlign.Center,
                    )
                }
            }

            // laser count & foot switch border
            Column(
                modifier = Modifier
                    .size(410.px.dp, 136.px.dp)
                    .border(
                        width = 1.px.dp,
                        color = Color(209, 209, 209),
                        RoundedCornerShape(12.px.dp)
                    ),
            ) {
                // Laser Count
                Column (
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(100.px.dp)
                        .background(
                            brush = Brush.verticalGradient(
                                colors = listOf(
                                    Color(255, 255, 255),
                                    Color(238, 238, 238)
                                ),
                                //start = Offset(0f, 0f), // Optional: Start at the top-left
                                //end = Offset(200f, 200f) // Optional: End at the bottom-right
                            ),
                            RoundedCornerShape(
                                topStart = 12.px.dp,
                                topEnd = 12.px.dp
                            )
                        )
                        .clip(
                            RoundedCornerShape(
                                topStart = 12.px.dp,
                                topEnd = 12.px.dp
                            )
                        ),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ){
                    Text(
                        text = stringResource(R.string.laser_count_title),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.ExtraLight,
                        fontSize = 14.px.sp,
                        color = Color.Black.copy(alpha = 0.6f),
                        textAlign = TextAlign.Center,
                    )

                    Text(
                        text = "%06d".format(laserCount),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Light,
                        fontSize = 36.px.sp,
                        color = Color.Black,
                        textAlign = TextAlign.Center,
                    )
                }

                // foot switch text
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(36.px.dp)
                        .background(Color.White)
                        .clip(
                            RoundedCornerShape(
                                bottomStart = 12.px.dp,
                                bottomEnd = 12.px.dp
                            )
                        ),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center
                ) {
                    Text(
                        text = stringResource(R.string.foot_switch_count_title),
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.Light,
                        fontSize = 12.px.sp,
                        color = Color.Black,
                        textAlign = TextAlign.Center,
                    )
                }
            }

            // alert logo and message
            Row(modifier = Modifier
                .fillMaxWidth()
                .height(70.px.dp),
                horizontalArrangement = Arrangement.Center,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_alert),
                    contentDescription = null,
                    modifier = Modifier.size(18.px.dp),
                )
                Spacer(modifier = Modifier.width(10.px.dp))
                Text(
                    text = description,
                    style = RobotoTypography.bodyMedium,
                    fontWeight = FontWeight.Light,
                    fontSize = 12.px.sp,
                    color = Color.Red,
                    textAlign = TextAlign.Center
                )
            }

            // back to EMStandBy | Ready
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(40.px.dp)
                    .padding(end = 20.px.dp),
                horizontalArrangement = Arrangement.End,
                verticalAlignment = Alignment.CenterVertically
            ) {
                TextButton(
                    modifier = Modifier
                        .size(153.px.dp, 40.px.dp)
                        .clip(RoundedCornerShape(24.px.dp))
                        .background(color = Color(35, 35, 35)),
                    onClick = {
                        onClick()
                    }
                ) {
                    Text(
                        text = stringResource(R.string.back_to_stand_by),
                        style = RobotoTypography.labelLarge,
                        fontWeight = FontWeight.Medium,
                        fontSize = 14.px.sp,
                        color = Color(243,205,81)
                    )
                }
            }
        }
    }
}
