use std::borrow::Borrow;

use gdk::{glib::Object, prelude::Cast, subclass::prelude::ObjectSubclassIsExt};
use gtk::Widget;

use crate::backend::IntermediateLocation;

gtk::glib::wrapper! {
    pub struct IntermediateLocationItem(ObjectSubclass<imp::IntermediateLocationItem>)
        @extends gtk::Box, gtk::Widget,
        @implements gtk::gio::ActionGroup, gtk::gio::ActionMap, gtk::Accessible, gtk::Buildable,
            gtk::ConstraintTarget;
}

impl IntermediateLocationItem {
    pub fn new(intermediate_location: &IntermediateLocation) -> Self {
        Object::builder::<Self>()
            .property("intermediate-location", intermediate_location)
            .build()
    }

    pub(crate) fn arrival_label(&self) -> Widget {
        let obj = self.imp();
        obj.alt_label_arrival
            .borrow()
            .dynamic_cast_ref::<gtk::Widget>()
            .expect("AltLabel to be a Widget")
            .clone()
    }
}

pub mod imp {
    use std::cell::RefCell;

    use gdk::glib::Properties;
    use glib::subclass::InitializingObject;
    use gtk::CompositeTemplate;
    use gtk::DirectionType;
    use gtk::glib;
    use gtk::prelude::*;
    use gtk::subclass::prelude::*;

    use crate::backend::IntermediateLocation;
    use crate::backend::Place;
    use crate::gui::alt_label::AltLabel;
    use crate::gui::utility::Utility;

    #[derive(CompositeTemplate, Default, Properties)]
    #[properties(wrapper_type = super::IntermediateLocationItem)]
    #[template(resource = "/ui/intermediate_location_item.ui")]
    pub struct IntermediateLocationItem {
        #[template_child]
        pub(super) alt_label_arrival: TemplateChild<AltLabel>,

        #[property(get, set)]
        intermediate_location: RefCell<Option<IntermediateLocation>>,
    }

    impl IntermediateLocationItem {
        fn format_intermediate_location_description(
            stop: &str,
            arrival: &Option<String>,
            platform: &Option<String>,
        ) -> String {
            // Translators: The formatting of the intermediate_locations's description for screen readers. Do not translate the strings in {}.
            let format_full = gettextrs::gettext("{stop} at {arrival} on platform {platform}");

            // Translators: The formatting of the intermediate_locations's description for screen readers. Do not translate the strings in {}.
            let format_no_platform = gettextrs::gettext("{stop} at {arrival}");

            match (arrival, platform) {
                (Some(arrival), Some(platform)) => format_full
                    .replace("{stop}", stop)
                    .replace("{arrival}", arrival)
                    .replace("{platform}", platform),
                (Some(arrival), None) => format_no_platform
                    .replace("{stop}", stop)
                    .replace("{arrival}", arrival),
                (_, _) => stop.to_string(),
            }
        }
    }

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

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
            WidgetClassExt::set_css_name(klass, "IntermediateLocationItem");
        }

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

    #[glib::derived_properties]
    impl ObjectImpl for IntermediateLocationItem {
        fn constructed(&self) {
            self.parent_constructed();

            self.obj().connect_notify_local(
                Some("intermediate-location"),
                |intermediate_location_item, _| {
                    let intermediate_location = intermediate_location_item
                        .property::<IntermediateLocation>("intermediate_location");
                    let stop = intermediate_location.property::<Place>("place");

                    intermediate_location_item.update_property(&[
                        gtk::accessible::Property::Label(
                            &IntermediateLocationItem::format_intermediate_location_description(
                                &stop.name(),
                                &intermediate_location
                                    .arrival()
                                    .or(intermediate_location.planned_arrival()),
                                &intermediate_location
                                    .arrival_platform()
                                    .or(intermediate_location.planned_arrival_platform()),
                            ),
                        ),
                    ]);
                },
            );
        }
    }

    impl WidgetImpl for IntermediateLocationItem {
        fn focus(&self, direction: DirectionType) -> bool {
            Utility::move_focus_within_container(self, direction)
        }
    }

    impl BoxImpl for IntermediateLocationItem {}
}
