import Layer from '../layer'
import { HeatmapLayer } from '@cgcs2000/l7'

class Heatmap extends Layer {
  constructor(layer, scene, zIndex) {
    super(layer, scene, zIndex)
    let { visible, minZoom, maxZoom } = this.layer
    this.layerInstance = new HeatmapLayer({
      visible: visible === undefined ? true : !!visible,
      minZoom: Number(minZoom) || 0,
      maxZoom: Number(maxZoom) || 18,
      zIndex
    })
    this.setSource()
  }

  setShape() {
    this.layerInstance.shape('heatmap')
  }

  setStyle() {
    let { style } = this._mergeProperties()
    let { field, intensity, radius, colors } = style
    if (!field) {
      console.error('错误：未指定专题字段')
    }
    if (colors.length !== 6) {
      console.error('错误：热力图的颜色数组必须为6个值')
    }
    this.layerInstance.size(field, [0, 1])
    this.layerInstance.style({
      intensity,
      radius,
      rampColors: {
        colors,
        positions: [0, 0.2, 0.4, 0.6, 0.8, 1.0]
      }
    })
  }
}

export default Heatmap
