import TFMapUitl from '/@/utils/TFMapUitl.ts';
import startImg from '../../../assets/images/startPoint.png';
import endImg from '../../../assets/images/endPoint.png';
import eventBus from '/@/utils/eventBus';
import { getRiverInfoById } from '/@/api/map/getData.ts';
class WaterLocation{

    currentHl = {
      river: null,
      start: null,
    };

    /**
     * 定位
     */
    goTo(id) {
      getRiverInfoById({id:id}).then(res=>{
        if(res.code==1){
          this.deleteHL();
          window.setTimeout(e=>{
            this.addHL(res.result);
            this.addMark(res.result);
          },50)
        }
      })
    
    }

    /**
     * 添加河流
     */
    addHL(item) {
      if (!item.txjson) {
        return;
      }
      const tempData = JSON.parse(item.txjson);
      const tempEnt = new TFMap.GeoPolyline(tempData, 14, 'rgba(255,255,0)');
      tempEnt.flyTo();
      this.currentHl.river = TFMapUitl.getId('drainageBasin', item.id);
      // tempEnt.setId(currentHl.river);
      tempEnt.setClassify(this.currentHl.river);
      tempEnt.info = item;
      tempEnt.addListener(map, (e) => {
        var info = e.graphic.overlay.info;
        eventBus.emit('drainageBasinInfoShow', info.id);
      });
      map.addOverlay(tempEnt);
    }

    addMark(item) {
      const tempAddMark = (image, deviceLon, deviceLat) => {
        var marker = new TFMap.Marker([deviceLon, deviceLat], image).init();
        const className = 'drainageBasin_img*';
        marker.setClassify(className);
        marker.info = item;
        map.addOverlay(marker);
        return className;
      };
      if (item.startLocationLat && item.startLocationLon) {
        this.currentHl.start = tempAddMark(startImg, item.startLocationLon, item.startLocationLat);
      }
      if (item.endLocationLat && item.endLocationLon) {
        tempAddMark(endImg, item.endLocationLon, item.endLocationLat);
      }
    }

    
    /**
     * 删除河流
     */
    deleteHL() {
      for (let item in this.currentHl) {
        if (this.currentHl[item]) {
          map.removeOverlay(this.currentHl[item]);
          this.currentHl[item] = null;
        }
      }
    }
}

const waterLocation=new WaterLocation();
export default  waterLocation