//! # DBus interface proxy for: `org.asuslinux.Daemon`
//!
//! This code was generated by `zbus-xmlgen` `1.0.0` from DBus introspection data.
//! Source: `Interface '/org/asuslinux/Charge' from service 'org.asuslinux.Daemon' on system bus`.
//!
//! You may prefer to adapt it, instead of using it verbatim.
//!
//! More information can be found in the
//! [Writing a client proxy](https://zeenix.pages.freedesktop.org/zbus/client.html)
//! section of the zbus documentation.
//!
//! This DBus object implements
//! [standard DBus interfaces](https://dbus.freedesktop.org/doc/dbus-specification.html),
//! (`org.freedesktop.DBus.*`) for which the following zbus proxies can be used:
//!
//! * [`zbus::fdo::PropertiesProxy`]
//! * [`zbus::fdo::PeerProxy`]
//! * [`zbus::fdo::IntrospectableProxy`]
//!
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.

use std::sync::{Arc, Mutex};

use zbus::{dbus_proxy, Connection, Result};

#[dbus_proxy(
    interface = "org.asuslinux.Daemon",
    default_path = "/org/asuslinux/Charge"
)]
trait Daemon {
    /// Limit method
    fn limit(&self) -> zbus::Result<i16>;

    /// SetLimit method
    fn set_limit(&self, limit: u8) -> zbus::Result<()>;

    /// NotifyCharge signal
    #[dbus_proxy(signal)]
    fn notify_charge(&self, limit: u8) -> zbus::Result<()>;
}

pub struct ChargeProxy<'a>(DaemonProxy<'a>);

impl<'a> ChargeProxy<'a> {
    #[inline]
    pub fn new(conn: &Connection) -> Result<Self> {
        Ok(ChargeProxy(DaemonProxy::new(&conn)?))
    }

    pub fn proxy(&self) -> &DaemonProxy<'a> {
        &self.0
    }

    #[inline]
    pub fn write_limit(&self, level: u8) -> Result<()> {
        self.0.set_limit(level)
    }

    #[inline]
    pub fn get_limit(&self) -> Result<i16> {
        self.0.limit()
    }

    #[inline]
    pub fn connect_notify_charge(&self, charge: Arc<Mutex<Option<u8>>>) -> zbus::fdo::Result<()> {
        self.0.connect_notify_charge(move |data| {
            if let Ok(mut lock) = charge.lock() {
                *lock = Some(data);
            }
            Ok(())
        })
    }
}
