package net.northking.cctp.se.script.bean;

import lombok.Data;
import net.northking.cctp.element.core.IScriptStep;
import net.northking.cctp.element.core.exception.ExecuteException;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 脚本步骤对象
 * @author  xuyang.wang
 */
@Data
public class ScriptStep implements IScriptStep, Serializable {
    /**
     * 步骤Id
     */
    private String id;
    /**
     * 是否跳过步骤
     */
    private boolean skip;
    /**
     * 出错继续下一步
     */
    private boolean ignoreError;
    /**
     * 调用组件库
     */
    private String componentLib;
    /**
     * 调用组件命令
     */
    private String command;
    /**
     * 步骤操作对象
     */
    private List<StepTarget> targets;
    /**
     * 步骤输出对象
     */
    private Map<String,String> output;
    /**
     * 步骤变量
     */
    private Map<String,String> args;
    /**
     * 步骤标题
     */
    private String title;
    /**
     * 步骤缩略图
     */
    private String imgUri;
    /**
     * 子步骤
     */
    private ScriptStep[] children;
    /**
     * if步骤map
     */
    private Map<String, List<ScriptStep>> ifStepMap;
    /**
     *
     */
    private String ifStep;
    /**
     * 可执行的子步骤
     */
    private List<ScriptStep> childSteps;

    /**
     * 是否最后一步
     */
    private boolean finalStep;

    /**
     * 父步骤的下一步
     */
    private String parentNextStepId;

    /**
     * 扩展属性
     */
    private Map<String, Object> extAttrs;

    /**
     * 输入参数
     */
    private Map<String, String> inputMap = new HashMap<>();

    public String getExtAttr(String attrKey) {
        if (this.extAttrs != null && this.extAttrs.containsKey(attrKey)
                && this.extAttrs.get(attrKey) != null) {
            return this.extAttrs.get(attrKey).toString();
        }
        return null;
    }

    public String getArg(String argKey) {
        if (this.args != null && this.args.containsKey(argKey)
                && this.args.get(argKey) != null) {
            return this.args.get(argKey);
        }
        return null;
    }

    public void addChildStep(ScriptStep child) {
        if (null == this.childSteps) {
            this.childSteps = new ArrayList<>();
        }
        this.childSteps.add(child);
    }

    /**
     * 添加if组件的步骤
     *
     * @param key
     * @param children
     */
    public void addIfStep(String key, ScriptStep children) {
        if (StringUtils.isBlank(key)) {
            return;
        }
        try {
            LogicStepType type = LogicStepType.valueOf(children.getCommand().toLowerCase());
            if (ifStepMap == null) {
                ifStepMap = new HashMap<>();
            }
            ifStepMap.computeIfAbsent(type.name(), k -> new ArrayList<>());
            ifStepMap.get(type.name()).add(children);
        } catch (Exception e) {
            throw new ExecuteException(String.format("组件 %s 不属于if的子组件", key));
        }
    }
}
