package com.laseroptek.raman.ui.screens.info

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.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
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.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.common.fullscreen.FullScreenPopup
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.px

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SoftwareInformationDialog(
    title: String = stringResource(R.string.software_information_title),
    version: Version = Version(),
    onClick: () -> Unit= {}
) {
    FullScreenPopup {
        Column(
            modifier = Modifier
                .size(330.px.dp, 304.px.dp)
                .clip(RoundedCornerShape(28.px.dp))
                .background(Color.White)
                .dropShadow(
                    shape = RoundedCornerShape(28.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(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
                ),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // Software version
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(72.px.dp)
                    .padding(start = 20.px.dp, end = 20.px.dp),
                horizontalArrangement = Arrangement.Start,
                verticalAlignment = Alignment.CenterVertically
            ){
                Text(
                    text = title,
                    style = RobotoTypography.bodySmall.copy(
                        fontWeight = FontWeight.Thin,
                    ),
                    fontWeight = FontWeight.Thin,
                    fontSize = 22.px.sp,
                    color = Color.Black,
                    textAlign = TextAlign.Center,
                )
            }

            Column(
                modifier = Modifier
                    .size(282.px.dp, 144.px.dp)
                    .clip(RoundedCornerShape(12.px.dp))
                    .background(Color(250, 250, 250))
                ,
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {

                // Product
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(48.px.dp)
                        .padding(start = 15.px.dp, end = 15.px.dp),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = "Product",
                        fontWeight = FontWeight.Thin,
                        style = RobotoTypography.bodySmall.copy(
                            letterSpacing = 0.25.px.sp
                        ),
                        fontSize = 14.px.sp,
                        color = Color.Black.copy(alpha = 0.6f),
                        textAlign = TextAlign.Center,
                    )
                    Spacer(Modifier.weight(1f))
                    Text(
                        text = "Code: %s, Name: %s".format(version.verCode.formatWithDots(), version.verName),
                        fontWeight = FontWeight.Thin,
                        style = RobotoTypography.bodySmall.copy(
                            letterSpacing = 0.25.px.sp
                        ),
                        fontSize = 12.px.sp,
                        color = Color.Black.copy(alpha = 0.8f),
                        textAlign = TextAlign.Center,
                    )
                }

                HorizontalDivider(
                    modifier = Modifier.padding(start = 16.px.dp, end=16.px.dp),
                    thickness = 0.1.px.dp,
                    color = Color.Gray
                )

                // Laser hand
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(48.px.dp)
                        .padding(start = 15.px.dp, end = 15.px.dp),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = "Laser Hand",
                        fontWeight = FontWeight.Thin,
                        style = RobotoTypography.bodySmall.copy(
                            letterSpacing = 0.25.px.sp
                        ),
                        fontSize = 14.px.sp,
                        color = Color.Black.copy(alpha = 0.6f),
                        textAlign = TextAlign.Center,
                    )
                    Spacer(Modifier.weight(1f))
                    Text(
                        text = "H/W: %s, S/W: %s".format(version.lhHwRev.formatWithDots(), version.lhHwRev.formatWithDots()),
                        fontWeight = FontWeight.Thin,
                        style = RobotoTypography.bodySmall.copy(
                            letterSpacing = 0.25.px.sp
                        ),
                        fontSize = 12.px.sp,
                        color = Color.Black.copy(alpha = 0.8f),
                        textAlign = TextAlign.Center,
                    )
                }

                HorizontalDivider(
                    modifier = Modifier.padding(start = 16.px.dp, end=16.px.dp),
                    thickness = 0.1.px.dp,
                    color = Color.Gray
                )

                // Power Supply
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(48.px.dp)
                        .padding(start = 15.px.dp, end = 15.px.dp),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = "Power Supply",
                        fontWeight = FontWeight.Thin,
                        style = RobotoTypography.bodySmall.copy(
                            letterSpacing = 0.25.px.sp
                        ),
                        fontSize = 14.px.sp,
                        color = Color.Black.copy(alpha = 0.6f),
                        textAlign = TextAlign.Center,
                    )
                    Spacer(Modifier.weight(1f))
                    Text(
                        text = "H/W: %s, S/W: %s".format(version.psHwVer.formatWithDots(), version.psSwVer.formatWithDots()),
                        fontWeight = FontWeight.Thin,
                        style = RobotoTypography.bodySmall.copy(
                            letterSpacing = 0.25.px.sp
                        ),
                        fontSize = 12.px.sp,
                        color = Color.Black.copy(alpha = 0.8f),
                        textAlign = TextAlign.Center,
                    )
                }
            }

            // close button
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(88.px.dp)
                    .padding(start = 24.px.dp, end = 12.px.dp),
                horizontalArrangement = Arrangement.End,
                verticalAlignment = Alignment.CenterVertically
            ) {
                TextButton(
                    modifier = Modifier
                        .size(92.px.dp, 88.px.dp),
                    onClick = onClick
                ) {
                    Text(
                        text = stringResource(R.string.close),
                        style = RobotoTypography.labelLarge,
                        fontWeight = FontWeight.Normal,
                        fontSize = 14.px.sp,
                        color = Color.Black,
                        textAlign = TextAlign.End
                    )
                }
            }
        }
    }
}


@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 PreviewSoftwareInformationDialog() {
    SoftwareInformationDialog()
}