package cn.stylefeng.workflow.design.modular.factory;

import cn.hutool.core.collection.ListUtil;
import cn.stylefeng.workflow.api.entity.design.Definition;
import cn.stylefeng.workflow.api.entity.design.Node;
import cn.stylefeng.workflow.api.entity.design.NodeEdge;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

import java.util.ArrayList;
import java.util.List;

public class GraphFactory {

    public static final List<String> CUSTOM_NODE_TYPE_NAME_X6 = ListUtil.list(true, "custom-vue-shape", "custom-polygon", "custom-circle");

    public static final String EDGE_TYPE_NAME_X6 = "edge";

    public static List<Node> parseX6NodeList(Definition definition, JSONObject x6Object) {

        List<Node> parseResult = new ArrayList<>();

        JSONArray cells = x6Object.getJSONArray("cells");

        if (cells.isEmpty()) {
            return parseResult;
        }

        for (int i = 0; i < cells.size(); i++) {
            JSONObject cellItem = cells.getJSONObject(i);
            String shape = cellItem.getString("shape");
            if (CUSTOM_NODE_TYPE_NAME_X6.contains(shape)) {

                JSONObject cellData = cellItem.getJSONObject("data");
                if (cellData == null) {
                    continue;
                }

                JSONObject nodeConfigJson = cellData.getJSONObject("data");
                if (nodeConfigJson == null) {
                    continue;
                }

                Node node = nodeConfigJson.toJavaObject(Node.class);

                node.setModelId(definition.getModelId());
                node.setDefinitionId(definition.getDefinitionId());

                parseResult.add(node);
            }
        }

        return parseResult;
    }

    public static List<NodeEdge> parseX6EdgeList(Definition definition, JSONObject x6Object) {

        List<NodeEdge> parseResult = new ArrayList<>();

        JSONArray cells = x6Object.getJSONArray("cells");

        if (cells.isEmpty()) {
            return parseResult;
        }

        for (int i = 0; i < cells.size(); i++) {
            JSONObject jsonObject = cells.getJSONObject(i);
            String shape = jsonObject.getString("shape");
            if (EDGE_TYPE_NAME_X6.equals(shape)) {

                NodeEdge edge = new NodeEdge();

                long newId = IdWorker.getId();
                edge.setEdgeId(newId);

                jsonObject.put("id", String.valueOf(newId));

                JSONObject source = jsonObject.getJSONObject("source");
                JSONObject target = jsonObject.getJSONObject("target");

                Long sourceId = source.getLong("cell");
                Long targetId = target.getLong("cell");

                edge.setSourceNodeId(sourceId);
                edge.setTargetNodeId(targetId);

                edge.setModelId(definition.getModelId());
                edge.setDefinitionId(definition.getDefinitionId());

                parseResult.add(edge);
            }
        }

        return parseResult;
    }

}
