import PointLayer from './point'
import util from '../libs/util'

class Scatter extends PointLayer {
  constructor(layer, scene, zIndex) {
    super(layer, scene, zIndex)
  }

  setShape() {
    let { shape } = this._mergeProperties()
    if (typeof shape === 'string') {
      if (shape.startsWith('image://')) {
        let imgId = util.generateId()
        this._scene.addImage(imgId, shape.replace('image://', ''))
        this.layerInstance.shape(imgId)
        this.isRasterShape = true
      } else {
        this.layerInstance.shape(shape)
      }
    } else {
      this.layerInstance.shape(shape.field, shape.value)
    }
  }

  setStyle() {
    let { style } = this._mergeProperties()
    let { size, color, animate } = style
    if (typeof color === 'string') { // 单色
      if(!this.isRasterShape) {
        this.layerInstance.color(color)
      }
      if (typeof size === 'object') {
        let { field, type, stops } = size
        if (type === 'interval') {
          this.layerInstance.size(field, value => {
            let st = stops.find(stop => {
              let { min, max } = stop
              min = typeof min !== 'number' ? -Infinity : min
              max = typeof max !== 'number' ? Infinity : max
              return value >= min && value <= max
            })
            if (st) {
              return st.size
            } else {
              return 4
            }
          })
        } else {
          this.layerInstance.size(field, value => {
            let st = stops.find(stop => stop.value == value)
            if (st) {
              return st.size
            } else {
              return 4
            }
          })
        }
      } else {
        this.layerInstance.size(Array.isArray(size) ? size[0] : size)
      }
    } else { // 多色
      let { field, type, stops } = color
      if (type === 'interval') {
        if (!this.isRasterShape) {
          this.layerInstance.color(field, value => {
            let st = stops.find(stop => {
              let { min, max } = stop
              min = typeof min !== 'number' ? -Infinity : min
              max = typeof max !== 'number' ? Infinity : max
              return value >= min && value <= max
            })
            if (st) {
              return st.color
            } else {
              return '#000000'
            }
          })
        }
        if (Array.isArray(size)) {
          let step = (size[1] - size[0]) / (stops.length - 1)
          this.layerInstance.size(field, value => {
            let stIndex = stops.findIndex(stop => {
              let { min, max } = stop
              min = typeof min !== 'number' ? -Infinity : min
              max = typeof max !== 'number' ? Infinity : max
              return value >= min && value <= max
            })
            if (stIndex !== -1) {
              return size[0] + step * stIndex
            } else {
              return 4
            }
          })
        } else {
          this.layerInstance.size(size)
        }
      } else {
        if (!this.isRasterShape) {
          this.layerInstance.color(field, value => {
            let st = stops.find(stop => stop.value == value)
            if (st) {
              return st.color
            } else {
              return '#000000'
            }
          })
        }
        if (Array.isArray(size)) {
          let step = (size[1] - size[0]) / (stop.length - 1)
          this.layerInstance.size(field, value => {
            let stIndex = stops.findIndex(stop => stop.value == value)
            if (stIndex !== -1) {
              return size[0] + step * stIndex
            } else {
              return 4
            }
          })
        } else {
          this.layerInstance.size(size)
        }
      }
    }
    this.layerInstance.style({
      opacity: style.opacity,
      stroke: style.stroke,
      strokeWidth: style.strokeWidth
    })
    this.layerInstance.animate(animate || false)
  }
}

export default Scatter
