<script>
import commonMixin from '@/base/mixins/common.js'
import { getSybmol } from '@/base/SymbolUtil.js'
import { GeometryType } from '@/base/GeometryType.js'
import SymbolConfig from '@/base/SymbolConfig.js'
import { getGeom } from '@/base/GeometryUtil.js'
export default {
  name: 'vector',
  render (h) { },
  mixins: [commonMixin('layer')],
  props: {
    id: {
      type: String,
      require: true
    },
    name: {
      type: String,
      require: true
    },
    styleConfig: {
      type: Object,
      default: function () {
        return SymbolConfig['defaultPoint']
      }
    },
    type: {
      type: String,
      default: GeometryType.POINT
    },
    features: {
      type: Array
    }
  },
  watch: {
    features (val) {
      this.originInstance.getSource().clear()
      this.addFeatures()
    }
  },
  methods: {
    load () {
      const { id, name, styleConfig, $rootComponent, type } = this
      this.originInstance = $rootComponent.addLayer({ id, name })
      if (styleConfig) {
        const sym = getSybmol(styleConfig, type)
        this.originInstance.setStyle(sym)
      }
      console.log(this.originInstance.get('id') + this.originInstance.get('name'))
      this.addFeatures()
    },
    addFeatures () {
      const { features, ol, styleConfig } = this
      if (features) {
        let vectors = []
        for (var index in features) {
          var feature = features[index]
          let { geom, symbol, attributes } = feature
          var geometry = getGeom(geom)
          var vector = new ol.Feature()
          vector.setGeometry(geometry)
          if (symbol) {
            var sty = getSybmol(geom.type, symbol)
            vector.setStyle(sty)
          } else {
            vector.setStyle(getSybmol(geom.type, styleConfig))
          }
          vector.set('attributes', attributes)
          vectors.push(vector)
        }
        this.originInstance.getSource().addFeatures(vectors)
      }
    }
  }
}
</script>
