package com.laseroptek.raman.ui.common.fullscreen

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.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import com.laseroptek.raman.R
import com.laseroptek.raman.ui.theme.PretendardTypography
import com.laseroptek.raman.utils.ext.px
import timber.log.Timber


@Composable
fun FullScreenPopup(
    //showPopup: MutableState<Boolean> = mutableStateOf(false),
    contentAlignment: Alignment = Alignment.Center,
    backgroundColor: Color = Color.Black.copy(alpha = 0.66f),
    onDismissRequest: () -> Unit = {},
    content: @Composable () -> Unit = {},
) {
    //if (showPopup.value) {
        // full screen background
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(backgroundColor)
        ) {

            // 팝업 윈도 외부 키 입력 방지를 위해 한번더 전체 화면으로 박스를 만듬.
            Popup(
                alignment = contentAlignment,
                onDismissRequest = { // This is called when a touch occurs outside the Popup
                    Timber.d("onDismissRequest")
                    onDismissRequest.invoke()
                },
                properties = PopupProperties(
                    focusable = true, // Allows intercepting back button and outside touches
                    dismissOnClickOutside = true // Explicitly enable this
                )
            ) {
                content()
            }
        }
    //}
}

@Composable
fun FSTitleLayout(
    modifier: Modifier = Modifier,
    title: String = stringResource(R.string.serial_number),
) {
    // Title
    Row(modifier = modifier
        .border(width = 2.px.dp, color = Color.Gray),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Start
    ) {
        Column(modifier = Modifier.size(60.px.dp, 90.px.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                painter = painterResource(id = R.drawable.ic_left),
                contentDescription = null,
                modifier = Modifier.size(45.px.dp),
            )
        }

        Column(modifier = Modifier
            .fillMaxSize()
            .weight(1f),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.Start
        ) {
            Text(
                text = title,
                style = PretendardTypography.titleSmall,
                fontWeight = FontWeight.SemiBold,
                fontSize = 30.px.sp,
            )
        }
    }
}


@Composable
fun FSBottomLayout(
    onClick : (Boolean) -> Unit = {}
) {
    // Bottom (Buttons)
    Row(modifier = Modifier
        .fillMaxWidth()
        .height(80.px.dp)
        .border(width = 2.px.dp, color = Color.Gray),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center
    ) {
        TextButton(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f)
                .border(width = 2.px.dp, color = Color.Gray)
                .background(color = Color(235, 235, 235)),
            onClick = {
                Timber.d("Cancel Clicked")
                onClick(false)
            }
        ) {
            Text(
                text = stringResource(R.string.cancel),
                style = PretendardTypography.titleSmall,
                fontWeight = FontWeight.SemiBold,
                fontSize = 30.px.sp,
                color = Color.Gray
            )
        }

        TextButton(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f)
                .border(width = 2.px.dp, color = Color.Gray)
                .background(color = Color(255, 204, 0)),
            onClick = {
                Timber.d("Cancel Clicked")
                onClick(false)
            }
        ) {
            Text(
                text = stringResource(R.string.ok),
                style = PretendardTypography.titleSmall,
                fontWeight = FontWeight.SemiBold,
                fontSize = 30.px.sp,
                color = Color.Gray
            )
        }
    }
}

@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 PreviewFullScreenPopup() {
    remember { mutableStateOf(true) }
    FullScreenPopup {
        Text("FULL SCREEN POPUP")
    }
}

