package org.springblade.flow.engine.custom.type;

import static org.springblade.flow.base.util.BpmnModelUtils.getAttributeValueByTaskDefinitionKey;

import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.Process;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.impl.RepositoryServiceImpl;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springblade.flow.base.util.SpringContextHolder;
import org.springblade.flow.engine.assembly.UserTaskExPropertyDTO;
import org.springblade.flow.engine.assembly.prop.UserTaskNodeEventDTO;
import org.springblade.flow.util.wxutil.HttpUtil;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import lombok.extern.slf4j.Slf4j;

/**
 * @author LoveMyOrange
 * @create 2022-01-18 17:22
 */
@Slf4j
@Component
public class TaskCompletedListener implements FlowableEventListener {
    @Override
    public void onEvent(FlowableEvent activitiEvent) {
        if (!(activitiEvent instanceof FlowableEntityEventImpl)) {
            return;
        }
        FlowableEntityEventImpl activitiEntityEventImpl = (FlowableEntityEventImpl) activitiEvent;
        Object entity = activitiEntityEventImpl.getEntity();

        if (!(entity instanceof TaskEntity)) {
            return;
        }
        TaskEntity taskEntity = (TaskEntity) entity;
        String taskDefinitionKey = taskEntity.getTaskDefinitionKey();
        RepositoryServiceImpl repositoryService = SpringContextHolder.getBean(RepositoryServiceImpl.class);
        Process mainProcess = repositoryService.getBpmnModel(taskEntity.getProcessDefinitionId()).getMainProcess();
        String property = getAttributeValueByTaskDefinitionKey(mainProcess, taskDefinitionKey, "userTask");
        UserTaskExPropertyDTO exPropertyDTO = null;
        if (StringUtils.isNotBlank(property)) {
            exPropertyDTO = JSONObject.parseObject(property, new TypeReference<UserTaskExPropertyDTO>() {
            });
        }

        UserTaskNodeEventDTO userTaskNodeEventDTO = exPropertyDTO.getUserTaskNodeEventDTO();
        //目前只有发起节点才有结束事件
       Boolean flag= userTaskNodeEventDTO.getHasStartActivityEndEventEnable()!=null ? userTaskNodeEventDTO.getHasStartActivityEndEventEnable():false;
       if(flag){
           String startActivityEndEventInterfaceType = userTaskNodeEventDTO.getStartActivityEndEventInterfaceType();
           String startActivityEndEventURL = userTaskNodeEventDTO.getStartActivityEndEventURL();
           HttpUtil.httpRequestAll(startActivityEndEventURL, "GET", null);
       }
        log.info("任务完成监听器触发.."+activitiEvent.getClass());
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}
