// This file was generated by gir (https://github.com/gtk-rs/gir)
// from ../../girs
// from ../../gtk-girs
// DO NOT EDIT

use crate::{Connectivity, Primary, State, Wifi, Wired, ffi, nm};
use glib::{
    prelude::*,
    signal::{SignalHandlerId, connect_raw},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    ///
    ///
    /// ## Properties
    ///
    ///
    /// #### `client`
    ///  Readable | Writeable
    ///
    ///
    /// #### `wifi`
    ///  Readable | Writeable
    ///
    ///
    /// #### `wired`
    ///  Readable | Writeable
    ///
    ///
    /// #### `primary`
    ///  Readable | Writeable
    ///
    ///
    /// #### `connectivity`
    ///  Readable
    ///
    ///
    /// #### `state`
    ///  Readable
    ///
    /// # Implements
    ///
    /// [`NetworkExt`][trait@crate::prelude::NetworkExt]
    #[doc(alias = "AstalNetworkNetwork")]
    pub struct Network(Object<ffi::AstalNetworkNetwork, ffi::AstalNetworkNetworkClass>);

    match fn {
        type_ => || ffi::astal_network_network_get_type(),
    }
}

impl Network {
    pub const NONE: Option<&'static Network> = None;

    #[doc(alias = "astal_network_network_new")]
    pub fn new() -> Network {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::astal_network_network_new()) }
    }

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`Network`] objects.
    ///
    /// This method returns an instance of [`NetworkBuilder`](crate::builders::NetworkBuilder) which can be used to create [`Network`] objects.
    pub fn builder() -> NetworkBuilder {
        NetworkBuilder::new()
    }

    #[doc(alias = "astal_network_network_get_default")]
    #[doc(alias = "get_default")]
    #[allow(clippy::should_implement_trait)]
    pub fn default() -> Option<Network> {
        assert_initialized_main_thread!();
        unsafe {
            let net = ffi::astal_network_network_get_default();
            if net.is_null() {
                None
            } else {
                from_glib_full(net)
            }
        }
    }
}

impl Default for Network {
    fn default() -> Self {
        Self::new()
    }
}

// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`Network`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct NetworkBuilder {
    builder: glib::object::ObjectBuilder<'static, Network>,
}

impl NetworkBuilder {
    fn new() -> Self {
        Self {
            builder: glib::object::Object::builder(),
        }
    }

    pub fn client(self, client: &nm::Client) -> Self {
        Self {
            builder: self.builder.property("client", client.clone()),
        }
    }

    pub fn wifi(self, wifi: &impl IsA<Wifi>) -> Self {
        Self {
            builder: self.builder.property("wifi", wifi.clone().upcast()),
        }
    }

    pub fn wired(self, wired: &impl IsA<Wired>) -> Self {
        Self {
            builder: self.builder.property("wired", wired.clone().upcast()),
        }
    }

    pub fn primary(self, primary: Primary) -> Self {
        Self {
            builder: self.builder.property("primary", primary),
        }
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`Network`].
    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
    pub fn build(self) -> Network {
        assert_initialized_main_thread!();
        self.builder.build()
    }
}

/// Trait containing all [`struct@Network`] methods.
///
/// # Implementors
///
/// [`Network`][struct@crate::Network]
pub trait NetworkExt: IsA<Network> + 'static {
    #[doc(alias = "astal_network_network_get_client")]
    #[doc(alias = "get_client")]
    fn client(&self) -> nm::Client {
        unsafe {
            from_glib_none(ffi::astal_network_network_get_client(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "astal_network_network_get_wifi")]
    #[doc(alias = "get_wifi")]
    fn wifi(&self) -> Option<Wifi> {
        unsafe {
            from_glib_none(ffi::astal_network_network_get_wifi(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "astal_network_network_get_wired")]
    #[doc(alias = "get_wired")]
    fn wired(&self) -> Option<Wired> {
        unsafe {
            from_glib_none(ffi::astal_network_network_get_wired(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "astal_network_network_get_primary")]
    #[doc(alias = "get_primary")]
    fn primary(&self) -> Primary {
        unsafe {
            from_glib(ffi::astal_network_network_get_primary(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "astal_network_network_get_connectivity")]
    #[doc(alias = "get_connectivity")]
    fn connectivity(&self) -> Connectivity {
        unsafe {
            from_glib(ffi::astal_network_network_get_connectivity(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "astal_network_network_get_state")]
    #[doc(alias = "get_state")]
    fn state(&self) -> State {
        unsafe {
            from_glib(ffi::astal_network_network_get_state(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn set_client(&self, client: Option<&nm::Client>) {
        ObjectExt::set_property(self.as_ref(), "client", client)
    }

    fn set_wifi<P: IsA<Wifi>>(&self, wifi: Option<&P>) {
        ObjectExt::set_property(self.as_ref(), "wifi", wifi)
    }

    fn set_wired<P: IsA<Wired>>(&self, wired: Option<&P>) {
        ObjectExt::set_property(self.as_ref(), "wired", wired)
    }

    fn set_primary(&self, primary: Primary) {
        ObjectExt::set_property(self.as_ref(), "primary", primary)
    }

    #[doc(alias = "client")]
    fn connect_client_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_client_trampoline<P: IsA<Network>, F: Fn(&P) + 'static>(
            this: *mut ffi::AstalNetworkNetwork,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Network::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"notify::client".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_client_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "wifi")]
    fn connect_wifi_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_wifi_trampoline<P: IsA<Network>, F: Fn(&P) + 'static>(
            this: *mut ffi::AstalNetworkNetwork,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Network::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"notify::wifi".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_wifi_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "wired")]
    fn connect_wired_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_wired_trampoline<P: IsA<Network>, F: Fn(&P) + 'static>(
            this: *mut ffi::AstalNetworkNetwork,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Network::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"notify::wired".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_wired_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "primary")]
    fn connect_primary_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_primary_trampoline<P: IsA<Network>, F: Fn(&P) + 'static>(
            this: *mut ffi::AstalNetworkNetwork,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Network::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"notify::primary".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_primary_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "connectivity")]
    fn connect_connectivity_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_connectivity_trampoline<
            P: IsA<Network>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::AstalNetworkNetwork,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Network::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"notify::connectivity".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_connectivity_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "state")]
    fn connect_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_state_trampoline<P: IsA<Network>, F: Fn(&P) + 'static>(
            this: *mut ffi::AstalNetworkNetwork,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(Network::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"notify::state".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_state_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<Network>> NetworkExt for O {}
