package com.uavps.framework.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uavps.common.utils.uuid.IdUtils;
import com.uavps.framework.udp.TaskInfo;
import com.uavps.framework.udp.utils.UdpDataUtils;
import com.uavps.system.udp.vo.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author MBQ
 * @Date: 2025/1/15
 * @Description:
 * @Version: 1.0
 **/
public class UdpUtils {
    public UdpInitParameter initParameter() {
        UdpInitParameter parameter = new UdpInitParameter();
        parameter.setBizId(IdUtils.simpleUUID());
        parameter.setRefreshRate("20");

        UnmannedAerialVehicle unmannedAerialVehicle = new UnmannedAerialVehicle();
        unmannedAerialVehicle.setInitialPositionX(135);
        unmannedAerialVehicle.setInitialPositionY(175);
        unmannedAerialVehicle.setInitialPositionZ(100);
        unmannedAerialVehicle.setInitialSpeed(20.33f);
        unmannedAerialVehicle.setInitialPoseX(2.33f);
        unmannedAerialVehicle.setInitialPoseY(2.33f);
        unmannedAerialVehicle.setInitialPoseZ(2.33f);
        parameter.setUnmannedAerialVehicle(unmannedAerialVehicle);

        TargetAircraft targetAircraft = new TargetAircraft();
        targetAircraft.setAircraftType("固定翼无人机");
        targetAircraft.setFormationAircraftCount(50);
        targetAircraft.setLeadAircraftSpeed(11.20f);
        targetAircraft.setInitialPositionX(100.00f);
        targetAircraft.setInitialPositionY(100.00f);
        targetAircraft.setInitialPositionZ(100.00f);
        targetAircraft.setFlightPathType("直线");
        targetAircraft.setPositionNoise(18.20f);
        targetAircraft.setFormationShape("V");
        targetAircraft.setFollowAircraftDistance(20.00f);

        List<TargetAircraft> multiTargetFormation = new ArrayList<>();
        multiTargetFormation.add(targetAircraft);

        parameter.setMultiTargetFormation(multiTargetFormation);
        return parameter;
    }

    public static void main(String[] args) {
        System.out.printf(IdUtils.simpleUUID());
    }

    public static String getJson(byte[] remoteData) throws JsonProcessingException {
//        uavData = new String(data, 0, packet.getLength());
        //byte[] remoteData = packet.getData();
        String json = "";
        if (remoteData != null && remoteData.length > 3) {
            // 封装成对象
            UdpData udpData = UdpDataUtils.parseFrame(remoteData);
            if (udpData != null) {
                PlatformAircraft platformAircraft = udpData.getPlatformAircraft();
                // 第一次数据是平台无人机的数据，基于平台无人机数据进行初始化xy坐标为中心坐标
                if (TaskInfo.INSTANCE.getInitFlag() && UdpData.PLANT_UAV.equals(udpData.getType()) && null != platformAircraft) {
                    // 初始化基坐标
                    TaskInfo.INSTANCE.setX( (platformAircraft.getLongitude() - TaskInfo.INSTANCE.getConversionCenterPixel()));
                    TaskInfo.INSTANCE.setY( (platformAircraft.getLatitude() - TaskInfo.INSTANCE.getConversionCenterPixel()));
                    TaskInfo.INSTANCE.setInitFlag(false);
                    getNewXYAircraftCoordinate(platformAircraft);
                } else if (!TaskInfo.INSTANCE.getInitFlag() && UdpData.PLANT_UAV.equals(udpData.getType()) && null != platformAircraft) {
                    // 基于初始化基坐标计算平台无人机坐标
                    getNewXYAircraftCoordinate(platformAircraft);
                } else if (!TaskInfo.INSTANCE.getInitFlag() && !UdpData.PLANT_UAV.equals(udpData.getType()) && udpData.getTargetAircraft() != null) {
                    // 基于初始化基坐标计算目标飞机坐标
                    if (udpData.getTargetAircraft() != null) {
                        for (AircraftFormation aircraftFormation : udpData.getTargetAircraft()) {
                            for (Aircraft aircraft : aircraftFormation.getAircrafts()) {
                                getNewXYAircraftCoordinate(aircraft);
                            }
                        }
                    }
                } else {
                    //其他情况轮空
                    return null;
                }
                ObjectMapper mapper = new ObjectMapper();
                json = mapper.writeValueAsString(udpData);
            }
        }
        return json;
    }

    private static void getNewXYAircraftCoordinate(PlatformAircraft platformAircraft) {
        platformAircraft.setCoordinateX(((platformAircraft.getLongitude() - TaskInfo.INSTANCE.getX())/ TaskInfo.INSTANCE.getConversionRate()));
        platformAircraft.setCoordinateY(TaskInfo.INSTANCE.getConversionCenterPixel()-((platformAircraft.getLatitude() - TaskInfo.INSTANCE.getY())/ TaskInfo.INSTANCE.getConversionRate()));
    }

    private static void getNewXYAircraftCoordinate(Aircraft aircraft) {
        aircraft.setCoordinateX((aircraft.getCoordinateX() - TaskInfo.INSTANCE.getX())/ TaskInfo.INSTANCE.getConversionRate());
        aircraft.setCoordinateY(TaskInfo.INSTANCE.getConversionCenterPixel()-(aircraft.getCoordinateY() - TaskInfo.INSTANCE.getY())/ TaskInfo.INSTANCE.getConversionRate());
    }
}
