use crate::geometry::{ContactEvent, IntersectionEvent};
use crossbeam::channel::Sender;

/// Trait implemented by structures responsible for handling events generated by the physics engine.
///
/// Implementors of this trait will typically collect these events for future processing.
pub trait EventHandler: Send + Sync {
    /// Handle an intersection event.
    ///
    /// A intersection event is emitted when the state of intersection between two colliders changes.
    fn handle_intersection_event(&self, event: IntersectionEvent);
    /// Handle a contact event.
    ///
    /// A contact event is emitted when two collider start or stop touching, independently from the
    /// number of contact points involved.
    fn handle_contact_event(&self, event: ContactEvent);
}

impl EventHandler for () {
    fn handle_intersection_event(&self, _event: IntersectionEvent) {}
    fn handle_contact_event(&self, _event: ContactEvent) {}
}

/// A physics event handler that collects events into a crossbeam channel.
pub struct ChannelEventCollector {
    intersection_event_sender: Sender<IntersectionEvent>,
    contact_event_sender: Sender<ContactEvent>,
}

impl ChannelEventCollector {
    /// Initialize a new physics event handler from crossbeam channel senders.
    pub fn new(
        intersection_event_sender: Sender<IntersectionEvent>,
        contact_event_sender: Sender<ContactEvent>,
    ) -> Self {
        Self {
            intersection_event_sender,
            contact_event_sender,
        }
    }
}

impl EventHandler for ChannelEventCollector {
    fn handle_intersection_event(&self, event: IntersectionEvent) {
        let _ = self.intersection_event_sender.send(event);
    }

    fn handle_contact_event(&self, event: ContactEvent) {
        let _ = self.contact_event_sender.send(event);
    }
}
