package com.zt.plat.module.qms.core.liteflow.parser;

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

import org.apache.commons.lang3.StringUtils;

import com.yomahub.liteflow.parser.el.ClassXmlFlowELParser;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import com.zt.plat.framework.tenant.core.util.TenantUtils;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSimpleFlowCodeDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSimpleFlowRuleDO;
import com.zt.plat.module.qms.business.config.service.ConfigSimpleFlowCodeService;
import com.zt.plat.module.qms.business.config.service.ConfigSimpleFlowRuleService;
import com.zt.plat.module.qms.core.liteflow.constant.QmsXmlFlowElParserConstant;


public class QmsXmlFlowELParser extends ClassXmlFlowELParser {
    
    private ConfigSimpleFlowRuleService configSimpleFlowRuleService;
    
    private ConfigSimpleFlowCodeService configSimpleFlowCodeService;
    
    public QmsXmlFlowELParser(ConfigSimpleFlowRuleService configSimpleFlowRuleService, ConfigSimpleFlowCodeService configSimpleFlowCodeService) {
        this.configSimpleFlowRuleService = configSimpleFlowRuleService;
        this.configSimpleFlowCodeService = configSimpleFlowCodeService;
    }

    @Override
    public String parseCustom() {
    	String content = TenantUtils.executeIgnore(() -> {
    		List<ConfigSimpleFlowCodeDO> liteflowScriptList = configSimpleFlowCodeService.getConfigSimpleFlowCodeList();
            List<String> scriptResult = new ArrayList<>();
            String nodesContent = StrUtil.EMPTY;
            if (scriptResult != null && scriptResult.size() >= 0) {
                for (ConfigSimpleFlowCodeDO liteflowScript : liteflowScriptList) {
                    String id = liteflowScript.getCodeNo();
                    String data = liteflowScript.getCodeData();
                    String name = liteflowScript.getCodeName();
                    String type = liteflowScript.getCodeType();
                    String language = liteflowScript.getCodeLanguage();
                    if (StringUtils.isNotBlank(language)) {
                    	scriptResult.add(StrUtil.format(QmsXmlFlowElParserConstant.NODE_ITEM_WITH_LANGUAGE_XML_PATTERN, XmlUtil.escape(id), XmlUtil.escape(name), type, language, data));
    				} else {
    					scriptResult.add(StrUtil.format(QmsXmlFlowElParserConstant.NODE_ITEM_XML_PATTERN, XmlUtil.escape(id), XmlUtil.escape(name), type, data));
    				}
                }
                nodesContent = StrUtil.format(QmsXmlFlowElParserConstant.NODE_XML_PATTERN, CollUtil.join(scriptResult, StrUtil.EMPTY));
            }
            
            List<ConfigSimpleFlowRuleDO> liteflowChainList = configSimpleFlowRuleService.getConfigSimpleFlowRuleList();
            List<String> chainResult = new ArrayList<>();
            for (ConfigSimpleFlowRuleDO liteflowChain : liteflowChainList) {
                String chainName = liteflowChain.getName() + liteflowChain.getTenantId();
                String elData = liteflowChain.getData();
                String route = liteflowChain.getRoute();
                String namespace = liteflowChain.getGroupName();
                if (StringUtils.isNotBlank(route)) {//决策路由
    				chainResult.add(StrUtil.format(QmsXmlFlowElParserConstant.CHAIN_WITH_ROUTE_XML_PATTERN, XmlUtil.escape(chainName), StrUtil.emptyIfNull(namespace), StrUtil.emptyIfNull(route), elData));
    			} else {
    				chainResult.add(StrUtil.format(QmsXmlFlowElParserConstant.CHAIN_XML_PATTERN, XmlUtil.escape(chainName), elData));
    			}
                
            }
            //当还未配置时，添加一个默认的规则，否则启动会报错
            if (chainResult.size() <= 0) {
				chainResult.add(StrUtil.format(QmsXmlFlowElParserConstant.CHAIN_XML_PATTERN, "default-demo", "SER()"));
			}
            String chainsContent = CollUtil.join(chainResult, StrUtil.EMPTY);
            
            return StrUtil.format(QmsXmlFlowElParserConstant.XML_PATTERN, nodesContent, chainsContent);
    	});
        return content;
    }
    
    

}
