use std::cell::RefCell;

use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Image};

#[derive(CompositeTemplate, Properties, Default)]
#[properties(wrapper_type = super::CardEntry)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/widgets/card_entry/template.ui.xml")]
pub struct CardEntry {
    #[template_child]
    pub image: TemplateChild<Image>,
    #[template_child]
    pub hieroglyph_label: TemplateChild<Label>,
    #[template_child]
    pub translation_label: TemplateChild<Label>,
    #[template_child]
    pub delete_button: TemplateChild<Button>,
    #[property(get, set)]
    pub imagepath: RefCell<String>,
    #[property(get, set)]
    hieroglyph: RefCell<String>,
    #[property(get, set)]
    translation: RefCell<String>,
}

#[glib::object_subclass]
impl ObjectSubclass for CardEntry {
    const NAME: &'static str = "CardEntry";
    type Type = super::CardEntry;
    type ParentType = gtk::Widget;

    fn class_init(klass: &mut Self::Class) {
        klass.bind_template();
        klass.set_layout_manager_type::<gtk::BoxLayout>();
    }

    fn instance_init(obj: &InitializingObject<Self>) {
        obj.init_template();
    }
}
#[glib::derived_properties]
impl ObjectImpl for CardEntry {
    fn constructed(&self) {
        self.parent_constructed();

        let hieroglyph_label_binding: &Label = self.hieroglyph_label.as_ref();
        self.obj()
            .bind_property("hieroglyph", hieroglyph_label_binding, "label")
            .sync_create()
            .build();

        let translation_label_binding: &Label = self.translation_label.as_ref();
        self.obj()
            .bind_property("translation", translation_label_binding, "label")
            .sync_create()
            .build();
    }

    fn dispose(&self) {
        self.dispose_template();
    }
}

impl WidgetImpl for CardEntry {}