var move = {
    moveElement: undefined,
    isVR: false,
    touchConfig: {
        touchX: 0,
        touchY: 0,
    },
    touchDistance: 0,
    ieMovement: {
        x: 0,
        y: 0
    },
    init: function (element) {
        this.moveElement = element;
    },

    //#region 电脑绑定事件
    /**
     * pc鼠标移动的事件
     */
    pcMove_noVR: function () {
        //清理PC VR事件
        this.moveElement.removeEventListener('mousemove', move.onPcMouseMove_VR, false);
        //使用PC正常移动事件
        this.moveElement.addEventListener('mousedown', move.onPcMouseDown_noVR, false);
        this.moveElement.addEventListener('wheel', move.onPcMouseWheel_noVR, false);
    },
    pcMove_VR: function () {
        //清理PC正常移动事件
        this.moveElement.removeEventListener('mousedown', move.onPcMouseDown_noVR, false);
        this.moveElement.removeEventListener('wheel', move.onPcMouseWheel_noVR, false);
        //使用PC VR事件
        this.moveElement.addEventListener('mousemove', move.onPcMouseMove_VR, false);
    },

    //#region PC正常移动事件
    onPcMouseDown_noVR: function (event) {
        event.preventDefault();
        move.moveElement.style.cursor = 'move';
        move.moveElement.addEventListener('mousemove', move.onPcMouseMove_noVR, false);
        move.moveElement.addEventListener('mouseup', move.onPcMouseUp_noVR, false);

        move.ieMovement.x = event.screenX;
        move.ieMovement.y = event.screenY;
    },
    onPcMouseMove_noVR: function (event) {
        event.preventDefault();
        var info = Util.decryptQuaternion(window.that.camera.quaternion); //解析当前相机四元数
        window.that.currentMoveConfig.lon = THREE.Math.radToDeg(info.theta);
        window.that.currentMoveConfig.lat = THREE.Math.radToDeg(info.delta);
        var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || event.screenX - move.ieMovement.x || 0;
        var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || event.screenY - move.ieMovement.y || 0;
        move.ieMovement.x = event.screenX;
        move.ieMovement.y = event.screenY;
        window.that.currentMoveConfig.lon += movementX * 0.1;
        window.that.currentMoveConfig.lat += movementY * 0.1;
        window.that.currentMoveConfig.lat = Math.max(-85, Math.min(85, window.that.currentMoveConfig.lat));
        window.that.currentMoveConfig.lon = Math.max(-360, Math.min(360, window.that.currentMoveConfig.lon));
        if (window.that.currentMoveConfig.lon === -360 || window.that.currentMoveConfig.lon === 360) {
            window.that.currentMoveConfig.lon = 0;
        }
        window.that.currentMoveConfig.phi = THREE.Math.degToRad(window.that.currentMoveConfig.lat);
        window.that.currentMoveConfig.theta = THREE.Math.degToRad(window.that.currentMoveConfig.lon);

        var newQuaternion = Util.encryptQuaternion(window.that.currentMoveConfig.theta, window.that.currentMoveConfig.phi);
        window.that.camera.quaternion.copy(newQuaternion);


    },
    onPcMouseUp_noVR: function (event) {
        event.preventDefault();
        move.moveElement.style.cursor = 'default';
        move.moveElement.removeEventListener('mousemove', move.onPcMouseMove_noVR, false);
        move.moveElement.removeEventListener('mouseup', move.onPcMouseUp_noVR, false);
    },
    //旧方法 修改FOV
    // onPcMouseWheel_noVR: function (event) {
    //     var camera = window.that.camera;
    //     var fov = camera.fov + event.deltaY * 0.05;
    //     camera.fov = THREE.Math.clamp(fov, window.that.cameraConfig.minFov, window.that.cameraConfig.maxFov);
    //     camera.updateProjectionMatrix();
    // },
    //更改zoom 2020年4月20日11:56:50 szm
    onPcMouseWheel_noVR: function (event) {
        var camera = window.that.camera;
        var add = 5;
        if(event.deltaY > 0) add = -5;
        var zoom = parseInt(camera.zoom * 100) + add;
        zoom = THREE.Math.clamp(zoom, window.that.cameraConfig.minZoom, window.that.cameraConfig.maxZoom);
        camera.zoom = zoom / 100;
        // console.log("fov:", camera.fov);
        // console.log("zoom:", camera.zoom);
        camera.updateProjectionMatrix();
    },
    //#endregion

    //#region PC VR模式移动事件
    onPcMouseMove_VR: function (event) {
        event.preventDefault();
        if (mouse.isLock) {
            var info = Util.decryptQuaternion(window.that.camera.quaternion); //解析当前相机四元数
            window.that.currentMoveConfig.lon = THREE.Math.radToDeg(info.theta);
            window.that.currentMoveConfig.lat = THREE.Math.radToDeg(info.delta);

            var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
            var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
            window.that.currentMoveConfig.lon -= movementX * 0.1;
            window.that.currentMoveConfig.lat -= movementY * 0.1;

            window.that.currentMoveConfig.lat = Math.max(-85, Math.min(85, window.that.currentMoveConfig.lat));
            window.that.currentMoveConfig.lon = Math.max(-360, Math.min(360, window.that.currentMoveConfig.lon));
            if (window.that.currentMoveConfig.lon === -360 || window.that.currentMoveConfig.lon === 360) {
                window.that.currentMoveConfig.lon = 0;
            }
            window.that.currentMoveConfig.phi = THREE.Math.degToRad(window.that.currentMoveConfig.lat);
            window.that.currentMoveConfig.theta = THREE.Math.degToRad(window.that.currentMoveConfig.lon);

            var newQuaternion = Util.encryptQuaternion(window.that.currentMoveConfig.theta, window.that.currentMoveConfig.phi);
            window.that.camera.quaternion.copy(newQuaternion);
        }
    },
    //#endregion

    //#region 手机绑定事件
    mobileMove: function () {

        move.moveElement.addEventListener('touchstart', onDocumentTouchStart, false);

        function onDocumentTouchStart(event) {

            event.preventDefault();
            if (event.touches.length === 1) {
                //单点触屏触发
                var touch = event.touches[0];
                move.touchConfig.touchX = touch.screenX;
                move.touchConfig.touchY = touch.screenY;
                move.moveElement.addEventListener('touchmove', onDocumentTouchMove, false);
                move.moveElement.addEventListener('touchend', onDocumentTouchMoveEnd, false);
            } else if (event.touches.length > 1) {
                // alert(123);
                //多点触屏触发
                var pointA = new THREE.Vector2(event.touches[0].pageX, event.touches[0].pageY);
                var pointB = new THREE.Vector2(event.touches[1].pageX, event.touches[1].pageY);
                move.touchDistance = pointA.distanceTo(pointB);
                move.moveElement.addEventListener('touchmove', onTouchUpdateDistance, false);
                move.moveElement.addEventListener('touchend', onTouchUpdateDistanceEnd, false);
            }

        }

        function onDocumentTouchMove(event) {
            event.preventDefault();

            var info = Util.decryptQuaternion(window.that.camera.quaternion); //解析当前相机四元数
            window.that.currentMoveConfig.lon = THREE.Math.radToDeg(info.theta);
            window.that.currentMoveConfig.lat = THREE.Math.radToDeg(info.delta);

            //单点触屏移动为移动
            var touch = event.touches[0];
            //2020年4月20日15:22:26 采用zoom缩放后 增大了拖动速度
            window.that.currentMoveConfig.lon += (touch.screenX - move.touchConfig.touchX) * 0.25;
            window.that.currentMoveConfig.lat += (touch.screenY - move.touchConfig.touchY) * 0.25;

            window.that.currentMoveConfig.lat = Math.max(-85, Math.min(85, window.that.currentMoveConfig.lat));
            window.that.currentMoveConfig.lon = Math.max(-360, Math.min(360, window.that.currentMoveConfig.lon));
            if (window.that.currentMoveConfig.lon === -360 || window.that.currentMoveConfig.lon === 360) {
                window.that.currentMoveConfig.lon = 0;
            }
            window.that.currentMoveConfig.phi = THREE.Math.degToRad(window.that.currentMoveConfig.lat);
            window.that.currentMoveConfig.theta = THREE.Math.degToRad(window.that.currentMoveConfig.lon);

            var newQuaternion = Util.encryptQuaternion(window.that.currentMoveConfig.theta, window.that.currentMoveConfig.phi);
            window.that.camera.quaternion.copy(newQuaternion);

            move.touchConfig.touchX = touch.screenX;
            move.touchConfig.touchY = touch.screenY;

        }

        function onDocumentTouchMoveEnd(event) {
            move.moveElement.removeEventListener('touchmove', onDocumentTouchMove, false);
            move.moveElement.removeEventListener('touchend', onDocumentTouchMoveEnd, false);
        }
        //旧方法 调整FOV实现缩放
        // function onTouchUpdateDistance(event) {
        //     var old = move.touchDistance;
        //     var pointA = new THREE.Vector2(event.touches[0].pageX, event.touches[0].pageY);
        //     var pointB = new THREE.Vector2(event.touches[1].pageX, event.touches[1].pageY);
        //     var distance = pointA.distanceTo(pointB);
        //     // alert('distance:' + distance);
        //     // alert('old distance:' + old);
        //     if (old < distance) {
        //         // 放大
        //         // alert(window.that.camera.fov > window.that.cameraConfig.minFov);
        //         if (window.that.camera.fov > window.that.cameraConfig.minFov)
        //             window.that.camera.fov -= window.that.cameraConfig.fovChangeSpeed;
        //         // alert('big fov:' + window.that.camera.fov);
        //     } else if (old > distance) {
        //         if (window.that.camera.fov < window.that.cameraConfig.maxFov)
        //             window.that.camera.fov += window.that.cameraConfig.fovChangeSpeed;
        //         // alert('small fov:' + window.that.camera.fov);
        //     }
        //     window.that.camera.updateProjectionMatrix();
        //     move.touchDistance = distance;
        // }

        //新方法，通过zoom放大缩小 2020年4月20日17:30:49
        function onTouchUpdateDistance(event) {
            var old = move.touchDistance;
            var pointA = new THREE.Vector2(event.touches[0].pageX, event.touches[0].pageY);
            var pointB = new THREE.Vector2(event.touches[1].pageX, event.touches[1].pageY);
            var distance = pointA.distanceTo(pointB);
            // alert('distance:' + distance);
            // alert('old distance:' + old);
            var camera = window.that.camera;
            var zoom = Math.round(window.that.camera.zoom * 100);
            if (old < distance) {
                // 放大
                if (zoom >= window.that.cameraConfig.minZoom) {
                    zoom += 1;
                    zoom = THREE.Math.clamp(zoom, window.that.cameraConfig.minZoom, window.that.cameraConfig.maxZoom);
                    camera.zoom = zoom / 100;
                } 
            }
            else if (old > distance) {
                if (zoom <= window.that.cameraConfig.maxZoom){
                    zoom -= 1;
                    zoom = THREE.Math.clamp(zoom, window.that.cameraConfig.minZoom, window.that.cameraConfig.maxZoom);
                    camera.zoom = zoom / 100;
                }
            }
            window.that.camera.updateProjectionMatrix();
            move.touchDistance = distance;
        }   

        function onTouchUpdateDistanceEnd(event) {
            move.moveElement.removeEventListener('touchmove', onTouchUpdateDistance, false);
            move.moveElement.removeEventListener('touchend', onTouchUpdateDistanceEnd, false);
        }

    }
    //#endregion
}