use super::TlsAcceptorService;
use rama_core::Layer;
use rama_tls::server::TlsServerConfig;

/// A [`Layer`] which wraps the given service with a [`TlsAcceptorService`].
#[derive(Debug, Clone)]
pub struct TlsAcceptorLayer {
    config: TlsServerConfig,
    store_client_hello: bool,
}

impl TlsAcceptorLayer {
    /// Creates a new [`TlsAcceptorLayer`] using the given [`TlsServerConfig`].
    ///
    /// The acceptor config is resolved per-connection from this base config (see
    /// [`TlsAcceptorService`]).
    #[must_use]
    pub const fn new(config: TlsServerConfig) -> Self {
        Self {
            config,
            store_client_hello: false,
        }
    }

    rama_utils::macros::generate_set_and_with! {
        /// Set that the client hello should be stored
        pub fn store_client_hello(mut self, store: bool) -> Self {
            self.store_client_hello = store;
            self
        }
    }
}

impl<S> Layer<S> for TlsAcceptorLayer {
    type Service = TlsAcceptorService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        TlsAcceptorService::new(self.config.clone(), inner, self.store_client_hello)
    }

    fn into_layer(self, inner: S) -> Self::Service {
        TlsAcceptorService::new(self.config, inner, self.store_client_hello)
    }
}
