use crate::{elements::Figure, widget_factory::WidgetBuilder};
use gtk4::{prelude::*, Align, Orientation, Widget};

impl WidgetBuilder {
    pub(super) fn create_figure(&self, figure: Figure) -> Widget {
        if let Some(caption) = figure.caption {
            let content = self.build_element(*figure.element, None, false);
            content.set_halign(Align::Center);

            let caption_widget = self.build_element(*caption, None, false);
            caption_widget.set_halign(Align::Center);

            let box_ = gtk4::Box::builder()
                .orientation(Orientation::Vertical)
                .spacing(10)
                .margin_top(6)
                .margin_bottom(6)
                .build();
            box_.append(&content);
            box_.append(&caption_widget);
            box_.upcast()
        } else {
            self.build_element(*figure.element, None, false)
        }
    }
}
