<script>
import { GeometryType } from '@/base/GeometryType.js'
export default {
  name: 'bs-feature',
  render () { },
  props: {
    feature: {
      type: Object,
      require: true
    }
  },
  data () {
    return {
      attr: {},
      coods: [],
      center: []
    }
  },
  watch: {
    feature (val) {
      if (val) {
        this.attr = val.get('attributes')
        const type = val.getGeometry().getType()
        if (type === GeometryType.POINT) {
          this.center = val.getGeometry().getCoordinates()
          this.coods = val.getGeometry().getCoordinates()
        }
        if (type === GeometryType.POLYGON) {
          this.center = val.getGeometry().getInteriorPoint().getCoordinates()
          this.coods = val.getGeometry().getCoordinates()
        }
        if (type === GeometryType.MULTIPOLYGON) {
          const bounds = val.getGeometry().getExtent()
          this.center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2]
          const polygons = val.getGeometry().getPolygons()
          for (let polygon of polygons.values()) {
            this.coods.push(polygon.getCoordinates())
          }
        }
        if (type === GeometryType.POLYLINE) {
          this.center = val.getGeometry().getLastCoordinate()
          this.coods = val.getGeometry().getCoordinates()
        }
      }
    }
  },
  methods: {
    getAttributes () {
      return this.attr
    },
    getCoods () {
      return this.coods
    },
    getCenter () {
      return this.center
    }
  },
  created () {
    if (this.feature) {
      this.attr = this.feature.get('attributes')
      const type = this.feature.getGeometry().getType()
      if (type === GeometryType.POINT) {
        this.center = this.feature.getGeometry().getCoordinates()
        this.coods = this.feature.getGeometry().getCoordinates()
      }
      if (type === GeometryType.POLYGON) {
        this.center = this.feature.getGeometry().getInteriorPoint().getCoordinates()
        this.coods = this.feature.getGeometry().getCoordinates()
      }
      if (type === GeometryType.MULTIPOLYGON) {
        const bounds = this.feature.getGeometry().getExtent()
        this.center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2]
        const polygons = this.feature.getGeometry().getPolygons()
        for (let polygon of polygons.values()) {
          this.coods.push(polygon.getCoordinates())
        }
      }
      if (type === GeometryType.POLYLINE) {
        this.center = this.feature.getGeometry().getLastCoordinate()
        this.coods = this.feature.getGeometry().getCoordinates()
      }
    }
  },
  mounted () { }
}
</script>
