use std::path::PathBuf;

use crate::{
    NodeId, PixelCoordinate, PixelDimension,
    color::Color,
    node::{GenericNode, GenericNodeInfo},
};

/// A file node.
#[derive(
    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct FileNode {
    #[serde(flatten)]
    generic: GenericNode,
    file: PathBuf,
    #[serde(skip_serializing_if = "Option::is_none")]
    subpath: Option<String>,
}

impl FileNode {
    /// Creates a new [`FileNode`].
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub const fn new(
        id: NodeId,
        x: PixelCoordinate,
        y: PixelCoordinate,
        width: PixelDimension,
        height: PixelDimension,
        color: Option<Color>,
        file: PathBuf,
        subpath: Option<String>,
    ) -> Self {
        Self {
            generic: GenericNode::new(id, x, y, width, height, color),
            file,
            subpath,
        }
    }

    /// Returns a reference to the file of this [`FileNode`].
    #[must_use]
    pub const fn file(&self) -> &PathBuf {
        &self.file
    }

    /// Returns the subpath of this [`FileNode`].
    #[must_use]
    pub const fn subpath(&self) -> Option<&String> {
        self.subpath.as_ref()
    }
}

impl GenericNodeInfo for FileNode {
    fn id(&self) -> &NodeId {
        self.generic.id()
    }

    fn x(&self) -> PixelCoordinate {
        self.generic.x()
    }

    fn y(&self) -> PixelCoordinate {
        self.generic.y()
    }

    fn width(&self) -> PixelDimension {
        self.generic.width()
    }

    fn height(&self) -> PixelDimension {
        self.generic.height()
    }

    fn color(&self) -> &Option<Color> {
        self.generic.color()
    }
}
