package com.multictrl.common.utils.kmz;

import cn.hutool.core.util.StrUtil;
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;

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

    /**
     * 创建航线template内容
     */
    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");
        dc.addElement("wpml:createTime").setText(System.currentTimeMillis() + "");
        dc.addElement("wpml:updateTime").setText(System.currentTimeMillis() + "");

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

        //创建folder节点（飞行信息）
        Element folder = dc.addElement("Folder");
        String templateType = route.getTemplateType();
        folder.addElement("wpml:templateType").setText(templateType);
        folder.addElement("wpml:templateId").setText("0");
        Element waylineCoordinateSysParam = folder.addElement("wpml:waylineCoordinateSysParam");
        waylineCoordinateSysParam.addElement("wpml:coordinateMode").setText("WGS84");
        waylineCoordinateSysParam.addElement("wpml:heightMode").setText(StrUtil.isBlank(route.getHeightModel()) ? "relativeToStartPoint" : route.getHeightModel());
        waylineCoordinateSysParam.addElement("wpml:positioningType").setText("GPS");
        folder.addElement("wpml:autoFlightSpeed").setText(route.getFlightSpeed() + "");
        folder.addElement("wpml:globalHeight").setText(route.getFlightHeight() + "");
        folder.addElement("wpml:caliFlightEnable").setText("0");
        folder.addElement("wpml:gimbalPitchMode").setText("manual");
        folder.addElement("wpml:globalWaypointTurnMode").setText("toPointAndStopWithDiscontinuityCurvature");
        folder.addElement("wpml:globalUseStraightLine").setText("1");

        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:ellipsoidHeight").setText(waypoint.getFlightHeight() + "");
            placemark.addElement("wpml:height").setText(waypoint.getFlightHeight() + "");
            placemark.addElement("wpml:waypointSpeed").setText(waypoint.getFlightSpeed() + "");
            placemark.addElement("wpml:useGlobalSpeed").setText(waypoint.getFollowRouteSpeed() == true ? "1" : "0");
            placemark.addElement("wpml:useGlobalHeight").setText(waypoint.getFollowRouteHeight() == true ? "1" : "0");
            placemark.addElement("wpml:useGlobalHeadingParam").setText("0");
            placemark.addElement("wpml:useGlobalTurnParam").setText("0");
            //添加转弯模式/飞机偏航角模式
            XmlUtils.addWaypointTurnMode(placemark, waypoint);

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

        Element payloadParam = folder.addElement("wpml:payloadParam");
        payloadParam.addElement("wpml:payloadPositionIndex").setText("0");
        payloadParam.addElement("wpml:imageFormat").setText("wide,zoom,ir,visable");

        return document;
    }
}
