use std::{
    fmt::{Debug, Display},
    sync::Arc,
};

use getset::Getters;
use libntdsextract2::ObjectTreeEntry;

#[derive(Getters)]
#[getset(get = "pub")]
pub struct TreeEntry {
    entry: Arc<ObjectTreeEntry>,
}

impl TreeEntry {
    pub fn from(entry: Arc<ObjectTreeEntry>) -> Self {
        Self { entry }
    }
}

impl Display for TreeEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let is_deleted = self.entry.name().deleted_from_container().is_some();
        let display_name = self.entry.relative_distinguished_name();

        if is_deleted {
            write!(f, "{display_name} (DELETED)")
        } else {
            write!(f, "{display_name}")
        }
    }
}

impl Debug for TreeEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TreeEntry")
            .field("entry", &self.entry)
            .finish()
    }
}
