import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import * as TWEEN from 'three/examples/jsm/libs/tween.module.js'

// import { viewCubeUpdate } from './DmtViewCube'
const _changeEvent = { type: 'change' }
class DmtControl extends OrbitControls {
  constructor(object, domElement) {
    super(object, domElement)
    this.cubeControlDirection = {
      'TOP': 'TOP',
      'BOTTOM': 'BOTTOM',
      'FRONT': 'FRONT',
      'BACK': 'BACK',
      'LEFT': 'LEFT',
      'RIGHT': 'RIGHT',

      'BACK-LEFT': 'BACK-LEFT',
      'FRONT-LEFT': 'FRONT-LEFT',
      'TOP-LEFT': 'TOP-LEFT',
      'TOP-RIGHT': 'TOP-RIGHT',
      'RIGHT-BOTTOM': 'RIGHT-BOTTOM',
      'LEFT-BOTTOM': 'LEFT-BOTTOM',
      'BACK-BOTTOM': 'BACK-BOTTOM',
      'TOP-FRONT': 'TOP-FRONT',
      'FRONT-BOTTOM': 'FRONT-BOTTOM',
      'TOP-BACK': 'TOP-BACK',
      'RIGHT-FRONT': 'RIGHT-FRONT',
      'BACK-RIGHT': 'BACK-RIGHT',

      'BACK-RIGHT-BOTTOM': 'BACK-RIGHT-BOTTOM',
      'TOP-RIGHT-BACK': 'TOP-RIGHT-BACK',
      'TOP-LEFT-BACK': 'TOP-LEFT-BACK',
      'TOP-LEFT-FRONT': 'TOP-LEFT-FRONT',
      'FRONT-BOTTOM-RIGHT': 'FRONT-BOTTOM-RIGHT',
      'TOP-RIGHT-FRONT': 'TOP-RIGHT-FRONT',
      'BACK-BOTTOM-LEFT': 'BACK-BOTTOM-LEFT',
      'FRONT-BOTTOM-LEFT': 'FRONT-BOTTOM-LEFT'
    }

    const scope = this
    const STATE = {
      NONE: -1,
      ROTATE: 0,
      DOLLY: 1,
      PAN: 2,
      TOUCH_ROTATE: 3,
      TOUCH_PAN: 4,
      TOUCH_DOLLY_PAN: 5,
      TOUCH_DOLLY_ROTATE: 6
    }
    // eslint-disable-next-line no-unused-vars
    let state = STATE.NONE
    this.resetState = function(target0, position0, zoom0) {
      scope.target.copy(target0)
      scope.object.position.copy(position0)
      if (scope.object.isOrthographicCamera) {
        scope.object.zoom = zoom0
      } else {
        scope.object.zoom = 1
      }
      scope.object.updateProjectionMatrix()
      scope.dispatchEvent(_changeEvent)
      scope.update()
      state = STATE.NONE
      // viewCubeUpdate()
    }

    this.resetStateTween = function(target1, current1, target2, current2, duration) {
      const scope = this
      const positionVar = {
        x1: current1.x,
        y1: current1.y,
        z1: current1.z,
        x2: target1.x,
        y2: target1.y,
        z2: target1.z
      }
      // 关闭控制器
      scope.enabled = false
      var tween = new TWEEN.Tween(positionVar)
      tween.to({
        x1: current2.x,
        y1: current2.y,
        z1: current2.z,
        x2: target2.x,
        y2: target2.y,
        z2: target2.z
      }, duration)
      tween.onUpdate(function() {
        scope.object.position.x = positionVar.x1
        scope.object.position.y = positionVar.y1
        scope.object.position.z = positionVar.z1
        scope.target.x = positionVar.x2
        scope.target.y = positionVar.y2
        scope.target.z = positionVar.z2
        scope.object.updateProjectionMatrix()
        scope.update()
      })
      tween.start()
      tween.onComplete(function() {
        // /开启控制器
        scope.enabled = true
      })
      tween.easing(TWEEN.Easing.Cubic.InOut)
    }

    this.setLookAt = function(position0, target0, zoom0) {
      scope.target.copy(target0)
      scope.object.position.copy(position0)
      scope.object.zoom = zoom0
      scope.object.updateProjectionMatrix()
      scope.dispatchEvent(_changeEvent)
      scope.update()
      state = STATE.NONE
    }
    this.setLookAt1 = function(target0, rotation) {
      scope.target.copy(this.target)
      scope.object.rotation.copy(rotation)
      scope.object.updateProjectionMatrix()
      scope.dispatchEvent(_changeEvent)
      scope.update()
      state = STATE.NONE
    }
    this.moveTo = function(x, y, z) {
      scope.target.set(x, y, z)
    }
    this.getState = function() {
      return {
        target: scope.target.clone(),
        position: scope.object.position.clone(),
        zoom: parseFloat(scope.object.zoom.toString())
      }
    }
  }
}
export {
  DmtControl
}

