use std::cell::RefCell;

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


#[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 reading_label: TemplateChild<Label>,
    #[template_child]
    pub translation_label: TemplateChild<Label>,
    #[template_child]
    pub edit_button: TemplateChild<Button>,
    #[template_child]
    pub delete_button: TemplateChild<Button>,
    #[template_child]
    pub is_learning_switch: TemplateChild<Switch>,
    #[property(get, set)]
    pub imagepath: RefCell<String>,
    #[property(get, set)]
    pub hieroglyph: RefCell<String>,
    #[property(get, set)]
    pub reading: RefCell<String>,
    #[property(get, set)]
    pub translation: RefCell<String>,
    #[property(get, set)]
    pub islearning: RefCell<bool>,
}

#[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 reading_label_binding: &Label = self.reading_label.as_ref();
        self.obj()
            .bind_property("reading", reading_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();

        let is_learning_switch_binding: &Switch = self.is_learning_switch.as_ref();
        self.obj()
            .bind_property("islearning", is_learning_switch_binding, "active")
            .sync_create()
            .build();
    }

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

impl WidgetImpl for CardEntry {}
