import config from "@/api/config.js";
import * as KMap from "@/utils/ol-map/KMap";
import * as util from "@/common/ol_common.js";
import Point from "ol/geom/Point.js";
import Feature from "ol/Feature";
import VectorLayer from "ol/layer/Vector.js";
import WKT from "ol/format/WKT.js";
import ScaleLine from "ol/control/ScaleLine";
import { useRouter } from "vue-router";
import {unByKey} from "ol/Observable";
import Style from "ol/style/Style";
import Icon from "ol/style/Icon";
import {Cluster, Vector as VectorSource} from "ol/source";
import {newRegionFeature} from "../zhgl/map";

/**
 * @description 地图层对象
 */
class SamplePointLayer {
  constructor(map, farm) {
    let that = this;
    this.farmId = farm.id
    let vectorStyle = new KMap.VectorStyle();
    this.vectorStyle = vectorStyle;

    this.regionLayer = new KMap.VectorLayer("regionLayer",999,{
      minZoom:15,
      source:this.clusterSource,
      style:(f)=> this.getStyle(f)})
    map.addLayer(this.regionLayer.layer)
    this.initData(this.farmId)
  }
  getIconStyle(feature){
    let style = new Style({
      image: new Icon({
        src: feature.get('icon'),
        scale:1,
      })
    });
    return style
  }
  //得到点样式
  getStyle(feature){
    return this.getIconStyle(feature)
  }

  initData(farmId){
    let that = this
    VE_API.sample.list({farmId}).then(({data})=>{
      let features = []
      for(let item of data){
        that.getIcon(item)
        let f = newRegionFeature(item);
        features.push(f)
      }
      const source = new VectorSource({
        features: features,
      });
      that.regionLayer.setSource(source)
    })
  }

  reset(farm, region){
    this.clearCluster()
    this.initData(farm.id, region.id)
  }

  // 清除聚合图层，解除绑定
  clearCluster() {
    if (this.regionLayer) {
      this.regionLayer.layer.getSource().clear()
    }
  }
}

export default SamplePointLayer;
