import * as THREE from 'three'

export default class Event {

  event
  scene = null // 全局场景
  infos = null // 事件详情
  direct = null // 方向
  lanex // 挂载车道

  sign2d = null // 事件标识对象
  vm_instance = null

  constructor (context, infos) {

    this.scene = context.scene
    this.infos = infos
    this.direct = infos.direct
    this.lanex = infos.lanex

    this.setPoint(context)

  }

  setPoint (context) {

    const curve = context.curves[this.infos.lanex - 1]
    let progress = ((this.direct === 0 ? (context.end - this.infos.location) : (this.infos.location - context.start)) / context.miles).toFixed(2)
    
    if (progress < 0) progress = 0
    if (progress > 1) progress = 1

    const position = curve.getPointAt(progress)
    position.y += 0.05

    const geometry = new THREE.BoxGeometry(0.1, 0.1, 0.1)
    const material = new THREE.MeshBasicMaterial(0x8F373E)

    this.event = new THREE.Mesh(geometry, material)

    this.event.position.copy(position)
    this.scene.add(this.event)

  }

  clean () {
    this.event.remove(this.sign2d)
    this.scene.remove(this.event)
  }

}