import { __awaiter, __extends, __generator } from "tslib";
import { throttle } from 'lodash';
import { Cartesian3 } from '@taotieh/cesium_extend';
import { TIMESPAN_INSTANT } from '../../../core/Constant';
import ClipWithPlane from './ClipWithPlane';
var State;
(function (State) {
    State[State["READY"] = 0] = "READY";
    State[State["POINT_ORIGIN_SELECTED"] = 1] = "POINT_ORIGIN_SELECTED";
    State[State["POINT_ORIGIN_CHANGED"] = 2] = "POINT_ORIGIN_CHANGED";
    State[State["DONE"] = 3] = "DONE";
})(State || (State = {}));
/**
 * 使用强制绑定为竖直方向的面刨切
 *
 * 类提供在水平方向改变面朝向的能力
 */
var ClipWithPath = /** @class */ (function (_super) {
    __extends(ClipWithPath, _super);
    function ClipWithPath(viewer, instance) {
        var _this = 
        /** **********************************************************
         *  父类构造需要指定原点和法线, 这里的值无意义. 等待后续鼠标动作赋值
         *************************************************************/
        _super.call(this, viewer, instance, Cartesian3.ZERO.clone(), Cartesian3.UNIT_Y.clone()) || this;
        _this._onMove = throttle(function (evt) { return __awaiter(_this, void 0, void 0, function () {
            var currentPoint, x, y, _a, lastMovePoint, plane, deduceX, deduceY, distance, currentDistance;
            return __generator(this, function (_b) {
                currentPoint = this._currentPoint;
                if (this._isPanning) {
                    x = evt.layerX, y = evt.layerY;
                    _a = this, lastMovePoint = _a._lastMovePoint, plane = _a._selectedPlane;
                    deduceX = lastMovePoint.x - x;
                    deduceY = lastMovePoint.y - y;
                    distance = (Math.abs(deduceY) > Math.abs(deduceX) ? deduceY : deduceX) / 10;
                    this._updateCutingPlaneWithOrigin({ distance: distance, id: plane.id.id });
                    currentDistance = _super.prototype.getDistance.call(this);
                    this._lastMovePoint = { x: x, y: y };
                    console.log({ currentDistance: currentDistance[plane.id.id], distance: currentPoint.distance });
                    if (Math.abs(currentDistance[plane.id.id]) > currentPoint.distance) {
                        this._doChangePlane(currentDistance[plane.id.id]);
                        // this._isPanning = false;
                    }
                }
                return [2 /*return*/];
            });
        }); }, TIMESPAN_INSTANT);
        _this._toReset();
        return _this;
    }
    ClipWithPath.prototype._createPlane = function (normal, origin) {
        return __awaiter(this, void 0, void 0, function () {
            var entity;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        console.log(normal, origin);
                        return [4 /*yield*/, _super.prototype._toReset.call(this)];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, this._createCuttingPlaneWithOrigin(normal, origin)];
                    case 2:
                        entity = _a.sent();
                        return [4 /*yield*/, this.createRotateHandler(normal, origin, 
                            // @ts-ignore
                            entity)];
                    case 3:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    ClipWithPath.prototype._doChangePlane = function (flag) {
        console.log(flag);
        var _a = this, points = _a._points, currentPoint = _a._currentPoint;
        var nextPoint = {
            position: new Cartesian3(),
            normal: new Cartesian3()
        };
        if (flag > 0) {
            if (currentPoint.index + 1 > points.length) {
                return;
            }
            else {
                nextPoint = points[currentPoint.index + 1];
            }
        }
        else {
            if (currentPoint.index === 0) {
                return;
            }
            else {
                nextPoint = points[currentPoint.index - 1];
            }
        }
        if (!nextPoint)
            return;
        var position = nextPoint.position, normal = nextPoint.normal;
        this._isPanning = false;
        this._currentPoint = nextPoint;
        (position && normal) && this._createPlane(new Cartesian3(normal.x, normal.y, normal.z), position);
    };
    ClipWithPath.prototype._calcPointsDistance = function () {
        var _this = this;
        var option = this._option;
        var points = option.points;
        var midPoints = [];
        points && points.forEach && points.forEach(function (item, index) {
            var point = { position: new Cartesian3(item.position.x, item.position.y, item.position.z) };
            var isLastPoint = index + 1 === points.length;
            if (!isLastPoint) {
                var normal = new Cartesian3();
                // 计算两个点的半径
                var end = _this.transPositionWithMatrix(points[index + 1].position);
                var start = _this.transPositionWithMatrix(point.position);
                // const end = points[index + 1].position;
                // const start = point.position;
                console.log({ end: end, start: start });
                var distance = (Cartesian3.distance(start, end)) / 2;
                var position = new Cartesian3();
                // 计算两个点中点
                // @ts-ignore
                Cartesian3.midpoint(points[index + 1].position, point.position, position);
                // 计算两个点的方向
                Cartesian3.subtract(start, end, normal);
                midPoints.push({
                    index: index,
                    normal: normal,
                    distance: distance,
                    position: position
                });
            }
        });
        var firstPoint = midPoints[0];
        this._createPlane(firstPoint.normal, firstPoint.position);
        this._currentPoint = firstPoint;
        this._points = midPoints;
    };
    Object.defineProperty(ClipWithPath.prototype, "option", {
        set: function (option) {
            this._option = option;
        },
        enumerable: true,
        configurable: true
    });
    ClipWithPath.prototype._toReset = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this._state = State.READY;
                        return [4 /*yield*/, _super.prototype._toReset.call(this)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    ClipWithPath.prototype.start = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                _super.prototype.start.call(this);
                this._toReset();
                this.next();
                return [2 /*return*/];
            });
        });
    };
    ClipWithPath.prototype.next = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (this._state) {
                    case State.READY:
                        this._calcPointsDistance();
                        break;
                    default:
                        this._toReset();
                }
                return [2 /*return*/];
            });
        });
    };
    ClipWithPath.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._toReset()];
                    case 1:
                        _a.sent();
                        _super.prototype.stop.call(this);
                        return [2 /*return*/];
                }
            });
        });
    };
    return ClipWithPath;
}(ClipWithPlane));
export default ClipWithPath;
