package com.ai.trackwalker.capability.uav;

import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component
public class SimulatedUavVendorAdapter implements UavVendorAdapter {
    public static final List<String> CAPABILITIES = List.of(
            "DEVICE_TOPOLOGY",
            "MISSION_DISPATCH",
            "MISSION_PAUSE_RESUME",
            "MISSION_CANCEL",
            "TELEMETRY",
            "MEDIA_UPLOAD",
            "LIVE_STREAM"
    );

    @Override
    public String vendorCode() {
        return "SIMULATOR";
    }

    @Override
    public Map<String, Object> checkConnection(Map<String, Object> connection) {
        return Map.of(
                "status", "AVAILABLE",
                "checked_at", Instant.now().toString(),
                "layers", List.of(
                        layer("NETWORK", "PASSED", "进程内模拟链路可达"),
                        layer("AUTHENTICATION", "PASSED", "模拟器无需外部授权"),
                        layer("CAPABILITIES", "PASSED", CAPABILITIES.size() + " 项能力已注册"),
                        layer("BUSINESS_HANDSHAKE", "PASSED", "任务与遥测契约可用")
                ),
                "capabilities", CAPABILITIES
        );
    }

    @Override
    public List<Map<String, Object>> synchronizeDevices(Map<String, Object> connection) {
        return List.of(
                device("SIM-UAV-01", "铁路巡检模拟机", 116.090, 39.095, 92),
                device("SIM-UAV-02", "桥隧巡检模拟机", 116.126, 39.114, 86)
        );
    }

    @Override
    public Map<String, Object> dispatchMission(Map<String, Object> mission, Map<String, Object> route) {
        return Map.of(
                "accepted", true,
                "vendor_mission_id", "SIM-MISSION-" + shortId(),
                "vendor_transaction_id", "SIM-TXN-" + shortId(),
                "status", "DISPATCHED",
                "message", "模拟器已接收航线任务",
                "accepted_at", Instant.now().toString()
        );
    }

    @Override
    public Map<String, Object> executeCommand(Map<String, Object> mission, String command, Map<String, Object> parameters) {
        return Map.of(
                "accepted", true,
                "vendor_transaction_id", "SIM-CMD-" + shortId(),
                "command", command,
                "message", "模拟器命令已执行",
                "completed_at", Instant.now().toString()
        );
    }

    private Map<String, Object> layer(String layer, String status, String message) {
        return Map.of("layer", layer, "status", status, "message", message);
    }

    private Map<String, Object> device(String vendorId, String model, double longitude, double latitude, int battery) {
        return Map.of(
                "vendor_device_id", vendorId,
                "device_type", "AIRCRAFT",
                "model", model,
                "serial_number", vendorId,
                "status", "ONLINE",
                "longitude", longitude,
                "latitude", latitude,
                "capabilities", List.of("RTK", "VISIBLE", "THERMAL", "MISSION_PAUSE_RESUME", "TELEMETRY"),
                "health_detail", Map.of("battery_percent", battery, "rtk", "FIXED", "link_quality", 98)
        );
    }

    private String shortId() {
        return UUID.randomUUID().toString().replace("-", "").substring(0, 10).toUpperCase();
    }
}
