package com.dji.sample.common.util;

import com.dji.sample.wayline.model.dto.WaylinePointDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @description：解析point
 */
@Slf4j
@Component
public class WaylineUtil {

    public static  List<WaylinePointDTO> extractWaypoints(InputStream inputStream) throws IOException {
        // 从 waylinesDoc 中提取航点信息
        List<WaylinePointDTO> waypoints = new ArrayList<>();
        try {
            // 读取KML文件
            // 将 InputStream 读取到字节数组中
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                baos.write(buffer, 0, length);
            }
            byte[] content = baos.toByteArray();

            // 使用 ByteArrayInputStream 重新创建 InputStream
            ByteArrayInputStream bais = new ByteArrayInputStream(content);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(bais);
            // 规范化文档
            doc.getDocumentElement().normalize();
            // 获取根节点
            Element root = doc.getDocumentElement();
            // 获取所有Folder节点
            NodeList folderNodes = doc.getElementsByTagName("Folder");
            if (folderNodes.getLength() > 0) {
                for (int i = 0; i < folderNodes.getLength(); i++) {

                    Element folder = (Element) folderNodes.item(i);
                    // 获取wpml:waylineId
                    String waylineId = getTagValue(folder, "wpml:waylineId");
                    // 获取所有Placemark节点
                    NodeList placemarkNodes = folder.getElementsByTagName("Placemark");
                    for (int j = 0; j < placemarkNodes.getLength(); j++) {
                        WaylinePointDTO waylinePointDTO = new WaylinePointDTO();
                        waylinePointDTO.setFolderId(waylineId);
                        Element placemark = (Element) placemarkNodes.item(j);
                        // 获取coordinates
                        String coordinates = getTagValue(placemark, "coordinates");
                        String[] coo = coordinates.split(",");
                        waylinePointDTO.setLatitude(Double.valueOf(coo[1]));
                        waylinePointDTO.setLongitude(Double.valueOf(coo[0]));
                        // 获取wpml:index
                        String index = getTagValue(placemark, "wpml:index");
                        waylinePointDTO.setPointIndex(index);
                        waypoints.add(waylinePointDTO);
                    }
                }
            }
            return waypoints;
        } catch (Exception e) {
            e.printStackTrace();
            return waypoints;
        }

    }

    private static String getTagValue(Element element, String tagName) {
        NodeList nodeList = element.getElementsByTagName(tagName);
        if (nodeList.getLength() > 0) {
            Node node = nodeList.item(0);
            if (node instanceof Text) {
                return node.getTextContent().trim();
            } else if (node.hasChildNodes()) {
                return node.getFirstChild().getTextContent().trim();
            }
        }
        return "";
    }


}