import { __awaiter, __extends, __generator } from "tslib";
import { TIMESPAN_SOON } from '../core/Constant';
import { Cartesian3, Matrix4 } from '@taotieh/cesium_extend';
import Control from './_Control';
var CameraControl = /** @class */ (function (_super) {
    __extends(CameraControl, _super);
    function CameraControl(inst, viewer) {
        var _this = _super.call(this, inst) || this;
        _this._viewer = viewer;
        _this._camera = viewer.camera;
        _this._camera.frustum.near = 0.01;
        return _this;
    }
    /** ************************************
     *  飞行到指定目标点
     ***************************************/
    CameraControl.prototype.flyto = function (target, direction, up, timespan) {
        if (timespan === void 0) { timespan = TIMESPAN_SOON; }
        return __awaiter(this, void 0, void 0, function () {
            var cam, _position, _direction, _up, cAn;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        cam = this._camera;
                        _position = cam.position;
                        _direction = cam.direction;
                        _up = cam.up;
                        return [4 /*yield*/, this._instance.getInstanceAnimaControl()];
                    case 1:
                        cAn = _a.sent();
                        return [4 /*yield*/, Promise.all([
                                cAn.trans(_position, target, timespan),
                                direction && cAn.trans(_direction, direction, timespan),
                                up && cAn.trans(_up, up, timespan)
                            ])];
                    case 2:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    CameraControl.prototype.flyByBoundBox = function (_a, tile, scalar) {
        var MaxX = _a[0], MaxY = _a[1], MaxZ = _a[2], MinX = _a[3], MinY = _a[4], MinZ = _a[5];
        if (scalar === void 0) { scalar = 1; }
        return __awaiter(this, void 0, void 0, function () {
            var lower, upper, nLP, position, nnLP, up;
            return __generator(this, function (_b) {
                lower = new Cartesian3(MaxX, MaxY, MaxZ);
                upper = new Cartesian3(MinX, MinY, MinZ);
                upper = this.transPositionWithMatrix(tile.root.transform, upper);
                lower = this.transPositionWithMatrix(tile.root.transform, lower);
                nLP = new Cartesian3();
                position = new Cartesian3(), nnLP = new Cartesian3();
                up = new Cartesian3();
                Cartesian3.subtract(upper, lower, nLP);
                Cartesian3.multiplyByScalar(nLP, scalar, nLP);
                Cartesian3.add(upper, nLP, position);
                Cartesian3.normalize(nLP, nnLP);
                Cartesian3.normalize(position, up);
                Cartesian3.negate(nnLP, nnLP);
                console.log({ position: position, nnLP: nnLP, up: up }, 'flyByBoundBox');
                this.zoomto(position, nnLP, up, 1);
                return [2 /*return*/];
            });
        });
    };
    CameraControl.prototype.zoomto = function (destination, direction, up, timespan) {
        if (timespan === void 0) { timespan = 1; }
        return __awaiter(this, void 0, void 0, function () {
            var cam;
            return __generator(this, function (_a) {
                cam = this._camera;
                cam.flyTo({
                    destination: destination,
                    orientation: {
                        direction: direction,
                        up: up
                    },
                    duration: timespan
                });
                return [2 /*return*/];
            });
        });
    };
    CameraControl.prototype.transPositionWithMatrix = function (matrixArray, point) {
        var matrix = Matrix4.fromArray(matrixArray);
        return Matrix4.multiplyByPoint(matrix, point, new Cartesian3());
    };
    CameraControl.prototype.transBoundBoxToPosition = function (tile, _a) {
        var MaxX = _a[0], MaxY = _a[1], MaxZ = _a[2], MinX = _a[3], MinY = _a[4], MinZ = _a[5];
        var lower = new Cartesian3(MaxX, MaxY, MaxZ);
        var upper = new Cartesian3(MinX, MinY, MinZ);
        var mid = new Cartesian3();
        mid = Cartesian3.midpoint(lower, upper, mid);
        mid = this.transPositionWithMatrix(tile.root.transform, mid);
        return mid;
    };
    CameraControl.prototype.shaking = function () {
        return __awaiter(this, void 0, void 0, function () {
            var cam, position, p0, ps, cAn, i, p;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        cam = this._camera;
                        position = cam.position;
                        p0 = position.clone();
                        ps = [];
                        cAn = this._instance.getInstanceAnimaControl();
                        i = 0;
                        _a.label = 1;
                    case 1:
                        if (!(i < 5)) return [3 /*break*/, 4];
                        p = p0.clone();
                        p.x += Math.random() > 0.5 ? 0.2 : -0.2;
                        p.y += Math.random() > 0.5 ? 0.2 : -0.2;
                        p.z += Math.random() > 0.5 ? 0.2 : -0.2;
                        return [4 /*yield*/, cAn.trans(position, p, 100)];
                    case 2:
                        _a.sent();
                        _a.label = 3;
                    case 3:
                        i++;
                        return [3 /*break*/, 1];
                    case 4: return [4 /*yield*/, cAn.trans(position, p0, 100)];
                    case 5:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    return CameraControl;
}(Control));
export default CameraControl;
