use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};

#[derive(Default)]
pub struct LabelPrivate {
    label: gtk::Label,
    icon: gtk::Image,
}

#[glib::object_subclass]
impl ObjectSubclass for LabelPrivate {
    const NAME: &'static str = "Label";
    type Type = Label;
    type ParentType = gtk::Box;
}

impl ObjectImpl for LabelPrivate {}
impl WidgetImpl for LabelPrivate {}
impl BoxImpl for LabelPrivate {}

glib::wrapper! {
    pub struct Label(ObjectSubclass<LabelPrivate>) @extends gtk::Box, gtk::Widget,
        @implements gtk::Orientable;
}

impl Label {
    pub fn new(text: Option<&str>, icon: Option<gio::ThemedIcon>) -> Self {
        let s: Self = Object::builder().build();
        s.set_orientation(gtk::Orientation::Horizontal);
        s.set_spacing(8);
        s.set_margin_bottom(8);
        s.set_margin_top(8);
        s.set_margin_start(8);
        s.set_margin_end(8);

        s.append(&s.imp().icon);
        s.append(&s.imp().label);

        if let Some(text) = text {
            s.set_text(text);
        }

        if let Some(icon) = icon {
            s.set_icon(icon);
        }

        s
    }

    pub fn set_text(&self, text: &str) {
        self.imp().label.set_text(text);
    }

    pub fn set_icon(&self, icon: gio::ThemedIcon) {
        self.imp().icon.set_from_gicon(&icon);
    }
}
