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

#[derive(Default)]
pub struct GamePreviewPrivate;

#[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 GamePreview {
    pub fn new(element: GamePreviewElement) -> GamePreview {
        let s: Self = Object::builder().build();
        s.set_orientation(gtk::Orientation::Horizontal);

        println!("game_preview: {:?}", element);
        let black_player = match element.black_rank {
            Some(rank) => format!("{} ({})", element.black_player, rank.to_string()),
            None => element.black_player,
        };
        let white_player = match element.white_rank {
            Some(rank) => format!("{} ({})", element.white_player, rank.to_string()),
            None => element.white_player,
        };
        s.append(&gtk::Label::new(Some(&black_player)));
        s.append(&gtk::Label::new(Some(&white_player)));

        s
    }
}
