package com.friday.tooldriver.dto.infodator.response;

import com.friday.operation.api.tooldriver.constant.RpaJobStateEnum;
import com.friday.operation.api.tooldriver.dto.rpa.JobDto;
import com.friday.operation.api.tooldriver.dto.rpa.ReleaseDto;
import com.friday.operation.api.tooldriver.dto.rpa.RobotDto;
import com.friday.tooldriver.constants.infodator.InfodatorTaskRunStatusEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TaskStatus extends Task implements Serializable {
    private Integer creatorId;
    private Boolean deleted;
    private Integer version;
    private String taskName;
    private Integer triggerId;
    private String blackRobotIdList;
    private String robotGroupId;
    private String timeoutMinute ;
    private Integer trueTaskPriority;
    private Integer taskRunStatus;
    private String taskRunStatusManing;
    private String remark;
    private String startTime;
    private String endTime;
    private Integer successCount;
    private Integer failCount;
    private Integer unknownCount;
    private Integer remainCount;
    private Integer totalCount;
    private String itemHead;
    private String spendTimeMSTotal;
    private String spendTimeMSRemain;

    public JobDto createJobDto(){
        return JobDto.builder()
                .jobId(String.valueOf(this.getId()))
                .source("")
                .release(ReleaseDto.builder().build())
                .robot(RobotDto.builder().build())
                .creationTime(this.getCreateTime())
                .startTime(this.getStartTime())
                .endTime(this.getEndTime())
                .state(this.transferToJobState())
                .inputArguments(this.getInputArguments())
                .outputArguments("")
                .info(null)
                .build();
    }

    private String transferToJobState(){
        InfodatorTaskRunStatusEnum runStatusEnum = InfodatorTaskRunStatusEnum.getCode(this.getTaskRunStatus());
        switch (runStatusEnum) {
            case Waiting:
                return RpaJobStateEnum.Pending.getState();
            case Running:
                return RpaJobStateEnum.Resuming.getState();
            case Pausing:
                return RpaJobStateEnum.Pausing.getState();
            case Paused:
                return RpaJobStateEnum.Paused.getState();
            case Cancelling:
                return RpaJobStateEnum.Terminating.getState();
            case Cancelled:
                return RpaJobStateEnum.Terminated.getState();
            case Timeout:
            case CompletedWithError:
                return RpaJobStateEnum.Fail.getState();
            case Terminated:
                return RpaJobStateEnum.Faulted.getState();
            case Completed:
                return RpaJobStateEnum.Success.getState();
            default:
                return null;
        }
    }
}
