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

/// A text node.
#[derive(
    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct TextNode {
    #[serde(flatten)]
    generic: GenericNode,
    text: String,
}

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

    /// Returns a reference to the text of this [`TextNode`].
    #[must_use]
    pub const fn text(&self) -> &str {
        self.text.as_str()
    }
}

impl GenericNodeInfo for TextNode {
    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()
    }
}
