//! # Events helper
//!
//! @author Pierre Hubert



use crate::controllers::{calls_controller, comments_controller, conversations_controller, notifications_controller, user_ws_controller};
use crate::data::api_client::APIClient;
use crate::data::comment::Comment;
use crate::data::conversation::ConvID;
use crate::data::conversation_message::ConversationMessage;
use crate::data::error::Res;
use crate::data::user::UserID;
use crate::data::user_ws_connection::UserWsConnection;

pub enum Event<'a> {
    /// Websocket of a user was closed
    ///
    /// This event is propagated BEFORE the removal of the connection from the list
    UserWsClosed(&'a UserWsConnection),

    /// Destroyed a login token
    DestroyedLoginToken(UserID, &'a APIClient),

    /// Updated the number of notifications of one of multiple users user
    UpdatedNotificationsNumber(&'a Vec<UserID>),

    /// Updated the number of unread conversations
    UpdatedNumberUnreadConversations(&'a Vec<UserID>),

    /// Created a new conversation message
    NewConversationMessage(&'a ConversationMessage),

    /// Updated conversation message
    UpdatedConversationMessage(&'a ConversationMessage),

    /// Deleted a conversation message
    DeleteConversationMessage(&'a ConversationMessage),

    /// Created a new comment
    NewComment(&'a Comment),

    /// Updated a comment
    UpdatedComment(&'a Comment),

    /// Deleted a comment
    DeletedComment(&'a Comment),

    /// Connection to RTC relay was closed
    ClosedRTCRelayWebSocket,

    /// User joined call
    UserJoinedCall(&'a ConvID, &'a UserID),

    /// User left call
    UserLeftCall(&'a ConvID, &'a UserID),

    /// No event
    None,
}

/// Propagate an event through the different components of the application
pub fn propagate_event(e: &Event) -> Res {
    conversations_controller::handle_event(e)?;
    comments_controller::handle_event(e)?;
    notifications_controller::handle_event(e)?;
    user_ws_controller::handle_event(e)?;
    calls_controller::handle_event(e)?;
    Ok(())
}