import { Point } from '@mapgis/webclient-common'

class SceneViewUtil {}

SceneViewUtil.getMouseEvent = function (event, target, type) {
  const cartesian = target._innerView.scene.pickPosition(event.position)
  if (!cartesian) return
  const cartographic = Cesium.Cartographic.fromCartesian(cartesian)
  return {
    mapPoint: new Point({
      coordinates: [
        cartographic.longitude,
        cartographic.latitude,
        cartographic.height
      ]
    }),
    cartesianPoint: new Point({
      coordinates: [cartesian.x, cartesian.y, cartesian.z]
    }),
    x: event.position.x,
    longitude: cartographic.longitude,
    y: event.position.y,
    latitude: cartographic.latitude,
    height: cartographic.height,
    event,
    target,
    type
  }
}

SceneViewUtil.getMouseMoveEvent = function (event, target, type) {
  let _event = {
    event,
    target,
    type
  }
  const endPositionCartesian = target._innerView.scene.pickPosition(
    event.endPosition
  )
  const startPositionCartesian = target._innerView.scene.pickPosition(
    event.startPosition
  )

  if (endPositionCartesian) {
    const endPositionGraphic =
      Cesium.Cartographic.fromCartesian(endPositionCartesian)
    _event = Object.assign(_event, {
      mapPoint: new Point({
        coordinates: [
          endPositionGraphic.longitude,
          endPositionGraphic.latitude,
          endPositionGraphic.height
        ]
      }),
      cartesianPoint: new Point({
        coordinates: [
          endPositionCartesian.x,
          endPositionCartesian.y,
          endPositionCartesian.z
        ]
      }),
      x: event.endPosition.x,
      longitude: endPositionGraphic.longitude,
      y: event.endPosition.y,
      latitude: endPositionGraphic.latitude,
      height: endPositionGraphic.height
    })
  }

  if (startPositionCartesian) {
    const startPositionGraphic = Cesium.Cartographic.fromCartesian(
      startPositionCartesian
    )
    _event.prevPoint = new Point({
      coordinates: [
        startPositionGraphic.longitude,
        startPositionGraphic.latitude,
        startPositionGraphic.height
      ]
    })
  }
  return _event
}

SceneViewUtil.getKeyEvent = function (event, target, type) {
  return {
    key: event.originalEvent.key,
    timeStamp: event.originalEvent.timeStamp,
    event: event.originalEvent,
    target,
    type
  }
}

SceneViewUtil.getViewChangeEvent = function (event, target, type) {
  const _event = {
    event,
    target,
    type
  }
  _event.mapView = target
  _event.scale = target._scale
  _event.zoom = target._zoom
  _event.center = target.center
  return _event
}
export default SceneViewUtil
