use adw::{prelude::*, subclass::prelude::*};
use glib::Object;
use gtk::glib;
// use otg_core::ui::GamePreviewElement;
use sgf::GameRecord;
use std::{cell::RefCell, rc::Rc};

#[derive(Default)]
pub struct GameObjectPrivate {
    game: Rc<RefCell<Option<GameRecord>>>,
}

#[glib::object_subclass]
impl ObjectSubclass for GameObjectPrivate {
    const NAME: &'static str = "GameObject";
    type Type = GameObject;
}

impl ObjectImpl for GameObjectPrivate {}

glib::wrapper! {
    pub struct GameObject(ObjectSubclass<GameObjectPrivate>);
}

impl GameObject {
    pub fn new(game: GameRecord) -> Self {
        let s: Self = Object::builder().build();
        *s.imp().game.borrow_mut() = Some(game);
        s
    }

    pub fn game(&self) -> Option<GameRecord> {
        self.imp().game.borrow().clone()
    }
}

pub struct LibraryPrivate {
    model: gio::ListStore,
    list_view: gtk::ColumnView,
}

impl Default for LibraryPrivate {
    fn default() -> Self {
        let vector: Vec<GameObject> = vec![];
        let model = gio::ListStore::new::<GameObject>();
        model.extend_from_slice(&vector);

        let selection_model = gtk::NoSelection::new(Some(model.clone()));
        let list_view = gtk::ColumnView::builder()
            .model(&selection_model)
            .single_click_activate(true)
            .hexpand(true)
            .build();

        fn make_factory<F>(bind: F) -> gtk::SignalListItemFactory
        where
            F: Fn(GameRecord) -> String + 'static,
        {
            let factory = gtk::SignalListItemFactory::new();
            factory.connect_setup(|_, list_item| {
                list_item
                    .downcast_ref::<gtk::ListItem>()
                    .unwrap()
                    .set_child(Some(
                        &gtk::Label::builder()
                            .halign(gtk::Align::Start)
                            .ellipsize(gtk::pango::EllipsizeMode::End)
                            .build(),
                    ))
            });
            factory.connect_bind(move |_, list_item| {
                let list_item = list_item.downcast_ref::<gtk::ListItem>().unwrap();
                let game = list_item.item().and_downcast::<GameObject>().unwrap();
                let preview = list_item.child().and_downcast::<gtk::Label>().unwrap();
                if let Some(game) = game.game() {
                    preview.set_text(&bind(game))
                }
            });
            factory
        }

        list_view.append_column(
            &gtk::ColumnViewColumn::builder()
                .title("date")
                .factory(&make_factory(|g| {
                    g.dates
                        .iter()
                        .map(|date| format!("{}", date))
                        .collect::<Vec<String>>()
                        .join(", ")
                }))
                .expand(true)
                .build(),
        );
        list_view.append_column(
            &gtk::ColumnViewColumn::builder()
                .title("game")
                .factory(&make_factory(|g| {
                    g.game_name.unwrap_or("Unnamed".to_owned())
                }))
                .expand(true)
                .build(),
        );
        list_view.append_column(
            &gtk::ColumnViewColumn::builder()
                .title("black")
                .factory(&make_factory(|g| {
                    g.black_player.name.unwrap_or("Black".to_owned())
                }))
                .expand(true)
                .build(),
        );
        list_view.append_column(
            &gtk::ColumnViewColumn::builder()
                .title("white")
                .factory(&make_factory(|g| {
                    g.white_player.name.unwrap_or("White".to_owned())
                }))
                .expand(true)
                .build(),
        );
        list_view.append_column(&gtk::ColumnViewColumn::new(
            Some("result"),
            Some(make_factory(|g| {
                g.result.map(|d| format!("{}", d)).unwrap_or("".to_owned())
            })),
        ));

        Self { model, list_view }
    }
}

#[glib::object_subclass]
impl ObjectSubclass for LibraryPrivate {
    const NAME: &'static str = "Library";
    type Type = Library;
    type ParentType = adw::Bin;
}

impl ObjectImpl for LibraryPrivate {}
impl WidgetImpl for LibraryPrivate {}
impl BinImpl for LibraryPrivate {}

glib::wrapper! {
    pub struct Library(ObjectSubclass<LibraryPrivate>) @extends adw::Bin, gtk::Widget;
}

impl Default for Library {
    fn default() -> Self {
        let s: Self = Object::builder().build();

        s.set_child(Some(&s.imp().list_view));
        s
    }
}

impl Library {
    pub fn new(on_select: impl Fn(GameRecord) + 'static) -> Library {
        let s = Library::default();

        s.imp().list_view.connect_activate({
            let s = s.clone();
            move |_, row_id| {
                println!("row activated: {}", row_id);

                let object = s.imp().model.item(row_id);
                let game = object.and_downcast_ref::<GameObject>().unwrap();
                println!(
                    "{:?} vs. {:?}",
                    game.game().unwrap().white_player,
                    game.game().unwrap().black_player
                );
                on_select(game.game().unwrap());
            }
        });

        s
    }

    pub fn set_games(&self, games: Vec<GameRecord>) {
        let games = games
            .into_iter()
            .map(GameObject::new)
            .collect::<Vec<GameObject>>();
        self.imp().model.extend_from_slice(&games);
    }
}
