use {
    crate::{
        client::{CAP_FOREIGN_TOPLEVEL_GEOMETRY_TRACKING, Client, ClientCaps, ClientError},
        globals::{Global, GlobalName},
        ifs::{
            ext_foreign_toplevel_handle_v1::ExtForeignToplevelHandleV1,
            xx_foreign_toplevel_geometry_tracker_v1::XxForeignToplevelGeometryTrackerV1,
        },
        leaks::Tracker,
        object::{Object, Version},
        wire::{
            XxForeignToplevelGeometryTrackingManagerV1Id,
            xx_foreign_toplevel_geometry_tracking_manager_v1::*,
        },
    },
    std::rc::Rc,
    thiserror::Error,
};

pub struct XxForeignToplevelGeometryTrackingManagerV1Global {
    pub name: GlobalName,
}

impl XxForeignToplevelGeometryTrackingManagerV1Global {
    pub fn new(name: GlobalName) -> Self {
        Self { name }
    }

    fn bind_(
        self: Rc<Self>,
        id: XxForeignToplevelGeometryTrackingManagerV1Id,
        client: &Rc<Client>,
        version: Version,
    ) -> Result<(), XxForeignToplevelGeometryTrackingManagerV1Error> {
        let obj = Rc::new(XxForeignToplevelGeometryTrackingManagerV1 {
            id,
            client: client.clone(),
            tracker: Default::default(),
            version,
        });
        track!(client, obj);
        client.add_client_obj(&obj)?;
        Ok(())
    }
}

pub struct XxForeignToplevelGeometryTrackingManagerV1 {
    pub id: XxForeignToplevelGeometryTrackingManagerV1Id,
    pub client: Rc<Client>,
    pub tracker: Tracker<Self>,
    pub version: Version,
}

impl XxForeignToplevelGeometryTrackingManagerV1RequestHandler
    for XxForeignToplevelGeometryTrackingManagerV1
{
    type Error = XxForeignToplevelGeometryTrackingManagerV1Error;

    fn destroy(&self, _req: Destroy, _slf: &Rc<Self>) -> Result<(), Self::Error> {
        self.client.remove_obj(self)?;
        Ok(())
    }

    fn get_geometry_tracker(
        &self,
        req: GetGeometryTracker,
        _slf: &Rc<Self>,
    ) -> Result<(), Self::Error> {
        let handle: Rc<ExtForeignToplevelHandleV1> = self.client.lookup(req.toplevel)?;
        let toplevel = handle.toplevel.clone();
        let tracker_obj = Rc::new(XxForeignToplevelGeometryTrackerV1 {
            id: req.tracker,
            client: self.client.clone(),
            tracker: Default::default(),
            toplevel: toplevel.clone(),
            version: self.version,
        });
        track!(self.client, tracker_obj);
        self.client.add_client_obj(&tracker_obj)?;
        if let Some(tl) = toplevel.get() {
            let data = tl.tl_data();
            data.geometry_trackers
                .set((self.client.id, req.tracker), tracker_obj.clone());
            let rect = tl.node_absolute_position();
            tracker_obj.send_geometry_update(&rect, &data.state);
        } else {
            tracker_obj.send_finished();
        }
        Ok(())
    }
}

global_base!(
    XxForeignToplevelGeometryTrackingManagerV1Global,
    XxForeignToplevelGeometryTrackingManagerV1,
    XxForeignToplevelGeometryTrackingManagerV1Error
);

impl Global for XxForeignToplevelGeometryTrackingManagerV1Global {
    fn version(&self) -> u32 {
        1
    }

    fn required_caps(&self) -> ClientCaps {
        CAP_FOREIGN_TOPLEVEL_GEOMETRY_TRACKING
    }
}

simple_add_global!(XxForeignToplevelGeometryTrackingManagerV1Global);

object_base! {
    self = XxForeignToplevelGeometryTrackingManagerV1;
    version = self.version;
}

impl Object for XxForeignToplevelGeometryTrackingManagerV1 {}

simple_add_obj!(XxForeignToplevelGeometryTrackingManagerV1);

#[derive(Debug, Error)]
pub enum XxForeignToplevelGeometryTrackingManagerV1Error {
    #[error(transparent)]
    ClientError(Box<ClientError>),
}
efrom!(XxForeignToplevelGeometryTrackingManagerV1Error, ClientError);
