use {
    crate::{
        object::Version,
        utils::clonecell::CloneCell,
        wire::{XdgSurfaceId, xdg_surface::*},
        wl_usr::{UsrCon, usr_object::UsrObject},
    },
    std::{convert::Infallible, rc::Rc},
};

pub struct UsrXdgSurface {
    pub id: XdgSurfaceId,
    pub con: Rc<UsrCon>,
    pub owner: CloneCell<Option<Rc<dyn UsrXdgSurfaceOwner>>>,
    pub version: Version,
}

pub trait UsrXdgSurfaceOwner {
    fn configure(&self) {
        // nothing
    }
}

impl XdgSurfaceEventHandler for UsrXdgSurface {
    type Error = Infallible;

    fn configure(&self, ev: Configure, _slf: &Rc<Self>) -> Result<(), Self::Error> {
        self.con.request(AckConfigure {
            self_id: self.id,
            serial: ev.serial,
        });
        if let Some(owner) = self.owner.get() {
            owner.configure();
        }
        Ok(())
    }
}

usr_object_base! {
    self = UsrXdgSurface = XdgSurface;
    version = self.version;
}

impl UsrObject for UsrXdgSurface {
    fn destroy(&self) {
        self.con.request(Destroy { self_id: self.id })
    }

    fn break_loops(&self) {
        self.owner.take();
    }
}
