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

import com.alibaba.fastjson.annotation.JSONField;
import com.friday.operation.api.distributor.constant.TaieJobState;
import com.friday.operation.api.tooldriver.dto.rpa.JobDto;
import com.friday.tooldriver.constants.uipath.JobState;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class JobResponseDto implements Serializable {
    @JSONField(name = "Key")
    private String key;
    @JSONField(name = "StartTime")
    private String startTime;
    @JSONField(name = "EndTime")
    private String endTime;
    @JSONField(name = "State")
    private String state;
    @JSONField(name = "Robot")
    private SimpleRobotResponseDto robot;
    @JSONField(name = "Release")
    private ReleaseResponseDto release;
    @JSONField(name = "Source")
    private String source;
    @JSONField(name = "SourceType")
    private String sourceType;
    @JSONField(name = "BatchExecutionKey")
    private String batchExecutionKey;
    @JSONField(name = "Info")
    private String info;
    @JSONField(name = "CreationTime")
    private String creationTime;
    @JSONField(name = "StartingScheduleId")
    private int startingScheduleId;
    @JSONField(name = "ReleaseName")
    private String releaseName;
    @JSONField(name = "Type")
    private String type;
    @JSONField(name = "InputArguments")
    private String inputArguments;
    @JSONField(name = "OutputArguments")
    private String outputArguments;
    @JSONField(name = "HostMachineName")
    private String hostMachineName;
    @JSONField(name = "Id")
    private int id;
    @JSONField(name = "Name")
    private String name;

    public JobDto transferToJobDto(){
        return JobDto.builder()
                .jobId(String.valueOf(id))
                .source(source)
                .state(this.transferToJobState())
                .creationTime(creationTime)
                .startTime(startTime)
                .endTime(endTime)
                .inputArguments(inputArguments)
                .outputArguments(outputArguments)
                .build();
    }

    private String transferToJobState(){
        switch (state) {
            case "Pending":
                return TaieJobState.Pending.getState();
            case "Running":
            case "Resumed":
                return TaieJobState.Running.getState();
            case "Successful":
                return TaieJobState.Success.getState();
            case "Faulted":
                return TaieJobState.Fail.getState();
            case "Stopped":
                return TaieJobState.Terminated.getState();
            case "Stopping":
                return TaieJobState.Pausing.getState();
            case "Terminating":
                return TaieJobState.Terminating.getState();
            case "Suspended":
                return TaieJobState.Paused.getState();
            default:
                return null;
        }
    }

    public static String createFromJobState(String jobState){
        switch (jobState) {
            case "Pending":
                return JobState.Pending.name();
            case "Running":
                return JobState.Running.name();
            case "Success":
                return JobState.Successful.name();
            case "Fail":
                return JobState.Faulted.name();
            case "Terminated":
                return JobState.Stopped.name();
            case "Terminating":
                return JobState.Terminating.name();
            case "Paused":
                return JobState.Suspended.name();
            default:
                return null;
        }
    }
}
