import {esri} from './arcMap'
export const arcline = {};

const DrawDregType =null;
const ldMapActionType ='';
const DrawstartPoint =[];

arcline.esriOn = function (){
  esri.view.on("drag", function(event){
    if(event.button==2&&DrawDregType&&ldMapActionType == 'regectionAction'){
     console.log(event)
     let point = esri.view.toMap({x: event.x, y: event.y});
     if(event.action=='start'){
       // self.DrawLine()
       DrawstartPoint = [point.x,point.y];
     }
     if(event.action == 'end'){
      arcline.Bindpolygon()
       self.view.graphics.removeAll();
      }
      if(event.action == 'update'){
       // self.destroyDraw()
       self.DrawenedPoint = [point.x,point.y];
       let data = {
         vertices:[self.DrawstartPoint, self.DrawenedPoint]
       }
       self.createPolygon(data)
      }

    }


  });
}
  //处理合并的经纬度
  arcline.Bindpolygon=function(){
    let self = this;
    let polygonLayer = self.sketchLayer.polygonLayer
    let graphic = self.view.graphics.items[0]


    if(!graphic||!self.sketchLayer.selectGraphic){return}
    let polygon1 = new Polygon({
      rings: graphic.geometry.rings
    })
    let item = self.sketchLayer.selectGraphic
    let rings = item.geometry.rings
    let newRings = []
    let deletFlag = false;
    rings.forEach(ring =>{
        let newRing = []
        ring.forEach(p=>{
           let point =   new Point({ x: p[0], y: p[1] })
           const pointFlag  = geometryEngine.intersect(polygon1, point);
           if(!pointFlag){
            newRing.push(p)
           }else{
            deletFlag = true
           }
        })
        newRing.length&&newRings.push(newRing)
    })
    console.log(newRings)
    if(deletFlag){
      if(newRings.length){
        item.attributes.data.action = 1
        item.geometry.set({
          rings:newRings
        })
        item.notifyGeometryChanged()
        self.polygonSketchViewModel.update(item, { tool: "reshape" })
      }else{
        polygonLayer.remove(item)
        let mapdata = useMapDataStore()
        mapdata.delData( item.attributes.data);
      }
    }






    self.view.graphics.removeAll()


  }