package cn.axzo.workflow.core.engine.listener;

import cn.axzo.workflow.core.common.context.NoticeOperationContext;
import cn.axzo.workflow.core.engine.event.MessagePushEvent;
import cn.axzo.workflow.core.engine.event.MessagePushEventType;
import cn.axzo.workflow.core.listener.BpmnMessagePushEventListener;
import com.google.common.collect.ImmutableSet;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.AbstractFlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static cn.axzo.workflow.core.engine.event.MessagePushEventType.CARBON_COPY;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.CARBON_COPY_COMPLETE;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.NOTICE;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.PENDING_COMPLETE;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.PENDING_PUSH;
import static cn.axzo.workflow.core.engine.event.MessagePushEventType.SMS;

/**
 * 消息推送事件
 *
 * @author wangli
 * @since 2023/11/22 11:48
 */
@Slf4j
@Component
public class EngineNoticeEventListener extends AbstractFlowableEventListener {

    @Resource
    ObjectProvider<List<BpmnMessagePushEventListener>> processEventListeners;

    public static final Set<MessagePushEventType> ACCEPT_EVENTS =
            ImmutableSet.<MessagePushEventType>builder()
                    .add(NOTICE)
                    .add(PENDING_PUSH)
                    .add(PENDING_COMPLETE)
                    .add(CARBON_COPY)
                    .add(CARBON_COPY_COMPLETE)
                    .add(SMS)
                    .build();

    @Override
    public void onEvent(FlowableEvent flowableEvent) {
        StopWatch stopWatch = new StopWatch("EngineNoticeEventListener");

        if (flowableEvent instanceof MessagePushEvent) {
            MessagePushEvent event = (MessagePushEvent) flowableEvent;
            MessagePushEventType pushEventType = (MessagePushEventType) flowableEvent.getType();

            if (ACCEPT_EVENTS.contains(pushEventType)) {
                switch (pushEventType) {
                    case NOTICE:
                        stopWatch.start("PUSH-NOTICE Event Execution Time");
                        getOrderedListeners().forEach(i -> i.onNotice(event));
                        stopWatch.stop();
                        log.info("PUSH-NOTICE StopWatch : running time = " + stopWatch.getTotalTimeSeconds() + " 's");
                        break;
                    case PENDING_PUSH:
                        stopWatch.start("PUSH-PENDING Event Execution Time");
                        getOrderedListeners().forEach(i -> i.onPendingPush(event));
                        stopWatch.stop();
                        log.info("PUSH-PENDING StopWatch : running time = " + stopWatch.getTotalTimeSeconds() + " 's");
                        break;
                    case PENDING_COMPLETE:
                        stopWatch.start("PUSH-PENDING-COMPLETE Event Execution Time");
                        getOrderedListeners().forEach(i -> i.onPendingComplete(event));
                        stopWatch.stop();
                        log.info("PUSH-PENDING-COMPLETE StopWatch : running time = " + stopWatch.getTotalTimeSeconds() + " 's");
                        break;
                    case CARBON_COPY:
                        stopWatch.start("CARBON-COPY Event Execution Time");
                        getOrderedListeners().forEach(i -> i.onCarbonCopy(event));
                        stopWatch.stop();
                        log.info("CARBON-COPY StopWatch : running time = " + stopWatch.getTotalTimeSeconds() + " 's");
                        break;
                    case CARBON_COPY_COMPLETE:
                        stopWatch.start("CARBON-COPY-COMPLETE Event Execution Time");
                        getOrderedListeners().forEach(i -> i.onCarbonCopyComplete(event));
                        stopWatch.stop();
                        log.info("CARBON-COPY-COMPLETE StopWatch : running time = " + stopWatch.getTotalTimeSeconds() + " 's");
                        break;
                    case SMS:
                        stopWatch.start("SMS Event Execution Time");
                        getOrderedListeners().forEach(i -> i.onSms(event));
                        stopWatch.stop();
                        log.info("SMS StopWatch : running time = " + stopWatch.getTotalTimeSeconds() + " 's");
                        break;
                    default:
                }
            }
        }
    }

    private List<BpmnMessagePushEventListener> getOrderedListeners() {
        NoticeOperationContext context = new NoticeOperationContext();
        List<BpmnMessagePushEventListener> orderListeners = new ArrayList<>();
        processEventListeners.ifAvailable(orderListeners::addAll);
        orderListeners.forEach(i -> i.setContext(context));
        return orderListeners;
    }


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

}
