//! # Monitor disk usage on a device
//!
//! In order to persist data on a device, Ditto can create a local database in a folder specified
//! with [`DittoBuilder::with_root`](crate::prelude::DittoBuilder::with_root).
//!
//! To monitor this folder size, [`DiskUsage`] offers two methods:
//!   - [`exec`](DiskUsage::exec) to retrieve an immediate `DiskUsageTree` and
//!   - [`observe`](DiskUsage::observe) to call a callback every time changes occurs in the Ditto
//!     folder.

use_prelude!();
use core::ffi::c_void;
use std::sync::{Arc, Mutex, Weak};

use ffi_sdk::{BoxedDitto, FsComponent};
use serde::Deserialize;

#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)]
/// Existing type in most file system.
pub enum FileSystemType {
    Directory = 0,
    File,
    SymLink,
}

#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
/// Entry in a file system
pub struct DiskUsageChild {
    /// Type of the entry.
    pub fs_type: FileSystemType,
    /// Relative path in the [`DittoRoot`](crate::fs::DittoRoot).
    pub path: String,
    /// Size of the entry.
    pub size_in_bytes: usize,
    /// If the entry is a directory, it can contain children.
    pub children: Option<Vec<DiskUsageChild>>,
}

// TODO(Ronan) : use trait alias when they become stable
// trait DiskUsageCallback = Fn(DiskUsageChild) + 'static + Send + Sync;
type DiskUsageObserverCtx = Arc<Mutex<DiskUsageObserver>>;

/// Handle returned by `DiskUsage::observe`.
/// Keep it in scope as long you want to receive updates on a given Ditto Subdirectory.
pub struct DiskUsageObserver {
    // Strong pointer to the inner callback
    // This will release the callback when dropped
    handle: Option<repr_c::Box<ffi_sdk::DiskUsageObserver>>,
    // callback
    on_update: Box<dyn FnMut(DiskUsageChild) + 'static + Send + Sync>,
}

impl DiskUsageObserver {
    pub(crate) fn new(on_update: impl FnMut(DiskUsageChild) + 'static + Send + Sync) -> Self {
        Self {
            on_update: Box::new(on_update),
            handle: None,
        }
    }

    pub(crate) fn set_handle(&mut self, handle: repr_c::Box<ffi_sdk::DiskUsageObserver>) {
        self.handle = Some(handle);
    }

    pub(crate) unsafe extern "C" fn on_event(ctx: *mut c_void, cbor: c_slice::Ref<'_, u8>) {
        let weak_ctx = Weak::from_raw(ctx as *const Mutex<DiskUsageObserver>);
        if let Some(strong_ctx) = weak_ctx.upgrade() {
            let tree = serde_cbor::from_slice(cbor.as_slice()).unwrap();
            (strong_ctx.lock().unwrap().on_update)(tree);
        }
        let _ = weak_ctx.into_raw();
    }
}

// TODO : after rebase on V3, add Observer trait to this

/// Used to monitor the disk usage of Ditto or of its sub-components.
pub struct DiskUsage {
    ditto: Arc<BoxedDitto>,
    component: FsComponent,
}

impl DiskUsage {
    /// Create a new DiskUsage for the given component.
    pub(crate) fn new(ditto: Arc<BoxedDitto>, component: FsComponent) -> Self {
        Self { ditto, component }
    }

    /// Return the tree representation of the ditto disk usage of the component
    pub fn exec(&self) -> DiskUsageChild {
        let cval = unsafe { ffi_sdk::ditto_disk_usage(&self.ditto, self.component) };
        serde_cbor::from_slice(cval.as_slice()).unwrap()
    }

    /// Register a callback to monitor the disk usage of the component
    pub fn observe(
        &self,
        callback: impl Fn(DiskUsageChild) + 'static + Send + Sync,
    ) -> DiskUsageObserverCtx {
        let observer = DiskUsageObserver::new(Box::new(callback));
        let observer = Arc::new(Mutex::new(observer));
        let weak_observer = Arc::downgrade(&observer);
        let raw_observer = weak_observer.into_raw() as *mut _;

        let handle = unsafe {
            ffi_sdk::ditto_register_disk_usage_callback(
                &self.ditto,
                self.component,
                raw_observer,
                None,
                None,
                Some(DiskUsageObserver::on_event),
            )
        };
        observer.lock().unwrap().set_handle(handle);
        observer
    }
}
