import Source from './source'
// import ss from 'simple-statistics'
// import _ from 'lodash'
import createFilter from './filter'
/**
 * 画布上各组件的通用对象，主要是各部件的通用函数，
 * 例如组件的缩放、平移、上移、下移、删除等
 */

class GeoJSONSource extends Source {
  constructor(...params) {
    super(...params)
  }

  reduceData(data) {
    //数据清洗
    if (
      !data.type ||
      (data.type !== 'Feature' && data.type !== 'FeatureCollection')
    ) {
      this.eventParent.fire('source.load', {
        id: this.id,
        data: {
          type: 'FeatureCollection',
          features: []
        }
      })
      throw new Error(
        `Error in component "${this.id}": The data is not a valid GeoJSON object`
      )
    }
    if (!this.transform) {
      return this.eventParent.fire('source.load', { id: this.id, data: data })
    }
    this.transform.forEach(t => {
      switch (t.type) {
        case 'filter':
          data = this.filter(data, t.condition || t.callback)
          break
        case 'aggregate':
          data = this.aggregate(data, t.fields, t.operations, t.as, t.groupBy)
          break
        case 'sort':
          data = this.sort(data, t.fields, t.orders)
          break
        default:
          break
      }
    })
    this.eventParent.fire('source.load', { id: this.id, data: data })
  }

  filter(data, params) {
    if (data.type === 'Feature') {
      return data
    }
    let features = data.features
    if (typeof params === 'function') {
      features = features.filter(params)
    } else if (typeof params === 'object') {
      let filterActions = createFilter(params, 'geojson')
      features = features.filter(filterActions)
    }
    data.features = features
    return data
  }

  map(data, fields, orders) {
    if (data.type === 'Feature') {
      data = {
        type: 'FeatureCollection',
        features: [data]
      }
    }
    let features = data.features
    features = features.map(f => {
      orders.forEach((order, index) => {
        if (!fields[index]) return
        switch (order) {
          case 'toNumber':
            f.properties[fields[index]] = +f.properties[fields[index]]
            break
          case 'toString':
            f.properties[fields[index]] = '' + f.properties[fields[index]]
            break
          case 'toUppercase':
            if (typeof f.properties[fields[index]] !== 'string') {
              console.warn('Can not convert non-string to uppercase')
              return
            }
            f.properties[fields[index]] = f.properties[
              fields[index]
            ].toUpperCase()
            break
          case 'toLowercase':
            if (typeof f.properties[fields[index]] !== 'string') {
              console.warn('Can not convert non-string to lowercase')
              return
            }
            f.properties[fields[index]] = f.properties[
              fields[index]
            ].toLowerCase()
            break
          default:
            break
        }
      })
      return f
    })
    data.features = features
    return data
  }

  mapAction(/*item*/) {}
}

export default GeoJSONSource
