use crate::resource_id::{ResourceId};

use std::net::{SocketAddr};

/// Information to identify the remote endpoint.
/// The endpoint is used mainly as a connection identified.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Endpoint {
    resource_id: ResourceId,
    addr: SocketAddr,
}

impl Endpoint {
    /// Creates a new Endpoint.
    pub(crate) fn new(resource_id: ResourceId, addr: SocketAddr) -> Self {
        Self { resource_id, addr }
    }

    /// Returns the inner network resource id used by this endpoint.
    /// It is not necessary to be unique for each endpoint if some of them shared the resource
    /// (an example of this is the different endpoints generated by when you listen by udp).
    pub fn resource_id(&self) -> ResourceId {
        self.resource_id
    }

    /// Returns the peer address of the endpoint.
    pub fn addr(&self) -> SocketAddr {
        self.addr
    }
}

impl std::fmt::Display for Endpoint {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}:{}", self.resource_id, self.addr)
    }
}
