package com.tofly.ghrq_route.module.sqlite;

import android.util.Log;

import com.tofly.ghrq_route.module.dasData.domain.DasData;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

public class KmlUtil {

    public static void parseKml(String pathName, List<DasData> dataList) {
        File file = new File(pathName);//pathName为KML文件的路径
        try {
            InputStream inputStream = null;
            String fileName = file.getName();
            if (fileName.endsWith("kml") || fileName.endsWith("kmz")) {
                inputStream = new FileInputStream(file);
                parseXmlWithDom4j(inputStream, dataList);
            } else if (fileName.endsWith("png")) {
                    /*ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
                    byte[] b = new byte[512];
                    int readedByteSize = 0;
                    while ((readedByteSize = zipInputStream.read(b)) != -1) {
                        byteArrayOut.write(b, 0, readedByteSize);
                    }
                    byteArrayOut.flush();
                    byteArrayOut.close();
                    InputStream isBitmap = new ByteArrayInputStream(byteArrayOut.toByteArray());
                    Bitmap bitmap = BitmapFactory.decodeStream(isBitmap);
                    isBitmap.close();*/
            }
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static void parseXmlWithDom4j(InputStream input, List<DasData> dataList) {
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(input);
        } catch (DocumentException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        Element root = document.getRootElement();//获取doc.kml文件的根结点
        listNodes(root, dataList);
    }

    //遍历当前节点下的全部节点
    private static void listNodes(Element node, List<DasData> dataList){
        Log.d("当前结点的名称:", node.getName());
        //假设当前节点内容不为空，则输出
        if("Placemark".equals(node.getName())){
            //Log.d("当前结点内容：", node.getText());
            DasData dasData = parsePlaceMark(node);
            if (dasData != null) {
                dataList.add(dasData);
            }
        }

        //同一时候迭代当前节点以下的全部子节点
        //使用递归
        Iterator<Element> iterator = node.elementIterator();
        while(iterator.hasNext()){
            Element e = iterator.next();
            listNodes(e, dataList);
        }
    }

    private static DasData parsePlaceMark(Element node) {
        List<Node> content = node.content();
        String name = null;
        String coordinates = null;
        for (Node node1 : content) {
            if (node1.getName().equals("name")) {
                name = node1.getStringValue();
            }
            if (node1.getName().equals("Point")) {
                coordinates = node1.getStringValue();
            }
        }

        DasData dasData = null;
        if (coordinates != null && !coordinates.isEmpty()) {
            dasData = DasData.parseDasData(name, coordinates);
        }
        return dasData;
    }
}
