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

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

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

use rog_aura::{AuraEffect, KeyColourArray, LedBrightness};

const BLOCKING_TIME: u64 = 40; // 100ms = 10 FPS, max 50ms = 20 FPS, 40ms = 25 FPS

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

    /// PrevLedMode method
    fn prev_led_mode(&self) -> zbus::Result<()>;

    /// SetBrightness method
    fn set_brightness(&self, brightness: LedBrightness) -> zbus::Result<()>;

    /// SetLedMode method
    fn set_led_mode(&self, effect: &AuraEffect) -> zbus::Result<()>;

    /// NotifyLed signal
    /// NotifyLed signal
    #[dbus_proxy(signal)]
    fn notify_led(&self, data: &str) -> zbus::Result<()>;

    /// LedBrightness property
    #[dbus_proxy(property)]
    fn led_brightness(&self) -> zbus::Result<i16>;

    /// LedMode property
    #[dbus_proxy(property)]
    fn led_mode(&self) -> zbus::Result<String>;

    /// LedModes property
    #[dbus_proxy(property)]
    fn led_modes(&self) -> zbus::Result<String>;
}

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

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

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

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

    #[inline]
    pub fn set_led_brightness(&self, level: LedBrightness) -> Result<()> {
        self.0.set_brightness(level)?;
        Ok(())
    }

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

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

    #[inline]
    pub fn set_led_mode(&self, mode: &AuraEffect) -> Result<()> {
        self.0.set_led_mode(mode)
    }

    /// Write a single colour block.
    ///
    /// Intentionally blocks for 10ms after sending to allow the block to
    /// be written to the keyboard EC. This should not be async.
    #[inline]
    pub fn set_per_key(&self, key_colour_array: &KeyColourArray) -> Result<()> {
        let group = key_colour_array.get();
        let mut vecs = Vec::with_capacity(group.len());
        for v in group {
            vecs.push(v.to_vec());
        }
        // TODO: let mode = AuraModes::PerKey(vecs);
        // self.set_led_mode(&mode)?;

        std::thread::sleep(std::time::Duration::from_millis(BLOCKING_TIME));

        // if self.stop.load(Ordering::Relaxed) {
        //     println!("Keyboard backlight was changed, exiting");
        //     std::process::exit(1)
        // }
        Ok(())
    }

    /// This method must always be called before the very first write to initialise
    /// the keyboard LED EC in the correct mode
    #[inline]
    pub fn init_effect(&self) -> Result<()> {
        // TODO: let mode = AuraModes::PerKey(vec![vec![]]);
        // self.0.set_led_mode(&serde_json::to_string(&mode).unwrap())
        Ok(())
    }

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