import go from "gojs";

/**
 * 初始化故障树图
 * @param data
 * @param domId
 * @return {*|jQuery|HTMLElement}
 */
export function initGoJsFaultTree(data, domId) {

    // Since 2.2 you can also author concise templates with method chaining instead of GraphObject.make
    // For details, see https://gojs.net/latest/intro/buildingObjects.html
    const $ = go.GraphObject.make;  // for conciseness in defining templates
    let diagram =
        $(go.Diagram, domId,
            {
                allowCopy: false,
                allowDelete: false,
                "draggingTool.dragsTree": true,
                layout: $(
                    go.TreeLayout,
                    {
                        angle: 90,
                        nodeSpacing: 10,
                        layerSpacing: 40,
                        layerStyle: go.TreeLayout.LayerUniform
                    }
                ),
                "undoManager.isEnabled": true,
                initialContentAlignment: go.Spot.Center,
                contentAlignment: go.Spot.Center,
            });

    //数据绑定
    const nodeDataArray = [];
    orgnizationData(data, nodeDataArray);
    // console.log(JSON.stringify(nodeDataArray))
    diagram.model = new go.TreeModel(nodeDataArray);

    diagram.nodeTemplate =
        $(
            go.Node, "Auto",
            {
                selectionAdorned: false,  // don't bother with any selection adornment
                //选择改变事件的回调函数
                selectionChanged: (node) => {
                    let icon = node.findObject("Icon");
                    if (icon !== null) {
                        //判断node的选择状态
                        if (node.isSelected)
                            icon.fill = "#9bf2eb";
                        else
                            icon.fill = "#fff";
                    }
                }
            },  // 在 Part.isSelected 改变后调用
            // new go.Binding("location", "loc", go.Point.parse),
            // new go.Binding("text"),
            $(go.Shape, "RoundedRectangle",
                {
                    // fill: "lightgray",
                    name: "Icon",
                    fill: "#fff",
                    // stroke: null,
                    strokeWidth: 1,
                    stretch: go.GraphObject.Fill,
                    alignment: go.Spot.Center

                },
                new go.Binding("fill", "#90CAF9")
            ),
            $(go.TextBlock,
                {
                    font: "14px sans-serif", // 字体
                    // font: "300 12px Droid Serif, sans-serif",
                    textAlign: "center",
                    margin: 10,
                    maxSize: new go.Size(80, NaN)
                },
                new go.Binding("text")
            )
        );

    diagram.linkTemplate =
        $(go.Link,  // the whole link panel
            {
                routing: go.Link.Orthogonal,
                corner: 5,
                selectable: false
            },
            $(go.Shape, {
                strokeWidth: 1,
                stroke: '#424242'
            })
        );
    return diagram;
}

//递归遍历 重新组装
function orgnizationData(obj, arr) {
    const key = arr.length;
    obj.key = key;
    arr.push(obj);
    const children = obj.children;
    if (children) {
        for (let i = 0; i < children.length; i++) {
            const o = children[i];
            o.parent = key;
            orgnizationData(o, arr);
        }
    }
}

/**
 * 更新故障树
 * @param diagram
 * @param data
 */
export function updateGoJsFaultTree(diagram, data) {
    diagram.model = go.Model.fromJson({});
    //数据绑定
    const nodeDataArray = [];
    orgnizationData(data, nodeDataArray);
    diagram.model = new go.TreeModel(nodeDataArray);
}



