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

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.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.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
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.graphics.SolidColor
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.data.model.serial.Version
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.dropShadow
import com.laseroptek.raman.utils.ext.formatWithDots
import com.laseroptek.raman.utils.ext.noRippleClickable
import com.laseroptek.raman.utils.ext.px
import com.laseroptek.raman.utils.ext.toFullStringRepresentation
import timber.log.Timber

@Composable
fun VersionView(
    title: String = stringResource(id = R.string.program_version),
    version: Version = Version(),
    productSerialList: List<String> = listOf(),
    laserHandSerialList: List<String> = listOf(),
    powerSupplySerialList: List<String> = listOf(),
    onClick: (Int) -> Unit = {},
) {
    var index by remember { mutableStateOf(0) }

    val selectedBackgroundBrush = Brush.verticalGradient(
        colors = listOf(
            Color(47, 51, 57),
            Color(83, 88, 97),
        )
    )

    val selectedBorderBrush = Brush.verticalGradient(
        colors = listOf(
            Color(92, 99, 107),
            Color(34, 34, 1),
        )
    )

    Timber.d("version: ${version}")

    Column(modifier = Modifier
        .noRippleClickable(onClick = { onClick.invoke( index) })
        .size(388.px.dp, 154.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.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // Title
        Column(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                Row(
                    modifier = Modifier
                        .size(296.px.dp, 38.px.dp)
                        .border(
                            width = 0.5.px.dp,
                            color = Color(209,209,209),
                            shape = RoundedCornerShape(8.px.dp)
                        )
                ) {
                    // Serial Number
                    Column(
                        modifier = Modifier
                            .noRippleClickable(onClick = { index = 0 })
                            .size(145.px.dp, 32.px.dp)
                            .background(
                                brush = if (index == 0) selectedBackgroundBrush else SolidColor(Color.Transparent),
                                shape = RoundedCornerShape(8.px.dp)
                            )
                            .border(
                                width = 0.5.px.dp,
                                brush = if (index == 0) selectedBorderBrush else SolidColor(Color.Transparent),
                                shape = RoundedCornerShape(8.px.dp)
                            )
                            .dropShadow(
                                shape = RoundedCornerShape(8.px.dp),
                                color = if (index == 0) Color(243, 205, 81).copy(alpha = 0.2f) else Color.Transparent,
                                blur = 3.5.px.dp,
                                offsetY = 0.px.dp,
                                offsetX = 0.px.dp,
                                spread = 0.px.dp
                            )
                            .dropShadow(
                                shape = RoundedCornerShape(8.px.dp),
                                color = if (index == 0) Color(0, 0, 0).copy(alpha = 0.1f) else Color.Transparent,
                                blur = 1.75.px.dp,
                                offsetY = 1.75.px.dp,
                                offsetX = 0.px.dp,
                                spread = 0.px.dp
                            ),
                        horizontalAlignment = Alignment.CenterHorizontally,
                        verticalArrangement = Arrangement.Center
                    ) {
                        Text(
                            text = "Serial Number",
                            style = RobotoTypography.bodySmall.copy(
                                fontSize = 16.px.sp,
                                fontWeight = FontWeight.ExtraLight
                            ),
                            textAlign = TextAlign.Center,
                            color = if (index == 0) Color.White else Color.Black,
                        )
                    }

                    Spacer(modifier = Modifier.width(6.px.dp))

                    // Program Version
                    Column(
                        modifier = Modifier
                            .noRippleClickable(onClick = { index = 1 })
                            .size(145.px.dp, 32.px.dp)
                            .background(
                                brush = if (index == 1) selectedBackgroundBrush else SolidColor(Color.Transparent),
                                shape = RoundedCornerShape(8.px.dp)
                            )
                            .border(
                                width = 0.5.px.dp,
                                brush = if (index == 1) selectedBorderBrush else SolidColor(Color.Transparent),
                                shape = RoundedCornerShape(8.px.dp)
                            )
                            .dropShadow(
                                shape = RoundedCornerShape(8.px.dp),
                                color = if (index == 1) Color(243, 205, 81).copy(alpha = 0.2f) else Color.Transparent,
                                blur = 3.5.px.dp,
                                offsetY = 0.px.dp,
                                offsetX = 0.px.dp,
                                spread = 0.px.dp
                            )
                            .dropShadow(
                                shape = RoundedCornerShape(8.px.dp),
                                color = if (index == 1) Color(0, 0, 0).copy(alpha = 0.1f) else Color.Transparent,
                                blur = 1.75.px.dp,
                                offsetY = 1.75.px.dp,
                                offsetX = 0.px.dp,
                                spread = 0.px.dp
                            ),
                        horizontalAlignment = Alignment.CenterHorizontally,
                        verticalArrangement = Arrangement.Center
                    ) {
                        Text(
                            text = "Program Version",
                            style = RobotoTypography.bodySmall.copy(
                                fontSize = 16.px.sp,
                                fontWeight = FontWeight.ExtraLight
                            ),
                            textAlign = TextAlign.Center,
                            color = if (index == 1) Color.White else Color.Black,
                        )
                    }
                }

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

                Image(
                    painter = painterResource(id = R.drawable.ic_edit),
                    contentDescription = null,
                    modifier = Modifier.size(18.px.dp, 18.px.dp),
                    colorFilter = Color.Black.let { ColorFilter.tint(it) }
                )
            }
        }

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

        // Product
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(30.px.dp),
        ) {
            Column(
                modifier = Modifier
                    .size(100.px.dp, 30.px.dp),
                verticalArrangement = Arrangement.Center,
            ) {
                Text(
                    text = "Product",
                    style = RobotoTypography.labelMedium,
                    fontWeight = FontWeight.Normal,
                    fontSize = 12.px.sp,
                    color = Color.Black
                )
            }

            Column(
                modifier = Modifier
                    .size(248.px.dp, 30.px.dp),
                verticalArrangement = Arrangement.Center,
            ) {
                Text(
                    text =
                        if (index == 0) productSerialList.toFullStringRepresentation()
                        else "Code: %s, Name: %s".format(version.verCode.formatWithDots(), version.verName),
                    style = RobotoTypography.bodySmall,
                    fontWeight = FontWeight.ExtraLight,
                    fontSize = 14.px.sp,
                    color = Color.Black
                )
            }
        }
        HorizontalDivider(
            modifier = Modifier
                .padding(start = 10.px.dp, end=10.px.dp),
            thickness = 0.2.px.dp,
            color = Color.LightGray
        )

        // Laser Hand
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(30.px.dp),
        ) {
            Column(
                modifier = Modifier
                    .size(100.px.dp, 30.px.dp),
                verticalArrangement = Arrangement.Center,
            ) {
                Text(
                    text = "Laser Hand",
                    style = RobotoTypography.labelMedium,
                    fontWeight = FontWeight.Normal,
                    fontSize = 12.px.sp,
                    color = Color.Black
                )
            }

            Column(
                modifier = Modifier
                    .size(248.px.dp, 30.px.dp),
                verticalArrangement = Arrangement.Center,
            ) {
                Text(
                    text =
                        if (index == 0) laserHandSerialList.toFullStringRepresentation()
                        else "H/W: %s, S/W: %s".format(version.lhHwRev.formatWithDots(), version.lhSwVer.formatWithDots()),
                    style = RobotoTypography.bodySmall,
                    fontWeight = FontWeight.ExtraLight,
                    fontSize = 14.px.sp,
                    color = Color.Black
                )
            }
        }
        HorizontalDivider(
            modifier = Modifier
                .padding(start = 10.px.dp, end=10.px.dp),
            thickness = 0.2.px.dp,
            color = Color.LightGray
        )

        // Power Supply
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(30.px.dp),
        ) {
            Column(
                modifier = Modifier
                    .size(100.px.dp, 30.px.dp),
                verticalArrangement = Arrangement.Center,
            ) {
                Text(
                    text = "Power Supply",
                    style = RobotoTypography.labelMedium,
                    fontWeight = FontWeight.Normal,
                    fontSize = 12.px.sp,
                    color = Color.Black
                )
            }

            Column(
                modifier = Modifier
                    .size(248.px.dp, 30.px.dp),
                verticalArrangement = Arrangement.Center,
            ) {
                Text(
                    text =
                        if (index == 0) powerSupplySerialList.toFullStringRepresentation()
                        else "H/W: %s, S/W: %s".format(version.psHwVer, version.psSwVer),
                    style = RobotoTypography.bodySmall,
                    fontWeight = FontWeight.ExtraLight,
                    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 PreviewProgramVersion() {
    VersionView()
}