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

import com.google.common.collect.ImmutableSet;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.AbstractFlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.ENTITY_ACTIVATED;
import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.ENTITY_CREATED;
import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.ENTITY_DELETED;
import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.ENTITY_INITIALIZED;
import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.ENTITY_SUSPENDED;
import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.ENTITY_UPDATED;

/**
 * 引擎内的 Entity 事件监听处理
 *
 * @author wangli
 * @since 2024-09-02 15:34
 */
@Component
@Slf4j
@AllArgsConstructor
public class EngineEntityEventListener extends AbstractFlowableEventListener {

    private final List<EntityEventHandle> handles;
    public static final Set<FlowableEngineEventType> SUPPORTED =
            ImmutableSet.<FlowableEngineEventType>builder()
                    .add(ENTITY_CREATED)
                    .add(ENTITY_INITIALIZED)
                    .add(ENTITY_UPDATED)
                    .add(ENTITY_DELETED)
                    .add(ENTITY_SUSPENDED)
                    .add(ENTITY_ACTIVATED)
                    .build();

    @Override
    public void onEvent(FlowableEvent event) {
        if (event instanceof FlowableEntityEvent && SUPPORTED.contains(event.getType())) {
            FlowableEntityEvent entityEvent = (FlowableEntityEvent) event;
            handles.forEach(handle -> {
                Object entity = entityEvent.getEntity();
                if (handle.support(entity)) {
                    Object convert = handle.convert(entity);
                    if (Objects.equals(event.getType(), ENTITY_CREATED)) {
                        handle.onCreate(convert);
                    } else if (Objects.equals(event.getType(), ENTITY_INITIALIZED)) {
                        handle.onInitialized(convert);
                    } else if (Objects.equals(event.getType(), ENTITY_UPDATED)) {
                        handle.onUpdated(convert);
                    } else if (Objects.equals(event.getType(), ENTITY_DELETED)) {
                        handle.onDeleted(convert);
                    } else if (Objects.equals(event.getType(), ENTITY_SUSPENDED)) {
                        handle.onSuspended(convert);
                    } else if (Objects.equals(event.getType(), ENTITY_ACTIVATED)) {
                        handle.onActivated(convert);
                    }
                }
            });
        }
    }


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