package com.friday.operation.api.keepalive.dto.smartrpa;

import com.friday.operation.api.keepalive.constant.smartrpa.JobState;
import com.friday.operation.api.keepalive.constant.smartrpa.RobotState;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author YaoYao
 * @Date Created In 18:57 2022/11/29
 * @Description 指令确认数据结构
 * @Modified By
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel(description = "指令确认数据结构")
public class ActionConfirmReq extends ExecActionDto {
    @ApiModelProperty(value = "指令是否执行成功(Y/N)")
    private String success;
    @ApiModelProperty(value = "失败原因")
    private String msg;
    @ApiModelProperty(value = "任务状态")
    private String jobState;
    @ApiModelProperty(value = "任务起始时间")
    private String startTime;
    @ApiModelProperty(value = "任务终止时间")
    private String endTime;
    @ApiModelProperty(value = "待设定机器状态")
    private String machineState;

    public boolean reqValid(){
        return super.actionValid() && actionStateValid() && jobStateValid() && machineStateValid();
    }

    public boolean actionStateValid(){
        return StringUtils.isEmpty(success)
                || "Y".equals(success)
                || "N".equals(success);
    }

    boolean jobStateValid(){
        return StringUtils.isAnyEmpty(jobState, super.getJobId())
                || (super.jobIdValid() && (JobState.Pending.getState().equals(jobState)
                    || JobState.Running.getState().equals(jobState)
                    || JobState.Success.getState().equals(jobState)
                    || JobState.Fail.getState().equals(jobState)
                    || JobState.Faulted.getState().equals(jobState)
                    || JobState.Terminating.getState().equals(jobState)
                    || JobState.Terminated.getState().equals(jobState)
                    || JobState.Pausing.getState().equals(jobState)
                    || JobState.Paused.getState().equals(jobState)
                    || JobState.Resuming.getState().equals(jobState)
                    || JobState.Invalid.getState().equals(jobState)));
    }

    boolean machineStateValid() {
        return StringUtils.isEmpty(machineState)
                || RobotState.Available.getState().equals(machineState)
                || RobotState.Busy.getState().equals(machineState)
                || RobotState.Exception.getState().equals(machineState)
                || RobotState.Maintenance.getState().equals(machineState)
                || RobotState.Unresponsive.getState().equals(machineState)
                || RobotState.Disconnected.getState().equals(machineState);
    }

    public boolean actionSuccess(){
        return "Y".equals(success);
    }

    public boolean actionFail(){
        return "N".equals(success);
    }

    public boolean confirmJobStateValid(){
        return JobState.Success.getState().equals(jobState)
                || JobState.Fail.getState().equals(jobState)
                || JobState.Faulted.getState().equals(jobState)
                || JobState.Paused.getState().equals(jobState)
                || JobState.Terminated.getState().equals(jobState);
    }

    public boolean confirmJobEnded(){
        return JobState.Success.getState().equals(jobState)
            || JobState.Fail.getState().equals(jobState)
            || JobState.Faulted.getState().equals(jobState)
            || JobState.Terminated.getState().equals(jobState);
    }
}
