<script>
import commonMixin from '../base/mixins/common.js'
import { GeometryType } from '../base/GeometryType.js'
import { covertObjToGeometry } from '../base/GeometryUtil.js'
import  Feature  from 'ol/Feature'
import Heatmap  from 'ol/layer/Heatmap'
import GeoJSON from 'ol/format/GeoJSON'
import KML from 'ol/format/KML'
import VectorSource from 'ol/source/Vector'
import {bbox as bboxStrategy} from 'ol/loadingstrategy'
export default {
  name: 'sl-heatlayer',
  render (h) { },
  mixins: [commonMixin('layer')],
  props: {
    id: {
      type: String,
      require: true
    },
    name: {
      type: String,
      require: true
    },
    features: {
      type: Array
    },
    visible: {
      type: Boolean,
      default: true
    },
    zIndex: {
      type: Number,
      require: true,
      default: 1
    },
    blur: {
      type: Number,
      default: 30
    },
    radius:{
      type: Number,
      default: 15
    },
    url:{
      type: String
    },
    format:{
      type: String,
      default: "geojson"
    }
  },
  watch: {
    features (val) {
      if (this.url) {
        return
      }
      this.originInstance.getSource().clear()
      this.addFeatures()
    },
    visible (val) {
      this.originInstance.setVisible(val)
    },
    zIndex(val) {
      this.originInstance.setZIndex(val)
    }
  },
  methods: {
    load () {
      const { id, $rootComponent, zIndex, url, format, blur, radius, visible } = this
      if ($rootComponent.isLayerExist(id)) {
           $rootComponent.removeLayer(id)
      }
      let featureFormat = new GeoJSON()
      if (format === "kml") {
         featureFormat = new KML()
      }
      if (url) {
          let param =  {
               format: featureFormat,
               url,
               strategy: bboxStrategy
          }
          var source = new VectorSource(param)
          this.originInstance = new Heatmap({
                source,
                id,
                name,
                blur,
                radius
            })
          this.originInstance.setZIndex(zIndex)
        } else {
            this.originInstance = new Heatmap({
                source: new VectorSource({strategy: bboxStrategy}),
                id,
                name,
                blur,
                radius
              })  
            this.originInstance.setZIndex(zIndex)
            this.addFeatures()
          }
        // 添加热点图层----------
        this.map.addLayer(this.originInstance)
        this.originInstance.setVisible(visible)
        this.originInstance.getSource().on('addfeature', function(event) {
        event.feature.set('weight', Math.random()+'')
      })
    },
    addFeatures () {
      const { features } = this
      if (features) {
        let vectors = []
        for (var index in features) {
          var feature = features[index]
          let { geom, attributes } = feature
          var geometry = covertObjToGeometry(geom)
          var vector = new Feature()
          vector.setGeometry(geometry)
          vector.set('attributes', attributes)
          vector.setId(attributes.id)
          vectors.push(vector)
        }
        this.originInstance.getSource().addFeatures(vectors)
      }
    }
  }
}
</script>
