import * as THREE from 'three'
import { animation, init } from './meshAnimation'
import grentMesh from './grentMesh'
import config from './config'
import addText from './addText'
import { threeLayer } from './initScene3D'




async function listener(event) {
  let geoJsonArray = event.data.geoJsonArray
  geoJsonArray.forEach(j => {
    let {
      maxHeight,
      animateBuffArrays,
      fids,
      verticesBuffArrays: verticesArray,
      normalsBuffArrays: normalsArray,
      type,
      texts
    } = j

    let geometry = new THREE.BufferGeometry()
    geometry.addAttribute('position', new THREE.BufferAttribute(verticesArray, 3));
    geometry.addAttribute('normal', new THREE.BufferAttribute(normalsArray, 3));

    var mesh = grentMesh(geometry, type);
    threeLayer.getScene().add(mesh)
    mesh.maxHeight = maxHeight
    mesh.fids = fids

    init(verticesArray, maxHeight)
    animation(mesh, verticesArray, animateBuffArrays, config.stepTotal)

    addText(mesh, texts)
  })
}

export default listener