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

#[derive(Default)]
pub struct GamePreviewPrivate {
    date: gtk::Label,
    title: gtk::Label,
    black_player: gtk::Label,
    white_player: gtk::Label,
    result: gtk::Label,
}

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

impl ObjectImpl for GamePreviewPrivate {}
impl WidgetImpl for GamePreviewPrivate {}
impl BoxImpl for GamePreviewPrivate {}

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

impl Default for GamePreview {
    fn default() -> Self {
        let s: Self = Object::builder().build();
        s.set_orientation(gtk::Orientation::Horizontal);
        s.set_homogeneous(true);
        s.set_hexpand(false);

        s.append(&s.imp().date);
        s.append(&s.imp().title);
        s.append(&s.imp().black_player);
        s.append(&s.imp().white_player);
        s.append(&s.imp().result);

        s
    }
}

impl GamePreview {
    pub fn set_game(&self, element: GamePreviewElement) {
        self.imp().black_player.set_text(&element.black_player);
        self.imp().white_player.set_text(&element.white_player);
        self.imp().title.set_text(&element.name);
        self.imp().date.set_text(&element.date);
        self.imp().result.set_text(&element.result);
    }
}
