//! Event tag
use super::Addr;
use std::fmt::{self, Display};

/// A tag that references another event
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Event {
    /// The other event to relate Id
    pub id: Addr,
    /// The relayer where the related content can be found
    pub relayer_url: Option<String>,
    /// <marker> is optional and if present is one of "reply", "root", or "mention".
    pub marker: Option<Marker>,
}

/// Marker as defined NIP-10
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Marker {
    /// For top level replies (those replying directly to the root event), only
    /// the "root" marker should be used
    Root,
    /// Denotes the id of the reply event being responded to
    Reply,
    /// Denotes a quoted or reposted event id.
    Mention,
    /// Unknown marker
    Unknown(String),
}

impl Display for Marker {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Root => "root",
                Self::Reply => "reply",
                Self::Mention => "mention",
                Self::Unknown(x) => x,
            }
        )
    }
}

impl From<&str> for Marker {
    fn from(marker: &str) -> Self {
        match marker.to_ascii_lowercase().as_str() {
            "root" => Self::Root,
            "reply" => Self::Reply,
            "mention" => Self::Mention,
            _ => Self::Unknown(marker.to_owned()),
        }
    }
}
