package com.friday.tooldriver.dto.smartrpa;

import com.friday.base.common.utils.DataConvertUtil;
import com.friday.operation.api.keepalive.constant.smartrpa.JobState;
import com.friday.operation.api.tooldriver.dto.rpa.JobDto;
import com.friday.tooldriver.po.smartrpa.JobPo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;

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

/**
 * @Author YaoYao
 * @Date Created In 16:50 2022/12/12
 * @Description 神码robot
 * @Modified By
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SmartJobDto implements Serializable {
    /**
     * 主键ID
     */
    private Integer id;
    /**
     * 任务来源
     */
    private String source;
    /**
     * 运行机器
     */
    private String robotKey;
    /**
     * 流程
     */
    private String processReleaseKey;
    /**
     * 任务状态
     * {@link JobState}
     */
    private String state;
    /**
     * 是否需要rpa主动同步状态
     */
    private Boolean syncState;
    /**
     * 任务开始时间
     */
    private LocalDateTime startTime;
    /**
     * 任务结束时间
     */
    private LocalDateTime endTime;
    /**
     * 任务入参
     */
    private String inputArguments;
    /**
     * 任务出参
     */
    private String outputArguments;
    /**
     * 备注
     */
    private String remark;

    public JobPo transferToJobPo(){
        return JobPo.builder()
                .id(id)
                .source(source)
                .robotKey(robotKey)
                .processReleaseKey(processReleaseKey)
                .startTime(startTime)
                .endTime(endTime)
                .inputArguments(inputArguments)
                .outputArguments(outputArguments)
                .state(state)
                .syncState(syncState)
                .valid(true)
                .remark(remark)
                .build();
    }

    public JobDto transferToJobDto(){
        return JobDto.builder()
                .jobId(String.valueOf(id))
                .source(source)
                .startTime(DataConvertUtil.localDateTimeToString(startTime))
                .endTime(DataConvertUtil.localDateTimeToString(endTime))
                .inputArguments(inputArguments)
                .outputArguments(outputArguments)
                .state(state)
                .info(remark)
                .build();
    }

    public boolean canStop(){
        return Pending.getState().equals(state) || Running.getState().equals(state) || Paused.getState().equals(state);
    }

    public boolean canPause(){
        return Running.getState().equals(state);
    }

    public boolean canResume(){
        return Paused.getState().equals(state);
    }
}
