package com.mortals.flowable.listener;

import com.alibaba.fastjson.JSON;
import com.mortals.xhx.common.pdu.flow.FlowTaskNotifyPdu;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.impl.event.FlowableEntityEventImpl;
import org.flowable.engine.TaskService;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 全局监听-工作流待办消息提醒
 *
 * @author azhuzhu
 */
@Slf4j
//@Component
@RequiredArgsConstructor
//@EnableBinding(ProcessTaskSource.class)
public class TaskCreateListener implements FlowableEventListener {

    @Autowired
    private final TaskService taskService;
//    @Autowired
//    @SuppressWarnings("all")
//    private IMessageService messageService;
//
//    @Autowired
//    @SuppressWarnings("all")
//    private ProcessTaskSource processTaskSource;

    @Override
    public void onEvent(FlowableEvent flowableEvent) {
        log.info("任务创建事件全局监听!");
        TaskEntity taskEntity = (TaskEntity) ((FlowableEntityEventImpl) flowableEvent).getEntity();
        String taskId = taskEntity.getId();

        List<IdentityLink> idList = taskService.getIdentityLinksForTask(taskId);
        if (CollectionUtils.isEmpty(idList)) {
            log.info("idList is null!");
            return;
        }
        List<String> userNameList = new ArrayList<>();
        // 获取接收人，此处从Identity获取，实际情况会更复杂
        idList.forEach(identityLink -> {
            if (StringUtils.isNotBlank(identityLink.getUserId())) {
                userNameList.add(identityLink.getUserId());
            }
        });
        if (CollectionUtils.isNotEmpty(userNameList)) {
            // TODO: kafka发送提醒消息
            String tenantId = taskEntity.getTenantId();
            log.info(String.format("userNameList:%s,tenantId:%s", JSON.toJSONString(userNameList),tenantId));


            FlowTaskNotifyPdu flowTaskNotifyPdu = new FlowTaskNotifyPdu();
            flowTaskNotifyPdu.setUserNameList(userNameList);
            flowTaskNotifyPdu.setTaskId(taskEntity.getId());
            flowTaskNotifyPdu.setTaskName(taskEntity.getName());
            flowTaskNotifyPdu.setTenantId(tenantId);

            log.info("发送代办任务消息："+JSON.toJSONString(flowTaskNotifyPdu));
           // messageService.sendMessage(processTaskSource.output(), JSON.toJSONString(flowTaskNotifyPdu));

        }
    }

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

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

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