package com.friday.tooldriver.po.smartrpa;

import com.friday.tooldriver.dto.smartrpa.SmartRobotDto;
import com.friday.operation.api.keepalive.constant.smartrpa.LicenseState;
import com.friday.operation.api.keepalive.constant.smartrpa.RobotState;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;

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

/**
 * @Author YaoYao
 * @Date Created In 9:32 2022/12/8
 * @Description
 * @Modified By
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class RobotPo {
    /**
     * 主键ID
     */
    private Integer id;
    /**
     * 机器码
     */
    private String machineCode;
    /**
     * 机器用户
     */
    private String machineLoginUser;
    /**
     * 机器名称
     */
    private String robotKey;
    /**
     * 机器状态
     * {@link RobotState}
     */
    private String robotState;
    /**
     * 机器状态上次更新时间
     */
    private LocalDateTime robotStateLastUpdateTime;
    /**
     * 许可状态
     * {@link LicenseState}
     */
    private String licenseState;
    /**
     * 许可状态上次更新时间
     */
    private LocalDateTime licenseLastUpdateTime;
    /**
     * 待设置机器状态
     * {@link RobotState}
     */
    private String robotStateToChange;
    /**
     * 未响应次数
     */
    private Integer unresponsiveTime;
    /**
     * 是否有效
     */
    private Boolean valid;
    /**
     * 备注
     */
    private String remark;
    /**
     * 创建时间
     */
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    private LocalDateTime updateTime;

    /**
     * 机器是否需要登录验证
     */
    private String authState;

    /**
     * 中控租戶id
     */
    private Long ocTenantId;


    public static RobotPo createFromSmartRobotDto(SmartRobotDto robotDto){
        return RobotPo.builder()
                .id(robotDto.getId())
                .robotKey(robotDto.getRobotKey())
                .machineCode(robotDto.getMachineCode())
                .machineLoginUser(robotDto.getMachineLoginUser())
                .robotState(robotDto.getRobotState())
                .robotStateLastUpdateTime(robotDto.getRobotStateLastUpdateTime())
                .licenseState(robotDto.getLicenseState())
                .licenseLastUpdateTime(robotDto.getLicenseLastUpdateTime())
                .robotStateToChange(robotDto.getRobotStateToChange())
                .authState(robotDto.getAuthState())
                .ocTenantId(robotDto.getOcTenantId())
                .build();
    }

    public SmartRobotDto transferToSmartRobotDto(){
        return SmartRobotDto.builder()
                .id(id)
                .ocTenantId(ocTenantId)
                .robotKey(robotKey)
                .machineCode(machineCode)
                .machineLoginUser(machineLoginUser)
                .robotState(robotState)
                .robotStateLastUpdateTime(robotStateLastUpdateTime)
                .licenseState(licenseState)
                .licenseLastUpdateTime(licenseLastUpdateTime)
                .robotStateToChange(robotStateToChange)
                .authState(authState)
                .build();
    }

    public String getActionByState(){
        StringBuilder action = new StringBuilder();
        if(ToggleOn.getState().equals(licenseState)){
            action.append(EnableLicense.getAction());
        } else if(ToggleOff.getState().equals(licenseState)){
            action.append(DisableLicense.getAction());
        }
        if(StringUtils.isNotEmpty(robotStateToChange)){
            action.append(SetMachineState.getAction());
        }
        return action.toString();
    }
}
