package com.zklh.dronecontroller.ui import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material.icons.filled.* import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.zklh.dronecontroller.core.msdk.DroneSdkState import com.zklh.dronecontroller.core.telemetry.TelemetrySnapshot import com.zklh.dronecontroller.core.telemetry.hasReliableRtkPosition @Composable internal fun PilotPreflightScreen(sdkState: DroneSdkState, telemetry: TelemetrySnapshot, onClose: () -> Unit, onEnterFlight: () -> Unit = onClose) { val connected = sdkState.productConnected val device = rememberPilotDeviceReadings(connected) val instruments = rememberFlightInstrumentReadings(connected, telemetry.cameraComponent) var propellersChecked by remember(sdkState.aircraftSerialNumber, connected) { mutableStateOf(false) } var airspaceChecked by remember(sdkState.aircraftSerialNumber, connected) { mutableStateOf(false) } fun reading(value: String) = if (connected) value else "—" Column(Modifier.fillMaxSize().background(PilotDesign.PageBrush).padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { Row(Modifier.height(36.dp), verticalAlignment = Alignment.CenterVertically) { IconButton(onClick = onClose, modifier = Modifier.size(32.dp)) { Icon(Icons.AutoMirrored.Filled.ArrowBack, "返回", tint = PilotDesign.Ink) } Text("飞前检查", color = PilotDesign.Ink, fontSize = 27.sp, lineHeight = 34.sp, fontWeight = FontWeight.Bold) Text("检查各项状态,确保满足飞行条件", color = PilotDesign.Muted, fontSize = 12.sp, modifier = Modifier.padding(start = 20.dp)) Spacer(Modifier.weight(1f)) Text("风速 ${instrumentNumber(instruments.windSpeed)} m/s ${rememberPilotTime()} ${reading(if (telemetry.batteryPercent >= 0) "${telemetry.batteryPercent}%" else "—")}", color = PilotDesign.Ink, fontSize = 12.sp) } Row(Modifier.weight(1f), horizontalArrangement = Arrangement.spacedBy(14.dp)) { Column(Modifier.weight(1f).fillMaxHeight().verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(8.dp)) { CheckCard("飞行器连接", if (connected) "已连接,实时获取设备状态" else "等待连接飞行器", Icons.Default.Flight, connected, listOf( "链路质量" to (device.downlinkQuality?.let { "$it%" } ?: "—"), "固件版本" to reading(telemetry.firmwareVersion.ifBlank { "—" }), "飞行模式" to reading(telemetry.flightModeString.ifBlank { telemetry.flightMode }), "飞行器状态" to if (connected) "已连接" else "未连接")) CheckCard("电池", "电量、温度与续航状态", Icons.Default.BatteryFull, connected && telemetry.batteryPercent > 30, listOf( "当前电量" to reading(if (telemetry.batteryPercent >= 0) "${telemetry.batteryPercent}%" else "—"), "预计续航" to reading(if (telemetry.remainingFlightTimeKnown) "${telemetry.remainingFlightTime / 60} min" else "—"), "电压" to reading(if (telemetry.batteryVoltageKnown) "${instrumentNumber(telemetry.batteryVoltageMv / 1000.0)} V" else "—"), "电池温度" to reading(if (telemetry.batteryTemperatureKnown) "${instrumentNumber(telemetry.batteryTemperature)} °C" else "—"), "循环次数" to reading(if (telemetry.batteryLoopTimesKnown) "${telemetry.batteryLoopTimes} 次" else "—"))) CheckCard("GPS / RTK", if (connected && telemetry.gpsValid) "实时定位状态" else "等待有效定位", Icons.Default.LocationOn, connected && telemetry.gpsValid, listOf( "GPS 卫星数" to reading(if (telemetry.gpsValid) telemetry.gpsSatelliteCount.toString() else "—"), "RTK 状态" to reading(if (telemetry.hasReliableRtkPosition()) "固定解" else if (telemetry.rtkEnabled) "等待定位" else "未启用"), "水平 / 垂直精度" to if (telemetry.hasReliableRtkPosition()) "${instrumentNumber(device.rtkHorizontalStdMeters?.times(100))} / ${instrumentNumber(device.rtkVerticalStdMeters?.times(100))} cm" else "—")) CheckCard("遥控器", if (sdkState.productLinkConnected) "已连接" else "等待连接遥控器", Icons.Default.SportsEsports, sdkState.productLinkConnected, listOf( "连接状态" to if (sdkState.productLinkConnected) "在线" else "离线", "电量" to if (sdkState.productLinkConnected && telemetry.rcBatteryPercent >= 0) "${telemetry.rcBatteryPercent}%" else "—", "设备型号" to telemetry.remoteControllerType.replace('_', ' ').ifBlank { "—" })) } Column(Modifier.weight(1f).fillMaxHeight(), verticalArrangement = Arrangement.spacedBy(8.dp)) { Column(Modifier.weight(1f).verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(8.dp)) { CheckCard("桨叶", "请现场确认安装、变形与裂纹情况", Icons.Default.Toys, propellersChecked, listOf("桨叶检查" to if (propellersChecked) "人工已确认" else "需人工确认", "安装状态" to if (propellersChecked) "已检查" else "待检查"), onConfirm = { propellersChecked = !propellersChecked }) CheckCard("返航点", if (connected && telemetry.homeLocationValid) "返航点已记录" else "尚未获取返航点", Icons.Default.Home, connected && telemetry.homeLocationValid, listOf( "返航点坐标" to reading(if (telemetry.homeLocationValid) "%.5f, %.5f".format(java.util.Locale.US, telemetry.homeLatitude, telemetry.homeLongitude) else "—"), "返航高度" to (device.goHomeHeight?.let { "$it m" } ?: "—"))) CheckCard("空域", "飞行许可与现场环境需人工确认", Icons.Default.Navigation, airspaceChecked, listOf("空域许可" to if (airspaceChecked) "人工已确认" else "请确认", "限高设置" to reading("${telemetry.heightLimit} m")), onConfirm = { airspaceChecked = !airspaceChecked }) PilotCard(Modifier.fillMaxWidth()) { Row(verticalAlignment = Alignment.CenterVertically) { Icon(Icons.Default.Shield, null, tint = PilotDesign.Blue, modifier = Modifier.size(18.dp)); Spacer(Modifier.width(8.dp)); Text("安全提示", color = PilotDesign.Ink, fontSize = 14.sp, fontWeight = FontWeight.Bold) } Text("• 请确认设备、环境及任务计划满足现场作业要求。\n• 起飞前再次检查周边人员、车辆与障碍物。\n• 如遇异常,及时中止任务并返航。", color = PilotDesign.Muted, fontSize = 11.sp, lineHeight = 17.sp, modifier = Modifier.padding(top = 5.dp)) } } PilotPrimaryButton("进入飞行", onEnterFlight, Modifier.fillMaxWidth(), Icons.Default.Send) } } } } @Composable private fun CheckCard(title: String, subtitle: String, icon: ImageVector, healthy: Boolean, rows: List>, onConfirm: (() -> Unit)? = null) { Surface(Modifier.fillMaxWidth(), color = Color.White.copy(alpha = 0.9f), shape = androidx.compose.foundation.shape.RoundedCornerShape(10.dp)) { Column(Modifier.padding(12.dp)) { Row(verticalAlignment = Alignment.CenterVertically) { Box(Modifier.size(40.dp).background(PilotDesign.Background, CircleShape), contentAlignment = Alignment.Center) { Icon(icon, null, tint = PilotDesign.Ink, modifier = Modifier.size(25.dp)) } Column(Modifier.weight(1f).padding(start = 14.dp), verticalArrangement = Arrangement.spacedBy(3.dp)) { Text(title, color = PilotDesign.Ink, fontSize = 17.sp, lineHeight = 21.sp, fontWeight = FontWeight.Bold) Text(subtitle, color = PilotDesign.Muted, fontSize = 11.sp, lineHeight = 14.sp) } if (onConfirm == null) PilotStatus(if (healthy) "正常" else "待确认", healthy) else TextButton(onClick = onConfirm, contentPadding = PaddingValues(4.dp)) { Icon(if (healthy) Icons.Default.CheckCircle else Icons.Default.Help, null, tint = if (healthy) PilotDesign.Teal else PilotDesign.Muted, modifier = Modifier.size(19.dp)); Text(if (healthy) "已确认" else "需确认", fontSize = 12.sp) } } Spacer(Modifier.height(10.dp)) Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) { rows.forEachIndexed { index, (label, value) -> if (index > 0) VerticalDivider(Modifier.height(32.dp), color = PilotDesign.Line) PilotMetric(label, value, Modifier.weight(1f)) } } } } }