package com.friday.distributor.frame;

import com.friday.distributor.common.ExecutorMethodInvokeHelper;
import com.friday.base.common.response.DubboResult;
import com.friday.distributor.bean.Job;
import com.friday.distributor.bean.TaieJobExecInfo;
import com.friday.operation.api.distributor.constant.TaieJobState;
import com.friday.operation.api.distributor.dto.StandardJobInput;
import com.friday.operation.api.distributor.dto.TaskResultDto;
import com.friday.operation.api.distributor.dto.TaskResultFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

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


    @Override
    public DubboResult<String> asyncStartTaieJob(Job job) {
        invokeHelper.executeJob(job, StandardJobInput.builder()
                .baseParam(job.getJobInput().getBaseParam())
                .bizParam(job.getJobInput().getBizParam())
                .configParam(job.getJobInput().getConfigParam())
                .build());
        return DubboResult.newSuccess(String.valueOf(job.getInvokeId()));
    }

    @Override
    public DubboResult<TaskResultDto> syncStartTaieJob(Job job) {
        DubboResult result = invokeHelper.executeJob(job, job.getJobInput());
        if(!result.success()){
            return result;
        }
        TaskResultDto jobResult = new TaskResultDto();
        jobResult.setExecState("SUCCESS");
        Map<String, Object> jobResultData = (HashMap)result.getObject();
        jobResult.setResultData(jobResultData);
        return DubboResult.newSuccess(jobResult);
    }

    @Override
    public DubboResult stopTaieJob(Job job) {
        return DubboResult.newSuccess();
    }

    @Override
    public DubboResult<TaieJobExecInfo> getTaieJobExecDetail(Job job) {
        TaskResultFilter filter = new TaskResultFilter();
        filter.setTaskId(job.getTaskId());
        filter.setInvokeId(job.getInvokeId());
        DubboResult<TaskResultDto> result = invokeHelper.getTaskResult(filter);
        if(!result.success() || Objects.isNull(result.getObject())){
            return DubboResult.newFailure(DubboResult.SERVER_EXCEPTION, result.getMessage());
        }
        TaieJobExecInfo taieJobExecInfo = TaieJobExecInfo.builder()
                .invokeId(job.getInvokeId())
                .execJobId(job.getExecJobId())
                .jobState(transferJobState(result.getObject().getExecState()))
                .build();
        return DubboResult.newSuccess(taieJobExecInfo);
    }

    private String transferJobState(String jobInvokeState){
        switch (jobInvokeState) {
            case "UNSTART":
            case "RUNNING":
                return TaieJobState.Running.getState();
            case "SUCCESS":
                return TaieJobState.Success.getState();
            case "FAIL":
                return TaieJobState.Fail.getState();
            case "TERMINATE":
                return TaieJobState.Terminated.getState();
            default:
                return TaieJobState.Invalid.getState();
        }
    }
}
