    import FeatureRender from '../base/FeatureRender'
    import FeatureCollection from '../document/layer/support/FeatureCollection'
    import { defaultValue, Log } from '../util'
    import { BaseRendererLayerView } from './index'
    import { updateRenderer } from './support/RendererUtil'
    import { Feature, FeatureSet } from '../base'
    import { RendererType } from '../base/enum'
    import { setElevationInfo } from '../document'

    class BaseFeatureLayerView extends BaseRendererLayerView {
      _initInstance(options) {
        super._initInstance(options)
        // 渲染器
        this.render = defaultValue(options.render, new FeatureRender())
        // 要素缓存
        this._featureSetCache = new FeatureCollection()
      }

      /**
       * @description: 定义监听器
       * @return {*}
       */
      _initHandlers() {
        super._initHandlers()
        this._onFeatureChange = this._onFeatureChange.bind(this)
        this._onFeatureSetCacheChange = this._onFeatureSetCacheChange.bind(this)
        this._zoomChange = this._zoomChange.bind(this)
        // 监听要素属性变化，更新操作
        this._featureSetCache.on('set', this._onFeatureChange)
        // 监听要素变化,进行绘制或者删除操作
        this._featureSetCache.on('change', this._onFeatureSetCacheChange)
        // 监听层级变化
        this.view.on('zoom', this._zoomChange)
      }

      /**
       * @description: 移除监听器
       * @return {*}
       */
      _removeHandlers() {
        super._removeHandlers()
        this.off('set', this._onFeatureChange)
        this.off('change', this._onFeatureSetCacheChange)
        this.view.off('zoom', this._zoomChange)
      }

      /**
       * @description: renderer变化Action
       * @return {*}
       */
      _rendererChangeAction(event) {
        super._rendererChangeAction(event)
        const featureItems = this._featureSetCache.items || []
        updateRenderer(featureItems, this.layer.renderer)
      }

      /**
       * @description: 单个要素变化Action
       * @param {*} event
       * @return {*}
       */
      _onFeatureChange(event) {
        const eventPath = event.eventPath || []
        const { value } = event.eventData
        const sourceEventData = event.sourceEventData
        // 处理要素刷新机制
        if (eventPath.length <= 0 || !value) return
        if (eventPath.length > 1) {
          // 获取到要素
          const feature = value
          if (!feature) return
          const updateType = eventPath[1]
          const updateDetail = {
            geometryUpdated: null,
            symbolUpdated: null
          }
          if (updateType === 'symbol') {
            updateDetail.symbolUpdated = sourceEventData
          } else if (updateType === 'geometry') {
            updateDetail.geometryUpdated = sourceEventData
          }
          this.render.updateFeature(this.innerLayer, [feature], [updateDetail])
        }
      }

      /**
       * @description: 要素数据集更新
       * @param {*} e
       * @return {*}
       */
      _onFeatureSetCacheChange(e) {
        if (e) {
          if (e.added) {
            this.render.drawFeature(this.innerLayer, e.added)
          }
          if (e.removed) {
            this.render.clearFeature(this.innerLayer, e.removed)
          }
        }
      }

      /**
       * @description: 层级变化
       * @param {*}
       * @return {*}
       */
      _zoomChange(e) {
        if (this.layer && this.layer.renderer) {
          const renderer = this.layer.renderer
          // 根据层级实时变化
          if (renderer.type === RendererType.random) {
            const featureItems = this._featureSetCache.items || []
            updateRenderer(featureItems, renderer)
          }
        }
      }

      /**
       * @description: 初始化要素
       * @param {*} featureSet
       * @param {*} renderer
       * @return {*}
       */
      _initFeatures(featureSet, renderer) {
        let features = []
        if (featureSet instanceof FeatureSet) {
          features = featureSet.features
        } else if (
          Array.isArray(featureSet) &&
          featureSet.every((feature) => feature instanceof Feature)
        ) {
          features = featureSet
        } else {
          Log.info('传入要素集错误')
        }
        // 如果要素上没有高程参数，就用图层上的，否则用要素自己的
        for (let i = 0; i < features.length; i++) {
          setElevationInfo(features[i], this.layer)
        }
        // 设置renderer信息
        updateRenderer(features, renderer)
        // 添加要素
        this._featureSetCache.addMany(features)
      }

      /**
       * 移除图层视图
       * @param {LayerEvent} 图层事件
       * @return {Promise<LayerView>} 图层视图
       */
      onRemove(event) {
        this._featureSetCache.removeAll()
        super.onRemove(event)
      }
    }

    export default BaseFeatureLayerView