import { __awaiter, __extends, __generator } from "tslib";
import { EventEmitter } from 'events';
import { Cartesian3, Cartesian2, Color, 
// @ts-ignore  //@ts-ignore
Batched3DModel3DTileContent, HeightReference, LabelStyle, VerticalOrigin } from '@taotieh/cesium_extend';
/** ****************************************
 *  这是剖切功能的基类, 不要在这个类里写剖切的实现
 *******************************************/
var Clip = /** @class */ (function (_super) {
    __extends(Clip, _super);
    function Clip(viewer, instance) {
        var _this = _super.call(this) || this;
        _this._entities = [];
        _this._target = new Cartesian3();
        /** **********************************************************
         *  基类实现 _onMove 的一些基础功能. 比如在跟踪拾取位置上描点.
         *  当子类没有去重写这部分功能的时候, 默认使用基类所实现的行为.
         *  子类有特殊需求的时候重写 _onMove 属性即可.
         *  子类不需要自行注册事件订阅/取消订阅. _onMove 是属性.
         *************************************************************/
        _this._onMove = function (evt) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
            return [2 /*return*/];
        }); }); };
        _this._onPointerDown = function (evt) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
            return [2 /*return*/];
        }); }); };
        _this._onPointerUp = function (evt) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
            return [2 /*return*/];
        }); }); };
        _this._viewer = viewer;
        _this._instance = instance;
        return _this;
    }
    Clip.prototype._installEvents = function () {
        return __awaiter(this, void 0, void 0, function () {
            var cPo;
            return __generator(this, function (_a) {
                console.log('_installEvents');
                cPo = this._instance.getInstancePointerControl();
                cPo.pickFeat = false;
                cPo.on('move', this._onMove);
                cPo.on('down', this._onPointerDown);
                cPo.on('up', this._onPointerUp);
                return [2 /*return*/];
            });
        });
    };
    Clip.prototype._uninstallEvents = function () {
        return __awaiter(this, void 0, void 0, function () {
            var cPo;
            return __generator(this, function (_a) {
                console.log('_uninstallEvents');
                cPo = this._instance.getInstancePointerControl();
                cPo.pickFeat = true;
                cPo.off('move', this._onMove);
                cPo.off('down', this._onPointerDown);
                cPo.off('up', this._onPointerUp);
                return [2 /*return*/];
            });
        });
    };
    Clip.prototype._selectPoint = function (target) {
        var _this = this;
        // Promise 方式等待用户拾取一个 空间点 对象
        var viewer = this._viewer;
        var scene = viewer.scene;
        return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
            var cPo, selector;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this._instance.getInstancePointerControl()];
                    case 1:
                        cPo = _a.sent();
                        selector = function (evt_down) {
                            return __awaiter(this, void 0, void 0, function () {
                                var _a, x, y, cart2_down;
                                var _this = this;
                                return __generator(this, function (_b) {
                                    _a = evt_down, x = _a.layerX, y = _a.layerY;
                                    cart2_down = new Cartesian2(x, y);
                                    cPo.once('up', function (evt_up) { return __awaiter(_this, void 0, void 0, function () {
                                        var _a, x, y, cart2_up;
                                        return __generator(this, function (_b) {
                                            _a = evt_up, x = _a.layerX, y = _a.layerY;
                                            cart2_up = new Cartesian2(x, y);
                                            if (Cartesian2.equals(cart2_down, cart2_up)) {
                                                scene.pickPosition(cart2_up, target);
                                                cPo.off('down', selector);
                                                resolve(target);
                                            }
                                            return [2 /*return*/];
                                        });
                                    }); });
                                    return [2 /*return*/];
                                });
                            });
                        };
                        cPo.on('down', selector);
                        return [2 /*return*/];
                }
            });
        }); });
    };
    Clip.prototype._selectC3DTileFeature = function (target) {
        if (target === void 0) { target = new Cartesian3(); }
        // Promise 方式等待用户选中一个 要素 对象
        var viewer = this._viewer;
        var scene = viewer.scene;
        return new Promise(function (resolve, reject) {
            viewer.container.addEventListener('click', function (evt) {
                var _a = evt, x = _a.layerX, y = _a.layerY;
                var cart2 = new Cartesian2(x, y);
                scene.pickPosition(cart2, target);
                var festure = scene.pick(cart2);
                if (festure &&
                    festure.content instanceof Batched3DModel3DTileContent) {
                    resolve(festure.content);
                }
                else {
                    reject(null);
                }
            }, { once: true });
        });
    };
    Clip.prototype._rmEnt = function (ent) {
        var _a = this, viewer = _a._viewer, entities = _a._entities;
        ent && viewer.entities.contains(ent) && viewer.entities.remove(ent);
    };
    Clip.prototype._drawPoint = function (cart3, color, size, depthTest) {
        if (depthTest === void 0) { depthTest = false; }
        var _a = this, viewer = _a._viewer, entities = _a._entities;
        var ent = viewer.entities.add({
            position: cart3,
            point: {
                color: color,
                pixelSize: size,
                heightReference: HeightReference.NONE,
                disableDepthTestDistance: 10
            }
        });
        entities.push(ent);
        return ent;
    };
    Clip.prototype._drawLine = function (cart3s, color, width, depthTest) {
        if (depthTest === void 0) { depthTest = false; }
        var _a = this, viewer = _a._viewer, entities = _a._entities;
        var ent = viewer.entities.add({
            polyline: {
                show: true,
                positions: cart3s,
                material: color,
                width: width,
                disableDepthTestDistance: 10
            }
        });
        entities.push(ent);
        return ent;
    };
    Clip.prototype._drawLabel = function (achor, text, offset, bg, size, fg, isScale, depthTest) {
        if (offset === void 0) { offset = new Cartesian2(0, -9); }
        if (size === void 0) { size = 12; }
        if (fg === void 0) { fg = Color.WHITE; }
        if (isScale === void 0) { isScale = false; }
        if (depthTest === void 0) { depthTest = false; }
        var _a = this, viewer = _a._viewer, entities = _a._entities;
        var ent = viewer.entities.add({
            position: achor,
            label: {
                text: text,
                font: size + "pt monospace",
                style: LabelStyle.FILL_AND_OUTLINE,
                outlineWidth: 2,
                verticalOrigin: VerticalOrigin.BOTTOM,
                pixelOffset: offset,
                showBackground: bg && true,
                backgroundColor: bg,
                backgroundPadding: new Cartesian2(7, 5),
                fillColor: fg,
                disableDepthTestDistance: 10
            }
        });
        entities.push(ent);
        return ent;
    };
    Clip.prototype._clearEntity = function (ent) {
        var _a = this, viewer = _a._viewer, entities = _a._entities;
        ent ? viewer.entities.remove(ent) : viewer.entities.removeAll();
        ent ? entities.splice(entities.indexOf(ent), 1) : (entities.length = 0);
    };
    Clip.prototype.start = function (option) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this._installEvents()];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    Clip.prototype.next = function (option) { };
    Clip.prototype.stop = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this._uninstallEvents()];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    return Clip;
}(EventEmitter));
export default Clip;
