/**
 * @Author: Caow
 * @Date: 2021-05-05 09:16:35
 */

import { Cesium } from '@bc-modules/namespace'
import State from '@bc-modules/state/State'
import Parse from '@bc-modules/parse/Parse'
import { Util } from '@bc-modules/utils'
import { Transform } from '@bc-modules/transform'
import Overlay from '../Overlay'
import DynamicOverlay from './DynamicOverlay'

class DynamicModel extends DynamicOverlay {
  constructor(position, modelUrl) {
    super()
    this._posistion = Parse.parsePosition(position)
    this._modelUrl = modelUrl
    this._delegate = new Cesium.Entity({ model: {} })
    this.type = Overlay.getOverlayType('dynamic-model')
    this._state = State.INITIALIZED
  }

  set modelUrl(modelUrl) {
    this._modelUrl = modelUrl
    this._delegate.model.uri = this._modelUrl
    return this
  }

  get modelUrl() {
    return this._modelUrl
  }

  _mountedHook() {
    /**
     * set the location
     */
    this._samplePosition.forwardExtrapolationType =
      Cesium.ExtrapolationType.HOLD
    this._startTime = Cesium.JulianDate.now()
    this._samplePosition.addSample(
      this._startTime,
      Transform.transformWGS84ToCartesian(this._posistion)
    )
    this._delegate.position = this._samplePosition
    this._delegate.orientation = new Cesium.VelocityOrientationProperty(
      this._samplePosition
    )
    this._cache.push(this._startTime)
    /**
     *  initialize the Overlay parameter
     */
    this.modelUrl = this._modelUrl
  }

  /**
   * Sets style
   * @param style
   * @returns {DynamicModel}
   */
  setStyle(style) {
    if (!style || Object.keys(style).length === 0) {
      return this
    }
    delete style['uri']
    this._style = style
    Util.merge(this._delegate.model, this._style)
    return this
  }
}

Overlay.registerType('dynamic-model')

export default DynamicModel
