package com.iscs.bozzys.ui.dialog

import android.view.WindowManager
import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.graphics.SolidColor
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
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.pages.compose.CardBox
import com.iscs.bozzys.ui.theme.Main
import com.iscs.bozzys.ui.theme.Text

/**
 * 公共输入框Dialog
 */
@Composable
fun InputDialog(
    show: Boolean,
    value: String,
    title: String = "",
    placeholder: String = "",
    onConfirm: () -> Unit = {},
    onCancel: () -> Unit = {},
    onInputChanged: (String) -> Unit
) {
    val ctx = LocalContext.current
    if (!show || ctx !is PageBase) return

    LaunchedEffect(Unit) {
        ctx.setNavigationLight(false)
    }

    Dialog(
        onDismissRequest = onCancel,
        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.4f))
                .clickable(
                    indication = null,
                    interactionSource = remember { MutableInteractionSource() },
                    onClick = {}),
            contentAlignment = Alignment.Center
        ) {
            CardBox(
                topRadius = 16.dp,
                bottomRadius = 16.dp,
                modifier = Modifier
                    .fillMaxWidth(0.75f)
                    // 防止点击穿透
                    .clickable(
                        indication = null,
                        interactionSource = remember { MutableInteractionSource() },
                        onClick = {})
                    .background(Color.White, RoundedCornerShape(16.dp))
            ) {
                Column(Modifier.fillMaxWidth()) {
                    Row(
                        Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 16.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text(
                            title,
                            fontSize = 18.sp,
                            lineHeight = 48.sp,
                            fontWeight = FontWeight.Bold,
                            color = Text,
                            modifier = Modifier.weight(1f),
                            textAlign = TextAlign.Center
                        )
                    }
                    Spacer(
                        Modifier
                            .fillMaxWidth()
                            .height(0.5.dp)
                            .background(Color.Black.copy(alpha = 0.05f))
                    )
                    BasicTextField(
                        value,
                        onValueChange = { onInputChanged(it) },
                        Modifier
                            .padding(vertical = 16.dp, horizontal = 10.dp)
                            .fillMaxWidth()
                            .height(46.dp)
                            .border(1.dp, shape = RoundedCornerShape(6.dp), color = Color(0xFFE5E6EB))
                            .padding(horizontal = 10.dp),
                        singleLine = true,
                        textStyle = LocalTextStyle.current.copy(fontSize = 16.sp, lineHeight = 20.sp),
                        decorationBox = { innerTextField ->
                            Box(contentAlignment = Alignment.CenterStart) {
                                innerTextField()
                                if (value.isEmpty()) {
                                    Text(
                                        placeholder,
                                        color = Color(0xFF9CA3AF),
                                        fontSize = 16.sp,
                                        lineHeight = 20.sp,
                                        modifier = Modifier.offset(y = (-1).dp)
                                    )
                                }
                            }
                        },
                        cursorBrush = SolidColor(Main),
                        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text)
                    )
                    Spacer(
                        Modifier
                            .fillMaxWidth()
                            .height(0.5.dp)
                            .background(Color.Black.copy(alpha = 0.05f))
                    )
                    Row(Modifier.padding(horizontal = 5.dp, vertical = 10.dp)) {
                        Text(
                            "取消", 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(
                            "确定",
                            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
                        )
                    }
                }
            }
        }
    }
}