//! Use [`DittoLogger`] to control Ditto logging options.

use_prelude!();

use std::path::Path;

use tokio::sync::oneshot;

#[cfg(doc)]
use crate::error::{CoreApiErrorKind, ErrorKind};
use crate::{error::Result, prelude::*, utils::extension_traits::FfiResultIntoRustResult};

/// Type with free associated functions ("static methods") to customize the logging behavior from
/// Ditto and log messages with the Ditto logging infrastructure.
pub struct DittoLogger(::never_say_never::Never);

impl DittoLogger {
    /// Enable or disable logging.
    ///
    /// Logs exported through [`export_to_file()`] are not affected by this
    /// setting and will also include logs emitted while `enabled` is false.
    ///
    /// [`export_to_file()`]: DittoLogger::export_to_file()
    pub fn set_logging_enabled(enabled: bool) {
        ffi_sdk::ditto_logger_enabled(enabled)
    }

    /// Return true if logging is enabled.
    ///
    /// Logs exported through [`export_to_file()`] are not affected by this
    /// setting and will also include logs emitted while `enabled` is false.
    ///
    /// [`export_to_file()`]: DittoLogger::export_to_file()
    pub fn get_logging_enabled() -> bool {
        ffi_sdk::ditto_logger_enabled_get()
    }

    /// Represent whether or not emojis should be used as the log level indicator in the logs.
    pub fn get_emoji_log_level_headings_enabled() -> bool {
        ffi_sdk::ditto_logger_emoji_headings_enabled_get()
    }

    /// Set whether or not emojis should be used as the log level indicator in the logs.
    pub fn set_emoji_log_level_headings_enabled(enabled: bool) {
        ffi_sdk::ditto_logger_emoji_headings_enabled(enabled);
    }

    /// Get the current minimum log level.
    ///
    /// Logs exported through [`export_to_file()`] are not affected by this
    /// setting and include all logs at [`LogLevel::Warning`] and above.
    ///
    /// [`export_to_file()`]: DittoLogger::export_to_file()
    pub fn get_minimum_log_level() -> LogLevel {
        ffi_sdk::ditto_logger_minimum_log_level_get()
    }

    /// Set the current minimum log level.
    ///
    /// Logs exported through [`export_to_file()`] are not affected by this
    /// setting and include all logs at [`LogLevel::Warning`] and above.
    ///
    /// [`export_to_file()`]: DittoLogger::export_to_file()
    pub fn set_minimum_log_level(log_level: LogLevel) {
        ffi_sdk::ditto_logger_minimum_log_level(log_level);
    }
}

impl DittoLogger {
    /// Not part of the public API.
    #[doc(hidden)]
    pub fn __log_error(msg: impl Into<String>) {
        ::ffi_sdk::ditto_log(CLogLevel::Error, char_p::new(msg.into()).as_ref())
    }
}

/// [`DittoLogger::export_to_file()`] API.
impl DittoLogger {
    fn rx_export_to_file(file_path: &Path) -> oneshot::Receiver<Result<u64>> {
        let (tx, rx) = oneshot::channel();
        let mut tx = Some(tx);
        ffi_sdk::dittoffi_logger_try_export_to_file_async(
            char_p::new(file_path.to_str().expect("path to be UTF-8")).as_ref(),
            Box::new(move |ffi_result: ::ffi_sdk::FfiResult<u64>, _: Blocking| {
                tx.take()
                    .expect("completion callback to be called exactly once")
                    .send(ffi_result.into_rust_result().map_err(DittoError::from))
                    .ok();
            })
            .into(),
        );
        rx
    }

