use adw::{prelude::*, subclass::prelude::*};
use gettextrs::gettext;
use gtk::{glib, glib::clone, CompositeTemplate};

use crate::{session::model::Room, spawn, toast, utils::BoundObjectWeakRef, Window};

mod imp {
    use glib::subclass::InitializingObject;

    use super::*;

    #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
    #[template(
        resource = "/org/gnome/Fractal/ui/session/view/content/room_history/state_row/tombstone.ui"
    )]
    #[properties(wrapper_type = super::StateTombstone)]
    pub struct StateTombstone {
        #[template_child]
        pub new_room_btn: TemplateChild<gtk::Button>,
        /// The [`Room`] this event belongs to.
        #[property(get, set = Self::set_room, construct_only)]
        pub room: BoundObjectWeakRef<Room>,
    }

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

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
            Self::Type::bind_template_callbacks(klass);
        }

        fn instance_init(obj: &InitializingObject<Self>) {
            obj.init_template();
        }
    }

    #[glib::derived_properties]
    impl ObjectImpl for StateTombstone {}

    impl WidgetImpl for StateTombstone {}
    impl BinImpl for StateTombstone {}

    impl StateTombstone {
        /// Set the room this event belongs to.
        fn set_room(&self, room: Room) {
            let obj = self.obj();

            let successor_handler =
                room.connect_successor_id_string_notify(clone!(@weak self as imp => move |room| {
                    imp.new_room_btn.set_visible(room.successor_id().is_some());
                }));
            self.new_room_btn.set_visible(room.successor_id().is_some());

            let successor_room_handler =
                room.connect_successor_notify(clone!(@weak obj => move |room| {
                    obj.update_button_label(room);
                }));
            obj.update_button_label(&room);

            self.room
                .set(&room, vec![successor_handler, successor_room_handler]);
        }
    }
}

glib::wrapper! {
    /// A widget presenting a room tombstone state event.
    pub struct StateTombstone(ObjectSubclass<imp::StateTombstone>)
        @extends gtk::Widget, adw::Bin, @implements gtk::Accessible;
}

#[gtk::template_callbacks]
impl StateTombstone {
    /// Construct a new `StateTombstone` with the given room.
    pub fn new(room: &Room) -> Self {
        glib::Object::builder().property("room", room).build()
    }

    /// Update the button of the label.
    fn update_button_label(&self, room: &Room) {
        let button = &self.imp().new_room_btn;
        if room.successor().is_some() {
            // Translators: This is a verb, as in 'View Room'.
            button.set_label(&gettext("View"));
        } else {
            button.set_label(&gettext("Join"));
        }
    }

    /// Join or view the successor of this event's room.
    #[template_callback]
    fn join_or_view_successor(&self) {
        let Some(room) = self.room() else {
            return;
        };
        let Some(session) = room.session() else {
            return;
        };
        let room_list = session.room_list();

        // Join or view the room with the given identifier.
        if let Some(successor) = room.successor() {
            let Some(window) = self.root().and_downcast::<Window>() else {
                return;
            };

            window.session_view().select_room(Some(successor));
        } else if let Some(successor_id) = room.successor_id().map(ToOwned::to_owned) {
            spawn!(clone!(@weak self as obj, @weak room_list => async move {
                if let Err(error) = room_list.join_by_id_or_alias(successor_id.into(), vec![]).await {
                    toast!(obj, error);
                }
            }));
        }
    }
}
