package com.multictrl.common.utils.kmz;

import com.multictrl.modules.business.dto.RouteDTO;
import com.multictrl.modules.business.dto.RouteWaypointDTO;
import com.multictrl.modules.business.dto.WaypointActionDTO;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.List;
import java.util.Map;

/**
 * waylines文件工具
 *
 * @author Sdy
 * @since 1.0.0 2026/4/21
 */
@Slf4j
public class WaylinesUtils {

    /**
     * 创建waylines文件
     */
    public static String createTemplateContent(RouteDTO route) {
        Document document = createDocument(route);

        //生成内容
        return XmlUtils.documentToString(document);
    }

    /**
     * 配置document
     */
    public static Document createDocument(RouteDTO route) {
        //创建document对象
        Document document = DocumentHelper.createDocument();

        //创建根kml节点
        Element kml = document.addElement("kml");
        kml.addNamespace("", "http://www.opengis.net/kml/2.2")
                .addNamespace("wpml", "http://www.dji.com/wpmz/1.0.6");

        //创建Document节点（创建信息）
        Element dc = kml.addElement("Document");

        //创建missionConfig节点（任务信息）
        XmlUtils.addMissionConfigElement(dc, route);

        //创建folder节点（飞行信息）
        Element folder = dc.addElement("Folder");
        folder.addElement("wpml:templateId").setText("0");
        folder.addElement("wpml:executeHeightMode").setText("relativeToStartPoint");
        folder.addElement("wpml:waylineId").setText("0");
//        folder.addElement("wpml:distance").setText("360.651885986328");
//        folder.addElement("wpml:duration").setText("73.1895170211792");
        folder.addElement("wpml:autoFlightSpeed").setText(route.getFlightSpeed() + "");

        //航点信息
        List<RouteWaypointDTO> waypointList = route.getRouteWaypointList();
        for (RouteWaypointDTO waypoint : waypointList) {
            Element placemark = folder.addElement("Placemark");
            Element point = placemark.addElement("Point");
            point.addElement("coordinates").setText(waypoint.getLongitude() + "," + waypoint.getLatitude());
            placemark.addElement("wpml:index").setText(waypoint.getWaypointSort() + "");
            placemark.addElement("wpml:executeHeight").setText(waypoint.getFlightHeight() + "");
            placemark.addElement("wpml:waypointSpeed").setText(waypoint.getFlightSpeed() + "");
            //添加转弯模式/飞机偏航角模式
            XmlUtils.addWaypointTurnMode(placemark, waypoint);

            //添加动作
            Map<Integer, List<WaypointActionDTO>> maps = XmlUtils.getWaypointActionMap(waypointList);
            XmlUtils.addWaypointAction(placemark, waypoint, maps);

            Element waypointGimbalHeadingParam = placemark.addElement("wpml:waypointGimbalHeadingParam");
            waypointGimbalHeadingParam.addElement("wpml:waypointGimbalPitchAngle").setText("0");
            waypointGimbalHeadingParam.addElement("wpml:waypointGimbalYawAngle").setText("0");
        }

        return document;
    }
}
