import * as THREE from 'three';
import * as CONSTANTS from '../../../types/world/worldConstants';

export default async function switchToThirdPerson(
    controls: any,
    camera: any
) {
    if (!controls) return;
    controls.mouseButtons.left = CONSTANTS.thirdPersonControls.leftMouse;
    controls.mouseButtons.right = CONSTANTS.thirdPersonControls.rightMouse;
    controls.mouseButtons.middle = CONSTANTS.thirdPersonControls.middleMouse;
    controls.mouseButtons.wheel = CONSTANTS.thirdPersonControls.wheelMouse;
    controls.unlockPointer();

    const cameraDirection = new THREE.Vector3();
    camera.getWorldDirection(cameraDirection);
    const targetOffset = cameraDirection.multiplyScalar(CONSTANTS.thirdPersonControls.targetOffset);
    const targetPosition = new THREE.Vector3(camera.position.x, camera.position.y, camera.position.z).add(targetOffset);

    controls.setPosition(camera.position.x, CONSTANTS.thirdPersonControls.cameraHeight, camera.position.z, true);
    controls.setTarget(targetPosition.x, 0, targetPosition.z, true);

    controls.azimuthRotateSpeed = CONSTANTS.thirdPersonControls.azimuthRotateSpeed;
    controls.polarRotateSpeed = CONSTANTS.thirdPersonControls.polarRotateSpeed;
    controls.truckSpeed = CONSTANTS.thirdPersonControls.truckSpeed;
    controls.minDistance = CONSTANTS.threeDimension.minDistance;
    controls.maxDistance = CONSTANTS.thirdPersonControls.maxDistance;
    controls.maxPolarAngle = CONSTANTS.thirdPersonControls.maxPolarAngle;
}