package com.iscs.bozzys.ui.dialog

import android.view.WindowManager
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
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.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.remember
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.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.iscs.bozzys.ui.common.PageBase
import com.iscs.bozzys.ui.theme.Text

/**
 * 默认公共弹窗
 */
@Composable
fun TipsDialog(
    show: Boolean = false,
    title: String = "提示",
    content: String = "",
    cancelText: String = "取消",
    onCancel: () -> Unit = {},
    confirmText: String = "确认",
    onConfirm: () -> Unit = {}
) {
    val ctx = LocalContext.current
    if (show && ctx is PageBase) {
        Dialog(onDismissRequest = {}, properties = DialogProperties(decorFitsSystemWindows = false)) {
            val activityWindow = ctx.window
            val dialogWindow = ctx.getDialogWindow()
            SideEffect {
                if (activityWindow != null && dialogWindow != null) {
                    val attributes = WindowManager.LayoutParams()
                    // 复制Activity窗口属性
                    attributes.copyFrom(activityWindow.attributes)
                    // 这个一定要设置
                    attributes.type = dialogWindow.attributes.type
                    // 更新窗口属性
                    dialogWindow.attributes = attributes
                    // 设置窗口的宽度和高度，这段代码Dialog源码中就有哦，可以自己去查看
                    dialogWindow.setLayout(
                        activityWindow.decorView.width,
                        activityWindow.decorView.height
                    )
                }
            }
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(Color.Black.copy(alpha = 0.5f))
                    .clickable(
                        indication = null,
                        interactionSource = remember { MutableInteractionSource() }
                    ) {},
                contentAlignment = Alignment.Center
            ) {
                Box(
                    modifier = Modifier
                        .clip(RoundedCornerShape(12.dp))
                        .background(Color.White)
                ) {
                    Column(modifier = Modifier.fillMaxWidth(0.75f), horizontalAlignment = Alignment.CenterHorizontally) {
                        Text(title, fontSize = 18.sp, lineHeight = 48.sp, fontWeight = FontWeight.Bold, color = Text)
                        Spacer(
                            Modifier
                                .fillMaxWidth()
                                .height(1.dp)
                                .background(Color(0xFFEEEEEE))
                        )
                        Text(
                            content,
                            fontSize = 16.sp,
                            color = Text.copy(alpha = 0.8f),
                            modifier = Modifier.padding(horizontal = 16.dp, vertical = 20.dp),
                            textAlign = TextAlign.Center
                        )
                        Spacer(
                            Modifier
                                .fillMaxWidth()
                                .height(0.5.dp)
                                .background(Color.Black.copy(alpha = 0.05f))
                        )
                        Row(Modifier.padding(horizontal = 5.dp, vertical = 10.dp)) {
                            Text(
                                cancelText, color = Color.White, modifier = Modifier
                                    .padding(horizontal = 5.dp)
                                    .weight(1f)
                                    .clip(RoundedCornerShape(6.dp))
                                    .background(Color.Gray.copy(alpha = 0.6f))
                                    .clickable(onClick = { onCancel() })
                                    .padding(vertical = 8.dp),
                                textAlign = TextAlign.Center,
                                fontSize = 16.sp,
                                fontWeight = FontWeight.Bold
                            )
                            Text(
                                confirmText,
                                color = Color.White,
                                modifier = Modifier
                                    .padding(horizontal = 5.dp)
                                    .weight(1f)
                                    .clip(RoundedCornerShape(6.dp))
                                    .background(MaterialTheme.colorScheme.primary)
                                    .clickable(onClick = { onConfirm() })
                                    .padding(vertical = 8.dp),
                                textAlign = TextAlign.Center,
                                fontSize = 16.sp,
                                fontWeight = FontWeight.Bold
                            )
                        }
                    }
                }
            }
        }
    }

}

data class StateTips(val title: String = "", val content: String = "", val show: Boolean = false, val type: String = "")