package com.horizon.extend.workflow.util;

import com.alibaba.fastjson.JSONObject;
import com.horizon.wf.definition.FlowDBJsonUtil;
import com.horizon.wf.definition.json.JSONFlowInfo;
import com.horizon.wf.definition.pub.IFlowNode;
import com.horizon.wf.definition.pub.node.INodeControl;
import edu.emory.mathcs.backport.java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class FlowJsonUtil {
    private final static Logger logger = LoggerFactory.getLogger(FlowJsonUtil.class);

    public static List<IFlowNode> getFlowJsonByWorkId(String workId, String identifier) {
        JSONObject dbFlow = FlowDBJsonUtil.getFlowChartData(5, workId, identifier, identifier);
        if (dbFlow == null) {
            logger.error("workId: {} ，对应的flow不存在", workId);
            return null;
        }
        JSONObject mapData = (JSONObject) dbFlow.get("mapData");
        JSONObject flowInfo = (JSONObject) dbFlow.get("flowInfo");
        JSONFlowInfo jsonFlow = new JSONFlowInfo(dbFlow); // 建模json转对象：
        List<IFlowNode> flowNodes = jsonFlow.getAllNodes(); //所有节点信息
        return flowNodes;
    }

    public static boolean getIsDefUser(String nodeId, List<IFlowNode> flowJsonNodes) {
        boolean isDefUser = false;
        for (IFlowNode node : flowJsonNodes) {
            if (!node.getNodeid().equals(nodeId)) continue;
            String controlAttribute = node.getParticipantsUser("Author").getControlAttribute();
            String[] ontrolAttributes = controlAttribute.split(";");
            List<String> controlAttributeList = Arrays.asList(ontrolAttributes);
            if (controlAttributeList.contains("ManualSelect")) {
                isDefUser = true;
            }
            break;
        }
        return isDefUser;
    }
}
