    import FeatureRender from '../base/FeatureRender'
    import FeatureCollection from '../document/layer/support/FeatureCollection'
    import { defaultValue, Log } from '../util'
    import { BaseRendererLayerView } from './index'
    import { applyRenderer } from './support/ApplyRenderer'
    import { Feature, FeatureSet } from '../base'

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

      /**
       * @description: 定义监听器
       * @return {*}
       */
      _initHandlers() {
        super._initHandlers()
        this._cacheFeatureCollectionChangeAction =
          this._cacheFeatureCollectionChangeAction.bind(this)
        // 监听要素属性变化，更新操作
        this._cacheFeatureCollection.on(
          'set',
          this._cacheFeatureCollectionChangeAction
        )
        // 监听要素变化,进行绘制或者删除操作
        this._cacheFeatureCollection.on('change', (e) => {
          if (e) {
            if (e.added) {
              this.render.drawFeature(this.innerLayer, e.added)
            }
            if (e.removed) {
              this.render.clearFeature(this.innerLayer, e.removed)
            }
          }
        })
      }

      /**
       * @description: 移除监听器
       * @return {*}
       */
      _removeHandlers() {
        super._removeHandlers()
        this._off('set', this._cacheFeatureCollectionChangeAction)
      }

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

      /**
       * @description: 要素缓存变化Action
       * @param {*} event
       * @return {*}
       */
      _cacheFeatureCollectionChangeAction(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 {*} 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('传入要素集错误')
        }
        // 设置renderer信息
        applyRenderer(features, renderer)
        // 添加要素
        this._cacheFeatureCollection.addMany(features)
      }
    }

    export default BaseFeatureLayerView