import { __awaiter, __extends, __generator } from "tslib";
import { throttle } from 'lodash';
import { MOUSE } from 'three';
import { EventEmitter } from 'events';
import * as Cesium from '@taotieh/cesium_extend';
var FirstMode = /** @class */ (function (_super) {
    __extends(FirstMode, _super);
    function FirstMode(conPointer, viewer) {
        var _this = _super.call(this) || this;
        _this._isActive = false;
        _this._isUpdating = false;
        _this._keyDownMap = new Map();
        _this.pointerLock = false;
        _this._onLockError = function (evt) {
            // console.log('onLockError', evt);
            console.log('光标锁定仅能由用户鼠标操作触发, 请点击一次屏幕.');
            document.addEventListener('click', _this._lockPointer.bind(_this), {
                once: true
            });
            document.addEventListener('pointerlockchange', _this._onLockOn, {
                once: true
            });
        };
        _this._onLockOn = function (evt) {
            document.addEventListener('pointerlockchange', _this._onLockOff, {
                once: true
            });
            // console.log('_onLockOn');
        };
        _this._onLockOff = function (evt) {
            // console.log('_onLockOff');
            _this.emit('exit', evt);
            _this._uninstallEvents();
        };
        _this._onLockChange = function (evt) {
            _this.pointerLock = !_this.pointerLock;
            if (_this.pointerLock)
                return;
            _this.emit('exit', evt);
            _this._uninstallEvents();
        };
        _this._onKeyDown = function (evt) {
            var KM = _this._keyDownMap;
            KM.set(evt.key, true);
            if (!_this._isUpdating) {
                // 没有正在运行的更新任务, 才启动. 防止重复启动
                _this._update();
            }
        };
        _this._onKeyUp = function (evt) {
            var KM = _this._keyDownMap;
            KM.set(evt.key, false);
        };
        _this._update = function () {
            var _a = _this, KM = _a._keyDownMap, viewer = _a._viewer;
            var camera = viewer.camera;
            var position = camera.position, direction = camera.direction, up = camera.up;
            var speed = 1;
            if (KM.get('w') ||
                KM.get('a') ||
                KM.get('s') ||
                KM.get('d') ||
                KM.get('q') ||
                KM.get('e')) {
                // 当前有键盘按下时, 申请下一帧
                requestAnimationFrame(_this._update);
                _this._isUpdating = true;
            }
            else {
                _this._isUpdating = false;
            }
            var step = Cesium.Cartesian3.ZERO.clone();
            var cross = Cesium.Cartesian3.cross(direction, up, Cesium.Cartesian3.ZERO.clone());
            if (KM.get('w')) {
                var canvas = viewer.canvas;
                var centerPoint2 = new Cesium.Cartesian2(canvas.width / 2, canvas.height / 2);
                var centerPoint3 = viewer.scene.pickPosition(centerPoint2);
                Cesium.Cartesian3.add(step, direction, step);
                if (centerPoint3) {
                    var camera_1 = viewer.camera;
                    var position_1 = camera_1.position;
                    speed = Cesium.Cartesian3.distance(centerPoint3, position_1) / 30;
                }
            }
            // KM.get('w') && Cesium.Cartesian3.add(step, direction, step) && ());
            KM.get('s') && Cesium.Cartesian3.subtract(step, direction, step);
            KM.get('a') && Cesium.Cartesian3.subtract(step, cross, step);
            KM.get('d') && Cesium.Cartesian3.add(step, cross, step);
            KM.get('q') && Cesium.Cartesian3.subtract(step, up, step);
            KM.get('e') && Cesium.Cartesian3.add(step, up, step);
            camera.move(step, speed);
        };
        _this._onDown = function (evt) {
            // console.log('down', evt);
            var button = evt.button;
            switch (button) {
                case MOUSE.LEFT:
                    break;
                case MOUSE.MIDDLE:
                    break;
                case MOUSE.RIGHT:
                    break;
            }
        };
        _this._onUp = function (evt) {
            // console.log('up', evt);
        };
        _this._onMove = throttle(function (evt) {
            if (!document.pointerLockElement)
                return;
            var x = evt.movementX, y = evt.movementY;
            // console.log({ x, y });
            var viewer = _this._viewer;
            var camera = viewer.camera;
            camera.lookRight((x * Math.PI) / 1440);
            camera.lookDown((y * Math.PI) / 1440);
            Cesium.Cartesian3.normalize(camera.position, camera.up);
        }, 10);
        _this._onWheel = function (evt) {
            // console.log('wheel', evt);
        };
        _this._viewer = viewer;
        _this._conPointer = conPointer;
        return _this;
    }
    // private _ptrLine:Cesium.Entity=;
    FirstMode.prototype._lockPointer = function () {
        return __awaiter(this, void 0, void 0, function () {
            var viewer, dom, res;
            return __generator(this, function (_a) {
                viewer = this._viewer;
                dom = viewer.container;
                try {
                    res = dom.requestPointerLock();
                }
                catch (e) { }
                return [2 /*return*/];
            });
        });
    };
    FirstMode.prototype._unlockPointer = function () {
    };
    FirstMode.prototype._installEvents = function () {
        var viewer = this._viewer;
        document.addEventListener('pointerlockerror', this._onLockError);
        document.addEventListener('pointerlockchange', this._onLockChange);
        document.addEventListener('pointermove', this._onMove);
        document.addEventListener('keydown', this._onKeyDown);
        document.addEventListener('keyup', this._onKeyUp);
    };
    FirstMode.prototype._uninstallEvents = function () {
        var viewer = this._viewer;
        document.removeEventListener('pointerlockerror', this._onLockError);
        document.removeEventListener('pointerlockchange', this._onLockChange);
        document.removeEventListener('pointermove', this._onMove);
        document.removeEventListener('keydown', this._onKeyDown);
        document.removeEventListener('keyup', this._onKeyUp);
    };
    Object.defineProperty(FirstMode.prototype, "isActive", {
        get: function () {
            return this._isActive;
        },
        set: function (val) {
            var conP = this._conPointer;
            if (val === this._isActive)
                return;
            if (val) {
                this._lockPointer();
                // 安装的时候先注册事件
                this._installEvents();
            }
            else {
                // 卸载的时候后卸载事件
                this._unlockPointer();
                this._uninstallEvents();
            }
            this._isActive = val;
        },
        enumerable: true,
        configurable: true
    });
    return FirstMode;
}(EventEmitter));
export default FirstMode;
