//! Reference https://freedesktop.org/wiki/Software/systemd/logind/
pub mod proxy;
pub mod types;

use proxy::{manager, session};
use types::logind::SessionInfo;
use zbus::{Connection, Result};

const DEFAULT_DEST: &str = "org.freedesktop.login1";

pub struct Logind<'a> {
    connection: Connection,
    logind_proxy: manager::ManagerProxy<'a>,
}

impl<'a> Logind<'a> {
    pub fn new() -> Result<Self> {
        let connection = Connection::new_system()?;
        let logind_proxy = manager::ManagerProxy::new(&connection)?;
        Ok(Self {
            connection,
            logind_proxy,
        })
    }

    /// Get the main manager interface
    pub fn manager(&self) -> &manager::ManagerProxy<'a> {
        &self.logind_proxy
    }

    /// Get a users session proxy from `SessionInfo`
    pub fn session(&self, session: &'a SessionInfo) -> session::SessionProxy<'a> {
        session::SessionProxy::new_for(&self.connection, DEFAULT_DEST, session.path()).unwrap()
    }
}
