import { __awaiter, __generator } from "tslib";
import Instance from '../../core/Instance';
import LineWithPoint from './Model/LineWithPoint';
import esriLoader from '../../util/arcgis';
import shortid from 'shortid';
import pointMap from './Data/pointMap';
import * as Cesium from '@msign/angularis';
import { Cartesian2, Cartesian3, Color, ColorMaterialProperty, CallbackProperty } from '@msign/angularis';
function computeCircle(radius) {
    var positions = [];
    for (var i = 0; i < 360; i++) {
        var radians = Cesium.Math.toRadians(i);
        positions.push(new Cartesian2(radius * Math.cos(radians), radius * Math.sin(radians)));
    }
    return positions;
}
function computeRect(width, hight) {
    var positions = [
        new Cartesian2(-width / 2, -hight / 2),
        new Cartesian2(width / 2, -hight / 2),
        new Cartesian2(width / 2, hight / 2),
        new Cartesian2(-width / 2, hight / 2)
    ];
    return positions;
}
var BizPipenetControl = /** @class */ (function () {
    function BizPipenetControl() {
    }
    BizPipenetControl._createCallbackColor = function (lineVo) {
        var color = Color.fromCssColorString(lineVo.color) || Cesium.Color.WHITE;
        return function (time, result) {
            // 高亮
            if (BizPipenetControl._highlights.get(lineVo.id)) {
                return BizPipenetControl._highlightColor;
            }
            else {
                return color;
            }
        };
    };
    BizPipenetControl._addClipAreaWithLine = function (lines, distance, unit, depth) {
        if (lines === void 0) { lines = []; }
        if (distance === void 0) { distance = 1; }
        if (unit === void 0) { unit = 'meters'; }
        if (depth === void 0) { depth = 10; }
        return __awaiter(this, void 0, void 0, function () {
            var cSc, scene, viewer, _a, geometryEngineAsync, Polyline, polines, bufPolygon, rings;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, Instance.getInstanceSceneControl()];
                    case 1:
                        cSc = _b.sent();
                        scene = cSc.scene, viewer = cSc.viewer;
                        return [4 /*yield*/, esriLoader.loadModules([
                                'esri/geometry/geometryEngineAsync',
                                'esri/geometry/Polyline'
                            ])];
                    case 2:
                        _a = _b.sent(), geometryEngineAsync = _a[0], Polyline = _a[1];
                        polines = lines.map(function (line) {
                            return new Polyline({
                                paths: [
                                    [line.startLng, line.startLat],
                                    [line.endLng, line.endLat]
                                ]
                            });
                        });
                        return [4 /*yield*/, geometryEngineAsync.geodesicBuffer(polines, polines.map(function (_) {
                                return distance;
                            }), unit, true)];
                    case 3:
                        bufPolygon = _b.sent();
                        // rings 是一个数组, 数组项即区域单元
                        console.log('生成缓冲区', bufPolygon);
                        debugger;
                        rings = bufPolygon[0].rings;
                        scene.globe.clippingPlans = new Cesium.ClippingPlaneCollection({
                            planes: [],
                            edgeColor: Cesium.Color.RED,
                            edgeWidth: 1
                        });
                        rings.forEach(function (area) {
                            var _a;
                            // 绘制开挖区域面
                            var entity = viewer.entities.add({
                                polygon: {
                                    clampToGround: true,
                                    hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights((_a = []).concat.apply(_a, area)),
                                    // height: 0 as any,
                                    material: Cesium.Color.ALICEBLUE.withAlpha(0.2),
                                    outline: true,
                                    outlineColor: Cesium.Color.BLACK
                                }
                            });
                            // 构造开挖区
                            // for (let i = 0; i < area.length; i++) {
                            //   const sp = area[i];
                            //   const ep = area[(i + 1) % area.length];
                            //   const n =
                            // }
                            // const clipPlane = [];
                            // scene.globe.clippingPlans.add(clipPlane);
                        });
                        return [2 /*return*/];
                }
            });
        });
    };
    BizPipenetControl._addClipAreaWithPoint = function (lines) {
        if (lines === void 0) { lines = []; }
        return __awaiter(this, void 0, void 0, function () {
            var _a, geometryEngine, Graphic;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, esriLoader.loadModules([
                            'esri/geometry/geometryEngine',
                            'esri/Graphic'
                        ])];
                    case 1:
                        _a = _b.sent(), geometryEngine = _a[0], Graphic = _a[1];
                        return [2 /*return*/];
                }
            });
        });
    };
    BizPipenetControl._addLine = function (lineVo) {
        return __awaiter(this, void 0, void 0, function () {
            var cSc, scene, viewer;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, Instance.getInstanceSceneControl()];
                    case 1:
                        cSc = _a.sent();
                        scene = cSc.scene, viewer = cSc.viewer;
                        return [2 /*return*/, viewer.entities.add({
                                name: lineVo.id,
                                polyline: {
                                    positions: Cesium.Cartesian3.fromDegreesArrayHeights([
                                        lineVo.startLng,
                                        lineVo.startLat,
                                        lineVo.startH,
                                        lineVo.endLng,
                                        lineVo.endLat,
                                        lineVo.endH
                                    ]),
                                    material: Cesium.Color.fromRandom({ alpha: 1.0 })
                                }
                            })];
                }
            });
        });
    };
    BizPipenetControl._addLine3D = function (lineVo) {
        return __awaiter(this, void 0, void 0, function () {
            var cSc, scene, viewer, str, _a, w, h, r, shape;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, Instance.getInstanceSceneControl()];
                    case 1:
                        cSc = _b.sent();
                        scene = cSc.scene, viewer = cSc.viewer;
                        str = lineVo.pdiameter;
                        _a = str.split('*').map(function (n) {
                            return Number.parseFloat(n);
                        }), w = _a[0], h = _a[1];
                        r = Number.parseFloat(str);
                        if (w && h) {
                            // 矩形
                            shape = computeRect(w / 1000, h / 1000);
                        }
                        else if (r) {
                            shape = computeCircle(r / 1000);
                        }
                        else {
                            shape = computeCircle(0.1);
                        }
                        return [2 /*return*/, viewer.entities.add({
                                id: lineVo.id,
                                polylineVolume: {
                                    positions: Cesium.Cartesian3.fromDegreesArrayHeights([
                                        lineVo.startLng,
                                        lineVo.startLat,
                                        lineVo.startH,
                                        lineVo.endLng,
                                        lineVo.endLat,
                                        lineVo.endH
                                    ]),
                                    shape: shape,
                                    // material: Color.fromCssColorString(lineVo.color) || Cesium.Color.RED
                                    material: new ColorMaterialProperty(new CallbackProperty(BizPipenetControl._createCallbackColor(lineVo), false))
                                }
                            })];
                }
            });
        });
    };
    BizPipenetControl._addPoint = function (pointVo) {
        return __awaiter(this, void 0, void 0, function () {
            var cSc, scene, viewer;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, Instance.getInstanceSceneControl()];
                    case 1:
                        cSc = _a.sent();
                        scene = cSc.scene, viewer = cSc.viewer;
                        return [2 /*return*/, viewer.entities.add({
                                name: pointVo.id,
                                position: Cesium.Cartesian3.fromDegreesArrayHeights([
                                    pointVo.lng,
                                    pointVo.lat,
                                    pointVo.h
                                ]),
                                point: {
                                    color: Cesium.Color.RED,
                                    pixelSize: 5,
                                    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
                                }
                            })];
                }
            });
        });
    };
    BizPipenetControl._addPoint3D = function (pointVo) {
        return __awaiter(this, void 0, void 0, function () {
            var cSc, scene, viewer, position, heading, pitch, roll, hpr, orientation, id;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, Instance.getInstanceSceneControl()];
                    case 1:
                        cSc = _a.sent();
                        scene = cSc.scene, viewer = cSc.viewer;
                        position = Cartesian3.fromDegrees(pointVo.lng, pointVo.lat, pointVo.h);
                        heading = Cesium.Math.toRadians(135);
                        pitch = 0;
                        roll = 0;
                        hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
                        orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);
                        id = shortid();
                        if (pointMap[pointVo.appendant]) {
                            return [2 /*return*/, (pointVo.appendant &&
                                    viewer.entities.add({
                                        id: pointVo.id,
                                        position: position,
                                        orientation: orientation,
                                        model: {
                                            uri: pointMap[pointVo.appendant],
                                            scale: 1
                                        }
                                    }))];
                        }
                        else {
                            // 未定义
                            return [2 /*return*/, (pointVo.appendant &&
                                    viewer.entities.add({
                                        id: pointVo.id,
                                        position: position,
                                        orientation: orientation,
                                        box: {
                                            dimensions: new Cesium.Cartesian3(1.0, 1.0, 1.0),
                                            material: Cesium.Color.WHITE.withAlpha(0.1),
                                            outline: true,
                                            outlineColor: Cesium.Color.YELLOW.withAlpha(0.5)
                                        }
                                    }))];
                        }
                        return [2 /*return*/];
                }
            });
        });
    };
    BizPipenetControl.addPipe = function (lwpJson) {
        return __awaiter(this, void 0, void 0, function () {
            var cSc, scene, viewer, lwp, ents, lineVos, pointVos;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, Instance.getInstanceSceneControl()];
                    case 1:
                        cSc = _a.sent();
                        scene = cSc.scene, viewer = cSc.viewer;
                        lwp = LineWithPoint.fromJson(lwpJson);
                        ents = [];
                        lineVos = lwp.lineVos;
                        ents.push.apply(ents, lineVos.map(this._addLine.bind(this)));
                        pointVos = lwp.pointVos;
                        ents.push.apply(ents, pointVos.map(this._addPoint.bind(this)));
                        ents && ents[0] && viewer.flyTo(ents[0]);
                        return [2 /*return*/];
                }
            });
        });
    };
    BizPipenetControl.addPipe3D = function (lwpJson) {
        return __awaiter(this, void 0, void 0, function () {
            var cSc, scene, viewer, lwp, ents, lineVos, pointVos;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, Instance.getInstanceSceneControl()];
                    case 1:
                        cSc = _a.sent();
                        scene = cSc.scene, viewer = cSc.viewer;
                        lwp = LineWithPoint.fromJson(lwpJson);
                        ents = [];
                        lineVos = lwp.lineVos;
                        ents.push.apply(ents, lineVos.map(this._addLine3D.bind(this)));
                        pointVos = lwp.pointVos;
                        ents.push.apply(ents, pointVos.map(this._addPoint3D.bind(this)));
                        // ents.push(
                        //   ...(await Promise.all(pointVos.map(this._addPoint3D.bind(this))))
                        // );
                        // ents.push(this._addPoint3D(pointVos[0]));
                        // ents && ents[ents.length - 1] && viewer.flyTo(ents[ents.length - 1]);
                        return [2 /*return*/, ents];
                }
            });
        });
    };
    BizPipenetControl.unhilight = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                if (!id) {
                    BizPipenetControl._highlights.clear();
                }
                else {
                    BizPipenetControl._highlights.delete(id);
                }
                return [2 /*return*/];
            });
        });
    };
    BizPipenetControl.highlight = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                BizPipenetControl._highlights.set(id, true);
                return [2 /*return*/];
            });
        });
    };
    BizPipenetControl._highlights = new Map();
    BizPipenetControl._highlightColor = Color.YELLOW.clone();
    return BizPipenetControl;
}());
export default BizPipenetControl;
