use std::cell::RefCell;

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

#[derive(CompositeTemplate, Properties, Default)]
#[properties(wrapper_type = super::CardDisplay)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/widgets/card_display/template.ui.xml")]
pub struct CardDisplay {
    #[template_child]
    pub image: TemplateChild<Image>,
    #[template_child]
    pub answer_entry: TemplateChild<Entry>,
    #[template_child]
    pub error_message: TemplateChild<Label>,
    #[template_child]
    pub incorrect_message: TemplateChild<Label>,
    #[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>,
}

#[glib::object_subclass]
impl ObjectSubclass for CardDisplay {
    const NAME: &'static str = "CardDisplay";
    type Type = super::CardDisplay;
    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 CardDisplay {
    fn constructed(&self) {
        self.parent_constructed();
        let layout_manager: BoxLayout = BoxLayout::new(gtk::Orientation::Vertical);

        self.obj().set_layout_manager(Some(layout_manager));
        self.image.set_width_request(256);
        self.image.set_height_request(256);
    }

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

impl WidgetImpl for CardDisplay {}
