package com.friday.distributor.frame;

import com.alibaba.fastjson.JSON;
import com.friday.base.common.response.DubboResult;
import com.friday.blank.api.dto.taskType.TaskTypeDetailDto;
import com.friday.distributor.bean.Job;
import com.friday.distributor.bean.TaieJobExecInfo;
import com.friday.operation.api.distributor.dto.TaskResultDto;
import com.friday.operation.api.tooldriver.dto.common.DriverBase;
import com.friday.operation.api.tooldriver.dto.rpa.JobDto;
import com.friday.operation.api.tooldriver.dto.rpa.StartJobDto;
import com.friday.operation.api.tooldriver.service.ICapabilityDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.friday.operation.api.tooldriver.constant.RpaJobStartSourceEnum.Distributor;
import static com.friday.operation.api.tooldriver.constant.RpaRobotTypeEnum.Attended;
import static com.friday.operation.api.tooldriver.constant.RpaRobotTypeEnum.Unattended;

/**
 * @Author YaoYao
 * @Date Created In 11:32 2022/2/24
 * @Description RPA运行相关
 * @Modified By
 */
@Service("RPAExecFrameService")
public class RPAExecFrameService implements IBizTypeStrategyContext{
    @Autowired
    private ICapabilityDriver rpaDriver;

    @Override
    public DubboResult<String> asyncStartTaieJob(Job job) {
        TaskTypeDetailDto taskExecConfig = job.getTaskExecConfig();
        if(Attended.getType().equals(taskExecConfig.getSchedule().getExecuteMethod())){
            //todo 调用先锋客户端启动
            return DubboResult.newSuccess("Success");
        }
        if(Unattended.getType().equals(taskExecConfig.getSchedule().getExecuteMethod())){
            DriverBase driverBase = DriverBase.builder()
                    .tenantKey(taskExecConfig.getProcess().getOcTenantKey())
                    .groupKey(taskExecConfig.getProcess().getGroupId())
                    .build();
            StartJobDto start = StartJobDto.builder()
                    .robotKey(job.getExecRobot().getRobotKey())
                    .processReleaseKey(taskExecConfig.getProcess().getReleaseId())
                    .inputArguments(JSON.toJSONString(job.getJobInput()))
                    .source(Distributor.getSource())
                    .build();
            DubboResult<JobDto> startRpaResult = rpaDriver.startJob(driverBase, start);
            if(!startRpaResult.success()){
                return DubboResult.newFailure(startRpaResult.getCode(), startRpaResult.getMessage());
            }
            return DubboResult.newSuccess(startRpaResult.getObject().getJobId());
        }
        return DubboResult.newFailure(DubboResult.CLIENT_EXCEPTION, "不支持的启动类型:" + taskExecConfig.getSchedule().getExecuteMethod());
    }

    @Override
    public DubboResult<TaskResultDto> syncStartTaieJob(Job job) {
        return DubboResult.newFailure(DubboResult.CLIENT_EXCEPTION, "RPA任务不支持同步启动");
    }

    @Override
    public DubboResult stopTaieJob(Job job) {
        TaskTypeDetailDto taskExecConfig = job.getTaskExecConfig();
        if(Attended.getType().equals(taskExecConfig.getSchedule().getExecuteMethod())){
            return DubboResult.newFailure(DubboResult.SERVER_EXCEPTION, "不支持终止前台任务");
        }
        if(Unattended.getType().equals(taskExecConfig.getSchedule().getExecuteMethod())){
            DriverBase driverBase = DriverBase.builder()
                    .tenantKey(taskExecConfig.getProcess().getOcTenantKey())
                    .groupKey(taskExecConfig.getProcess().getGroupId())
                    .build();
            return rpaDriver.stopJob(driverBase, job.getExecJobId());
        }
        return DubboResult.newSuccess();
    }

    @Override
    public DubboResult<TaieJobExecInfo> getTaieJobExecDetail(Job job) {
        TaskTypeDetailDto taskExecConfig = job.getTaskExecConfig();
        DriverBase driverBase = DriverBase.builder()
                .tenantKey(taskExecConfig.getProcess().getOcTenantKey())
                .groupKey(taskExecConfig.getProcess().getGroupId())
                .build();
        DubboResult<JobDto> jobExecDetail = rpaDriver.queryJobByKey(driverBase, job.getExecJobId());
        if(!jobExecDetail.success()){
            return DubboResult.newFailure(DubboResult.SERVER_EXCEPTION, jobExecDetail.getMessage());
        }
        TaieJobExecInfo taieJobExecInfo = TaieJobExecInfo.builder()
                .invokeId(job.getInvokeId())
                .execJobId(job.getExecJobId())
                .jobState(jobExecDetail.getObject().getState())
                .build();
        return DubboResult.newSuccess(taieJobExecInfo);
    }
}
