<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'
import turf_point from 'turf-point'
import turf_boolean_contains from '@turf/boolean-contains'
import turf_bbox  from '@turf/bbox-polygon'
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
    },
    allCount:{
       type: Number,
        require: true,
        default: 0
    },
    zIndex: {
      type: Number,
      require: true,
      default: 1
    },
    radius:{
      type: Number,
      default: 15
    },
    blur:{
      type: Number,
      default: 1
    },
    url:{
      type: String
    },
    format:{
      type: String,
      default: "geojson"
    }
  },
  watch: {
    features (val) {
      if (this.url) {
        return
      }
      this.originInstance.getSource().clear()
      this.addFeatures()
      this.reload();
    },
    visible (val) {
      this.originInstance.setVisible(val)
    },
    zIndex(val) {
      this.originInstance.setZIndex(val)
    }
  },
  methods: {
    load () {
      var _this = this;
      const { id, $rootComponent, zIndex, url, format, blur, radius, visible } = _this
      if ($rootComponent.isLayerExist(id)) {//$rootComponent为map组件
           $rootComponent.removeLayer(id)
      }
      console.log("热力图初始化......")
      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,
                blur,
                weight:function(feature) {
                      let count = feature.attributes
                      let num = count/_this.allCount
                      return num
                  }
            })
          _this.originInstance.setZIndex(zIndex)
        } else {
            _this.originInstance = new Heatmap({
                source: new VectorSource({strategy: bboxStrategy}),
                id,
                name,
                blur,
                radius,
                blur,
                weight:function(feature) {
                    let count = feature.attributes
                    let num = count/_this.allCount
                    return num
                  }
              })  
            _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()+'')
      })
      $rootComponent.$on('zoom', () => {
          // this.originInstance.getSource().clear()
          // this.addFeatures()
      })
    },
    addFeatures () {
      const { features,  $rootComponent} = this
      let currentExtent =  $rootComponent.getCurrentExtent() // 获取地图的当前可视范围----
      let _extent = [currentExtent.minX, currentExtent.minY, currentExtent.maxX, currentExtent.maxY]
      let turf_poly = turf_bbox(_extent)
      if (features) {
        let vectors = []
        for (var index in features) {//循环前端传递过来的热力图数组对象
          var feature = features[index]
          let { geom} = feature
          var geometry = covertObjToGeometry(geom)
          let count = feature.attributes.count;
          let _coord = geometry.getCoordinates()
          let turf_geom = turf_point(_coord)
          // if (turf_boolean_contains(turf_poly, turf_geom)) {
             var vector = new Feature()
             vector.attributes = count;
             vector.setGeometry(geometry)
             vectors.push(vector)
          // }      
        }
        this.originInstance.getSource().addFeatures(vectors)
      }
    }
  }
}
</script>
