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

import com.friday.operation.api.tooldriver.constant.RpaJobStateEnum;
import com.friday.operation.api.tooldriver.constant.RpaRobotStateEnum;
import com.friday.operation.api.tooldriver.dto.rpa.RobotDto;
import com.friday.tooldriver.constants.infodator.InfodatorRobotState;
import com.friday.tooldriver.constants.infodator.InfodatorTaskRunStatusEnum;
import lombok.*;

import java.io.Serializable;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class RpaRobot implements Serializable {
    /**
     * 机器人注册申请状态
     */
    private Integer approveStatus;
    private String robotUniqueId;
    private Integer groupId;
    private String osArch;
    private String activateType;
    private Integer creatorId;
    private boolean supportLiveShow;
    private String clientVersion;
    private String machineName;
    private String features;
    private String osInfo;
    private String osVersion;
    private  String expireDate;
    private Integer id;
    private  String lastHeartBeartTime;
    private String machineCode;
    private String robotName;
    private String ip;
    private String prevRobotUniqueId;
    private boolean supportRemoteCtrl;
    private  String updateTime;
    private String osName;
    private Integer version;
    private String robotDesc;
    private boolean deleted;
    private  String createTime;
    private Integer robotUserId;
    private Integer runningState;
    private Integer onlineState;

    public RobotDto createRobotDto(){
        return RobotDto.builder()
                .key(String.valueOf(this.getId()))
                .name(this.getRobotName())
                .state(this.transferToJobState())
                .build();
    }

    public String transferToJobState(){
        if (InfodatorRobotState.OnlineStateEnum.AVAILABLE.getState().equals(this.getOnlineState())) {
            return RpaRobotStateEnum.Disconnected.getState();
        } else if (InfodatorRobotState.RunningStateEnum.Available.getState().equals(this.getRunningState())) {
            return RpaRobotStateEnum.Available.getState();
        } else if (InfodatorRobotState.RunningStateEnum.Busy.getState().equals(this.getRunningState())) {
            return RpaRobotStateEnum.Busy.getState();
        } else {
            return RpaRobotStateEnum.Unknown.getState();
        }
    }

    public static Integer[] createFromJobState(String jobState){
        switch (jobState) {
            case "Pending":
                return new Integer[]{InfodatorTaskRunStatusEnum.Initializing.getCode(), InfodatorTaskRunStatusEnum.Waiting.getCode()};
            case "Running":
                return new Integer[]{InfodatorTaskRunStatusEnum.Running.getCode()};
            case "Success":
                return new Integer[]{InfodatorTaskRunStatusEnum.Completed.getCode(), InfodatorTaskRunStatusEnum.CompletedWithError.getCode()};
            case "Fail":
                return new Integer[]{InfodatorTaskRunStatusEnum.Terminated.getCode(), InfodatorTaskRunStatusEnum.Timeout.getCode()};
            case "Terminated":
                return new Integer[]{InfodatorTaskRunStatusEnum.Cancelled.getCode()};
            case "Terminating":
                return new Integer[]{InfodatorTaskRunStatusEnum.Cancelling.getCode()};
            case "Paused":
                return new Integer[]{InfodatorTaskRunStatusEnum.Pausing.getCode(), InfodatorTaskRunStatusEnum.Paused.getCode()};
            default:
                return new Integer[]{};
        }
    }


}
