import ldMap  from "./init";
import Polyline from "@arcgis/core/geometry/Polyline.js";

import * as geometryEngine from "@arcgis/core/geometry/geometryEngine.js";


function exporGis(){


  let alllayer =ldMap.sketchLayer.polygonLayer.graphics.items
  let textLayer =  ldMap.textLayer.graphics.items
  ;let  geojson = {
    type: "FeatureCollection",
    features: [],
    textLayer: []
  };


  alllayer.forEach(function(graphic) {
    let geometry = graphic.geometry;
    let attributes = graphic.attributes;

    var geojsonGeometry;

    switch(geometry.type) {
      case "polygon":
       let rings = []
       geometry.rings.forEach(item => {
          let ringsVal = []
          item.forEach(val=>{
            ringsVal.push( getCenterDistance(val))
          })
          rings.push(ringsVal)
        })


        geojsonGeometry = {
          type: "Polygon",
          coordinates:rings
        };
        break;

      //其他几何类型
    }
    geojson.features.push({
      type: "Feature",
      geometry: geojsonGeometry,
      type:attributes.type
    });

  });
  textLayer.forEach(item=>{
    let arr={
      coordinates: getCenterDistance([item.geometry.x,item.geometry.y]),
      text: item.symbol.text
    }
    geojson.textLayer.push(arr)
  })


console.log(JSON.stringify(geojson))

}
function getCenterDistance(p2){

  let p1 = []
  let px,py,polyline;

  let layer = ldMap.map.layers.items
  layer.forEach(item=>{
    if(item.url){
      p1=[item.fullExtent.center.x,item.fullExtent.center.y]
    }
  })

  polyline = new Polyline({
    paths: [p1, [p1[0], p2[1]]],
  })
  py = geometryEngine.geodesicLength(polyline, "meters");
  if(p1[1]>p2[1]) py = -py

  polyline = new Polyline({
    paths: [p1, [p2[0], p1[1]]],
  })

  px = geometryEngine.geodesicLength(polyline, "meters");
  if(p1[0]>p2[0]) px = -px
  console.log([ px, py ])
  return [ px, py ]
}
export default exporGis
