package com.friday.tooldriver.po.smartrpa;

import com.friday.tooldriver.dto.smartrpa.SmartJobDto;
import com.friday.operation.api.keepalive.constant.smartrpa.JobState;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

import static com.friday.operation.api.keepalive.constant.smartrpa.ExecAction.*;
import static com.friday.operation.api.keepalive.constant.smartrpa.JobState.*;

/**
 * @Author YaoYao
 * @Date Created In 9:32 2022/12/8
 * @Description
 * @Modified By
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class JobPo {
    /**
     * 主键ID
     */
    private Integer id;
    /**
     * 任务来源
     */
    private String source;
    /**
     * 运行机器
     */
    private String robotKey;
    /**
     * 流程
     */
    private String processReleaseKey;
    /**
     * 任务状态
     * {@link JobState}
     */
    private String state;
    /**
     * 任务状态上次修改时间
     */
    private LocalDateTime stateLastUpdateTime;
    /**
     * 是否需要rpa主动同步状态
     */
    private Boolean syncState;
    /**
     * 上次请求更新任务状态时间
     */
    private LocalDateTime lastSyncReqTime;
    /**
     * 任务开始时间
     */
    private LocalDateTime startTime;
    /**
     * 任务结束时间
     */
    private LocalDateTime endTime;
    /**
     * 任务入参
     */
    private String inputArguments;
    /**
     * 任务出参
     */
    private String outputArguments;
    /**
     * 是否有效
     */
    private Boolean valid;
    /**
     * 备注
     */
    private String remark;
    /**
     * 创建时间
     */
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    public SmartJobDto transferToSmartJobDto(){
        return SmartJobDto.builder()
                .id(id)
                .source(source)
                .robotKey(robotKey)
                .processReleaseKey(processReleaseKey)
                .startTime(startTime)
                .endTime(endTime)
                .inputArguments(inputArguments)
                .outputArguments(outputArguments)
                .state(state)
                .syncState(syncState)
                .remark(remark)
                .build();
    }

    public boolean ended(){
        return Success.getState().equals(state)
                || Fail.getState().equals(state)
                || Faulted.getState().equals(state)
                || Terminated.getState().equals(state);
    }

    public boolean started(){
        return !Pending.getState().equals(state);
    }

    public boolean pending(){
        return Pending.getState().equals(state);
    }

    public boolean pausing(){
        return Pausing.getState().equals(state);
    }

    public boolean terminating(){
        return Terminating.getState().equals(state);
    }

    public boolean resuming(){
        return Resuming.getState().equals(state);
    }

    public String getActionByState(){
        if(Pending.getState().equals(state)){
            return Start.getAction();
        } else if(Terminating.getState().equals(state)){
            return Terminate.getAction();
        } else if(Pausing.getState().equals(state)){
            return Pause.getAction();
        } else if(Resuming.getState().equals(state)){
            return Resume.getAction();
        }
        return "";
    }
}