    /// Exports collected logs to a compressed and JSON-encoded file on the
    /// local file system.
    ///
    /// `DittoLogger` locally collects a limited amount of logs at the `LogLevel::Debug`
    /// level and above, periodically discarding old logs. The internal logger is
    /// always enabled and works independently of the `enabled` setting and the
    /// configured `minimum_log_level`. Its logs can be requested and downloaded
    /// from any peer that is active in a Ditto app using the portal's device
    /// dashboard. This method provides an alternative way of accessing those
    /// logs by exporting them to the local filesystem.
    ///
    /// The logs will be written as a gzip compressed file at the URL specified
    /// by the `file_path` parameter. When uncompressed, the file contains one
    /// JSON value per line with the oldest entry on the first line (JSON lines
    /// format).
    ///
    /// By default, Ditto limits the amount of logs it retains on disk to 15 MB
    /// and a maximum age of 15 days. Older logs are periodically discarded once
    /// one of these limits is reached.
    ///
    /// This method currently only exports logs from the most recently created
    /// Ditto instance, even when multiple instances are running in the same
    /// process.
    ///
    /// This method currently only exports logs from the most recently created
    /// Ditto instance when multiple instances are running in the same process.
    ///
    /// - **Parameter** `file_path`: the path of the file to write the logs to. The file must not
    ///   already exist, and the containing directory must exist. **It is recommended for the path
    ///   to have the `.jsonl.gz` file extension** but Ditto won't enforce it, nor correct it.
    ///
    /// - **Errors**: it can run into I/O errors when the file cannot be written to disk. Prevent
    ///   this by ensuring that no file exists at the provided path, all parent directories exist,
    ///   sufficient permissions are granted, and that the disk is not full.
    ///
    ///   More precisely, this "throws" a [`DittoError`] whose [`.kind()`][DittoError::kind()] is
    ///   that of an [`ErrorKind::CoreApi`], such as:
    ///
    ///     - [`CoreApiErrorKind::IoNotFound`]
    ///     - [`CoreApiErrorKind::IoPermissionDenied`]
    ///     - [`CoreApiErrorKind::IoAlreadyExists`]
    ///     - [`CoreApiErrorKind::IoOperationFailed`]
    ///
    /// - **Returns**: the number of bytes written to disk.
    ///
    /// # Example
    ///
    /// ```rust ,no_run
    /// use dittolive_ditto::{
    ///     error::{CoreApiErrorKind, ErrorKind},
    ///     prelude::*,
    /// };
    /// # use eprintln as error;
    ///
    /// # _ = async {
    /// let ditto: Ditto = /* construct at least one ditto instance first */
    /// # todo!();
    /// match DittoLogger::export_to_file("/some/path.jsonl.gz").await {
    ///     Ok(_bytes_written) => { /* … */ },
    ///     Err(err) => match err.kind() {
    ///         ErrorKind::CoreApi(CoreApiErrorKind::IoNotFound) => { /* … */ },
    ///         ErrorKind::CoreApi(CoreApiErrorKind::IoPermissionDenied) => { /* … */ },
    ///         ErrorKind::CoreApi(CoreApiErrorKind::IoAlreadyExists) => { /* … */ },
    ///         ErrorKind::CoreApi(CoreApiErrorKind::IoOperationFailed) => { /* … */ },
    ///         _ => error!("{err}"),
    ///     },
    /// }
    /// # };
    /// ```
    pub async fn export_to_file(file_path: &(impl ?Sized + AsRef<Path>)) -> Result<u64> {
        Self::rx_export_to_file(file_path.as_ref())
            .await
            .expect("channel to be used by the FFI")
    }

    #[cfg(any(test, doctest))] // NOT exported yet until we settle on whether to Townhousify this API.
    /// Convenience function around [`Self::export_to_file()`], to be used when outside
    /// of `async`hronous contexts, by falling back to a blocking call.
    ///
    /// ### Panics
    ///
    /// This function may panic if called from within an asynchronous context.
    pub fn blocking_export_to_file(file_path: &(impl ?Sized + AsRef<Path>)) -> Result<u64> {
        Self::rx_export_to_file(file_path.as_ref())
            .blocking_recv()
            .expect("channel to be used by the FFI")
    }
}

#[cfg(any(test, doctest))]
#[path = "logger_tests.rs"]
mod tests;
