package jnpf.cultivate.event;

import jnpf.model.cultivate.event.JnpfApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 事件处理程序
 *
 * @author fantaibao
 * @date 2023/12/26
 */
@Component
public class EventHandler {

    @Resource
    private List<JnpfApplicationEventService> jnpfApplicationEventServices;


    @EventListener(value = JnpfApplicationEvent.class)
    public <T> void courseEvent(JnpfApplicationEvent<T> jnpfApplicationEvent) {
        Object data = jnpfApplicationEvent.getData();
        // 事件处理分发
        Optional<JnpfApplicationEventService> first = jnpfApplicationEventServices.stream()
                .filter(t -> t.isSupportedCourseEvent(data)).findFirst();
        first.ifPresent(jnpfApplicationEventService -> jnpfApplicationEventService.handlerCourseEvent(data));
    }


}
