//! # 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/Profile' 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::IntrospectableProxy`]
//! * [`zbus::fdo::PeerProxy`]
//! * [`zbus::fdo::PropertiesProxy`]
//!
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.

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

use rog_types::profile::ProfileEvent;
use zbus::{dbus_proxy, Connection, Result};

#[dbus_proxy(
    interface = "org.asuslinux.Daemon",
    default_path = "/org/asuslinux/Profile"
)]
trait Daemon {
    /// ActiveProfileName method
    fn active_profile_name(&self) -> zbus::Result<String>;

    /// NextProfile method
    fn next_profile(&self) -> zbus::Result<()>;

    /// Profile method
    fn profile(&self) -> zbus::Result<String>;

    /// Profiles method
    fn profiles(&self) -> zbus::Result<String>;

    /// ProfileNames method
    fn profile_names(&self) -> zbus::Result<Vec<String>>;

    /// Remove method
    fn remove(&self, profile: &str) -> zbus::Result<()>;

    /// SetProfile method
    fn set_profile(&self, profile: &str) -> zbus::Result<()>;

    /// NotifyProfile signal
    #[dbus_proxy(signal)]
    fn notify_profile(&self, profile: &str) -> zbus::Result<()>;
}

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

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

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

    #[inline]
    pub fn active_profile_name(&self) -> Result<String> {
        self.0.active_profile_name()
    }

    #[inline]
    pub fn active_profile_data(&self) -> Result<String> {
        self.0.profile()
    }

    #[inline]
    pub fn all_profile_data(&self) -> Result<String> {
        self.0.profiles()
    }

    #[inline]
    pub fn next_fan(&self) -> Result<()> {
        self.0.next_profile()
    }

    #[inline]
    pub fn write_fan_mode(&self, level: u8) -> Result<()> {
        self.0
            .set_profile(&serde_json::to_string(&ProfileEvent::ChangeMode(level)).unwrap())
    }

    #[inline]
    pub fn write_command(&self, cmd: &ProfileEvent) -> Result<()> {
        self.0.set_profile(&serde_json::to_string(cmd).unwrap())
    }

    #[inline]
    pub fn profile_names(&self) -> Result<Vec<String>> {
        self.0.profile_names()
    }

    #[inline]
    pub fn remove(&self, profile: &str) -> Result<()> {
        self.0.remove(profile)
    }

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