package cd.casic.ci.process.engine.runContext;

import cd.casic.ci.process.process.dataObject.base.PipBaseElement;
import cd.casic.ci.process.process.dataObject.task.PipTask;
import cd.casic.framework.commons.exception.ServiceException;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class TaskRunContext extends BaseRunContext{
    public TaskRunContext(PipTask contextDef, SecondStageRunContext parentContext,Map<String,Object> localVariable) {
        super(contextDef,0, parentContext, LocalDateTime.now(), parentContext.getResourceId(), parentContext.getTargetVersionId(), parentContext.getTargetType(), parentContext.getGlobalVariables(),localVariable, new HashMap<>());
    }
    private TaskRunContext(PipBaseElement contextDef, BaseRunContext parentContext, LocalDateTime startTime, String resourceId, String targetId, String targetType, Map<String, Object> globalVariables, Map<String, Object> localVariables, Map<String, BaseRunContext> childContext) {
        super(contextDef,0, parentContext, startTime, resourceId, targetId, targetType, globalVariables, localVariables, childContext);
    }

    /**
     *  task是最后一层没有下一级所以如果id相同直接返回它自己
     * */
    @Override
    public BaseRunContext getRunContext(String id) {
        if (StringUtils.isEmpty(id)||!id.equals(this.getContextDef().getId())) {
            return null;
        }
        return this;
    }

    @Override
    public void putChildRunContext(String key, BaseRunContext context) {
        throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"task没有子阶段");
    }
}
