package com.laseroptek.raman.ui.screens.main

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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
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.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.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.AlertType
import com.laseroptek.raman.ui.common.fullscreen.FullScreen
import com.laseroptek.raman.ui.theme.RobotoTypography
import com.laseroptek.raman.utils.ext.dropShadow
import com.laseroptek.raman.utils.ext.px

@Composable
fun AlertView(
    type: AlertType = AlertType.WARNING,
    title: String = stringResource(R.string.error_title),
    description: String= "The system temperature is very high",
    onClick: () -> Unit = {}
) {
    FullScreen(
        contentAlignment = Alignment.Center,
        backgroundColor = Color.Transparent
    ) {
        Column(
            modifier = Modifier
                .size(500.px.dp, 280.px.dp)
                .clip(RoundedCornerShape(28.px.dp))
                .background(Color(0,0,0).copy(0.75f))
                .dropShadow(
                    shape = RoundedCornerShape(28.px.dp),
                    color = Color(0,0,0).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(0,0,0).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
        ) {
            Column(
                modifier = Modifier
                    .size(470.px.dp, 250.px.dp)
                    .clip(RoundedCornerShape(20.px.dp))
                    .border(2.px.dp, color = Color(120, 123, 130), shape = RoundedCornerShape(28.px.dp)),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                // close button
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(40.px.dp),
                    horizontalArrangement = Arrangement.End,
                    verticalAlignment = Alignment.Bottom
                ) {
                    // Close
                    if (type != AlertType.ERROR) {
                        IconButton(
                            onClick = { onClick.invoke() },
                            modifier = Modifier.size(30.px.dp, 30.px.dp)
                        ) {
                            Image(
                                painter = painterResource(id = R.drawable.ic_close),
                                contentDescription = null,
                                modifier = Modifier.size(24.px.dp),
                                contentScale = ContentScale.Crop,
                                colorFilter = ColorFilter.tint(Color.White)
                            )
                        }
                        Spacer(modifier = Modifier.width(16.px.dp))
                    }
                }

                // logo
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(50.px.dp),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.Bottom
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.ic_warning),
                        contentDescription = null,
                        modifier = Modifier.size(50.px.dp, 40.px.dp),
                        contentScale = ContentScale.Crop,
                        colorFilter = ColorFilter.tint(Color(236,1,1))
                    )
                }

                // title
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(40.px.dp),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = title,
                        style = RobotoTypography.bodyMedium,
                        fontWeight = FontWeight.ExtraLight,
                        fontSize = 30.px.sp,
                        color = Color.White,
                        textAlign = TextAlign.Center,
                        letterSpacing = (3).sp
                        )
                }

                // description
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(80.px.dp),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = description,
                        style = RobotoTypography.bodySmall,
                        fontWeight = FontWeight.ExtraLight,
                        fontSize = 20.px.sp,
                        color = Color.White,
                        textAlign = TextAlign.Center,
                    )
                }

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

@Preview(
    apiLevel = 34,
    uiMode = Configuration.UI_MODE_NIGHT_NO,
    showBackground = false,
    device = "spec:width=1280dp,height=720dp,dpi=150,isRound=false,chinSize=0dp,orientation=landscape"
)
@Composable
fun PreviewAlertView() {
    AlertView(
        type = AlertType.WARNING,
        title = "Warning",
        description = "The system temperature is very high",
    )
}