use super::on_close::OnClose;
use futures::{channel, Stream, StreamExt};
use std::sync::Arc;

/// Receive messages out of a connection
pub struct Receiver {
    pub(super) inner: channel::mpsc::UnboundedReceiver<Result<RecvMessage, RecvError>>,
    pub(super) closer: Arc<OnClose>,
}

#[derive(thiserror::Error, Debug)]
pub enum RecvError {
    #[error("Text message contains invalid UTF8: {0}")]
    InvalidUtf8(#[from] std::string::FromUtf8Error),
    #[error("Stream finished")]
    StreamFinished,
    #[error("Failed to send close message")]
    CloseError,
}

impl Receiver {
    /// Ask the underlying Websocket connection to close.
    pub async fn close(&mut self) -> Result<(), RecvError> {
        self.closer.0.send(()).map_err(|_| RecvError::CloseError)?;
        Ok(())
    }
}

impl Stream for Receiver {
    type Item = Result<RecvMessage, RecvError>;
    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        self.inner.poll_next_unpin(cx).map_err(|e| e.into())
    }
}

/// A message that can be received from the channel interface
#[derive(Debug, Clone)]
pub enum RecvMessage {
    /// Send an owned string into the socket.
    Text(String),
    /// Send owned bytes into the socket.
    Binary(Vec<u8>),
}

impl RecvMessage {
    pub fn len(&self) -> usize {
        match self {
            RecvMessage::Binary(b) => b.len(),
            RecvMessage::Text(s) => s.len(),
        }
    }
}
