import Layer from 'ol/layer/Vector'
import Source from 'ol/source/Vector'
import Common from './Common'
import Select from 'ol/interaction/Select.js';
import {singleClick} from 'ol/events/condition'
import * as olEvents from 'ol/events';
import {OrderFunction} from 'ol/render';
/**
 * @description KMap.VectorLayer 矢量图层
 */
class VectorLayer {
  /**
   * @param {string} name 图层名称
   * @param {string} zIndex 图层层级
  */
  constructor(name,zIndex,options){
    let source = new Source({
      crossOrigin:'anonymous',
    })
    let ShowLevel = Common.ShowLevel
    let minZoom = ShowLevel[0]
    let maxZoom = ShowLevel[1]
    let style = null;
    if(options && options.source){
      source = options.source
    }
    if(options && options.minZoom){
      minZoom = options.minZoom
    }
    if(options && options.maxZoom){
      maxZoom = options.maxZoom
    }
    if(options && options.style){
      style = options.style
    }
    let layer = new Layer({
      source:source,
      name:name,
      zIndex:zIndex,
      minZoom:minZoom,
      maxZoom:maxZoom,
    })
    if(style){
      layer.setStyle(style)
    }
    this.layer = layer
    this.source = source
  }
  setMaxZoom(maxZoom){
    this.layer.setMaxZoom(maxZoom);
  }
  setMinZoom(minZoom){
    this.layer.setMinZoom(minZoom);
  }
  addFeature(feature){
    this.source.addFeature(feature)
  }
  refresh(){
    this.source.refresh()
  }
  getFeatureById(id){
    return this.source.getFeatureById(id)
  }
  addSingleSelect(callback,map,style){
    let option = {
        condition:singleClick,
        layers:[this.layer],
        multi:false
    };
    if(style){
      option["style"] = style
    }
    this.singleSelect = new Select(option)
    this.singleSelect.on("select",callback)
    map.addInteraction(this.singleSelect)
  }
  addToggleSelect(callback,map,style){
    let option = {
      condition:singleClick,
      toggleCondition: singleClick,
      layers:[this.layer],
      multi:true
    };
    if(style){
      option["style"] = style
    }
    this.toggleSelect = new Select(option)
    this.toggleSelect.on("select",callback)
    map.addInteraction(this.toggleSelect)
  }
}

export default VectorLayer
