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

import android.app.Activity
import android.content.res.Configuration
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import androidx.compose.ui.window.DialogWindowProvider
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun FullScreenDialog(
    //showPopup: MutableState<Boolean> = mutableStateOf(false),
    contentAlignment: Alignment = Alignment.Center,
    backgroundColor: Color = Color.Black.copy(alpha = 0.66f),
    onDismissRequest: () -> Unit = {},
    content: @Composable () -> Unit = {},
) {
    val context = LocalContext.current
    (context as? Activity)?.window // Get the Window from the Activity
        ?: throw IllegalStateException("FullscreenImmersiveDialog must be used within an Activity")

    // full screen background
    Box(modifier = Modifier.fillMaxSize().background(backgroundColor)) {
        Dialog(
            onDismissRequest = onDismissRequest,
            properties = DialogProperties(
                dismissOnBackPress = false,
                dismissOnClickOutside = false,
                usePlatformDefaultWidth = false // Make dialog full width
            )
        ) {
            val window = (LocalView.current.parent as? DialogWindowProvider)?.window
            val view = LocalView.current
            val windowInsetsController = window?.let { WindowCompat.getInsetsController(it, view) }
            DisposableEffect(view) {
                windowInsetsController?.hide(WindowInsetsCompat.Type.navigationBars())
                onDispose {
                    windowInsetsController?.hide(WindowInsetsCompat.Type.navigationBars())
                }
            }

            // 팝업 윈도 외부 키 입력 방지를 위해 한번더 전체 화면으로 박스를 만듬.
            Box(modifier = Modifier.fillMaxSize(), contentAlignment = contentAlignment) {
                content()
            }
        }
    }
}


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

