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.shape.RoundedCornerShape
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.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.pages.compose.CardBox
import com.iscs.bozzys.ui.pages.compose.FormBox
import com.iscs.bozzys.ui.pages.compose.getFormListByJsonList
import com.iscs.bozzys.ui.theme.Text

/**
 * 异常上报Dialog
 *
 * @param show          是否显示异常上报Dialog
 * @param onConfirm     确认上报
 * @param onCancel      取消上报
 */
@Composable
fun ExceptReportDialog(show: Boolean, mac: String, onConfirm: () -> Unit = {}, onCancel: () -> Unit = {}) {
    val ctx = LocalContext.current
    if (!show || ctx !is PageBase) return

    // 表单基准数据
    val json = "[{\"id\":\"device_mac\",\"label\":\"异常设备\",\"value\":[\"$mac\"],\"type\":\"input\",\"placeholder\":[\"请输入异常的设备Mac\"],\"required\":true,\"enabled\":false},{\"id\":\"execpt_type\",\"label\":\"异常类别\",\"value\":[\"0\"],\"type\":\"select\",\"placeholder\":[\"请选择异常类别\"],\"required\":true,\"options\":[{\"label\":\"硬件故障\",\"value\":\"0\"},{\"label\":\"硬件丢失\",\"value\":\"1\"},{\"label\":\"作业取消\",\"value\":\"2\"},{\"label\":\"作业异常\",\"value\":\"3\"},{\"label\":\"其他异常\",\"value\":\"4\"}]},{\"id\":\"report_status\",\"label\":\"上报状态\",\"value\":[\"1\"],\"type\":\"radio\",\"placeholder\":[\"\"],\"required\":true,\"options\":[{\"label\":\"已上报\",\"value\":\"0\"},{\"label\":\"未上报\",\"value\":\"1\"}]},{\"id\":\"device_return\",\"label\":\"设备是否归还\",\"value\":[\"1\"],\"type\":\"radio\",\"placeholder\":[\"\"],\"required\":true,\"options\":[{\"label\":\"已归还\",\"value\":\"0\"},{\"label\":\"未归还\",\"value\":\"1\"}]},{\"id\":\"job_finish\",\"label\":\"作业任务是否完成\",\"value\":[\"1\"],\"type\":\"radio\",\"placeholder\":[\"\"],\"required\":true,\"options\":[{\"label\":\"已完成\",\"value\":\"0\"},{\"label\":\"未完成\",\"value\":\"1\"}]}]"
    val forms = json.getFormListByJsonList(null)

    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.85f)
                    // 防止点击穿透
                    .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(
                            "异常上报",
                            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))
                    )
                    FormBox(
                        forms, {}, modifier = Modifier
                            .padding(horizontal = 16.dp)
                            .padding(bottom = 6.dp)
                    )
                    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
                        )
                    }
                }
            }
        }
    }
}