//! # DBus interface proxy for: `org.asuslinux.Gfx`
//!
//! This code was generated by `zbus-xmlgen` `1.0.0` from DBus introspection data.
//! Source: `Interface '/org/asuslinux/Gfx' 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::IntrospectableProxy`]
//! * [`zbus::fdo::PeerProxy`]
//!
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.

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

use rog_types::gfx_vendors::{GfxPower, GfxRequiredUserAction, GfxVendors};
use zbus::{dbus_proxy, Connection, Result};

#[dbus_proxy(
    interface = "org.asuslinux.Daemon",
    default_path = "/org/asuslinux/Gfx"
)]
trait Daemon {
    /// Power method
    fn power(&self) -> zbus::Result<GfxPower>;

    /// SetVendor method
    fn set_vendor(&self, vendor: &GfxVendors) -> zbus::Result<GfxRequiredUserAction>;

    /// Vendor method
    fn vendor(&self) -> zbus::Result<GfxVendors>;

    /// NotifyAction signal
    #[dbus_proxy(signal)]
    fn notify_action(&self, action: GfxRequiredUserAction) -> zbus::Result<()>;

    /// NotifyGfx signal
    #[dbus_proxy(signal)]
    fn notify_gfx(&self, vendor: GfxVendors) -> zbus::Result<()>;
}

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

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

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

    #[inline]
    pub fn gfx_get_pwr(&self) -> Result<GfxPower> {
        self.0.power()
    }

    #[inline]
    pub fn gfx_get_mode(&self) -> Result<GfxVendors> {
        self.0.vendor()
    }

    #[inline]
    pub fn gfx_write_mode(&self, vendor: &GfxVendors) -> Result<GfxRequiredUserAction> {
        self.0.set_vendor(vendor)
    }

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

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