package cn.fateverse.workflow.entity;

import cn.fateverse.workflow.entity.bpmn.UserInfo;
import cn.fateverse.workflow.enums.OperationStateEnums;
import cn.fateverse.workflow.enums.OperationEnums;
import com.alibaba.fastjson2.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.task.service.delegate.DelegateTask;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 操作历史对象
 *
 * @author Clay
 * @date 2023-02-24
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class HistoricalOperation {

    //任务id
    private String taskId;
    //节点id
    private String nodeId;
    //流程实例id
    private String processInstanceId;
    //操作名称
    private String operationName;
    //行为
    private OperationEnums operation;
    //审批模式
    private String approvalMode;
    //评论json数据
    private String message;
    //完成时间
    private Date finishTime;
    //节点类型
    private String nodeType;
    //返回类型
    private OperationStateEnums state;
    //开始时间
    private Long startTime;
    //用户json数据
    private String userInfo;
    //是否被标记, 标记表示当前操作不纳入到节点分析列表,0表示纳入
    private Integer mark;


    public static HistoricalOperation toHistoricalOperation(DelegateTask delegateTask, UserInfo userInfo) {
        return HistoricalOperation.builder()
                .taskId(makeTaskId())
                .nodeId(delegateTask.getTaskDefinitionKey())
                .processInstanceId(delegateTask.getProcessInstanceId())
                .operation(OperationEnums.OPINION)
                .operationName(delegateTask.getName())
                .state(OperationStateEnums.RUNNING)
                .startTime(new Date().getTime())
                .userInfo(JSON.toJSONString(userInfo))
                .build();
    }

    public static HistoricalOperation toHistoricalOperation(DelegateTask delegateTask, List<UserInfo> userInfos) {
        return HistoricalOperation.builder()
                .taskId(makeTaskId())
                .nodeId(delegateTask.getTaskDefinitionKey())
                .processInstanceId(delegateTask.getProcessInstanceId())
                .operation(OperationEnums.OPINION)
                .operationName(delegateTask.getName())
                .state(OperationStateEnums.RUNNING)
                .startTime(new Date().getTime())
                .userInfo(JSON.toJSONString(userInfos))
                .build();
    }

    public static HistoricalOperation toHistoricalOperation(DelegateExecution execution, List<UserInfo> userInfos, String nodeName, String approvalMode) {
        return HistoricalOperation.builder()
                .taskId(makeTaskId())
                .nodeId(execution.getCurrentActivityId())
                .processInstanceId(execution.getProcessInstanceId())
                .approvalMode(approvalMode)
                .operation(OperationEnums.OPINION)
                .operationName(nodeName)
                .state(OperationStateEnums.RUNNING)
                .startTime(new Date().getTime())
                .userInfo(JSON.toJSONString(userInfos))
                .build();
    }

    public static HistoricalOperation toHistoricalOperation(DelegateExecution execution, UserInfo userInfo) {
        return toHistoricalOperation(execution, userInfo, OperationEnums.CREATE);
    }

    public static HistoricalOperation toHistoricalOperation(DelegateExecution execution, List<UserInfo> list) {
        return toHistoricalOperation(execution, list, OperationEnums.CREATE);
    }

    public static HistoricalOperation toHistoricalOperation(DelegateExecution execution, UserInfo userInfo, OperationEnums operation) {
        return toHistoricalOperation(execution, Collections.singletonList(userInfo), operation);

    }

    public static HistoricalOperation toHistoricalOperation(DelegateExecution execution, List<UserInfo> list, OperationEnums operation) {
        Date date = new Date();
        return HistoricalOperation.builder()
                .taskId(makeTaskId())
                .nodeId(execution.getCurrentActivityId())
                .processInstanceId(execution.getProcessInstanceId())
                .operation(operation)
                .operationName("提交申请")
                .state(OperationStateEnums.AGREE)
                .startTime(date.getTime())
                .finishTime(date)
                .userInfo(JSON.toJSONString(list))
                .build();
    }

    public static String makeTaskId() {
        return UUID.randomUUID().toString();
    }


}
