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

use crate::{ffi, SearchResult};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    #[doc(alias = "PpsSearchContext")]
    pub struct SearchContext(Object<ffi::PpsSearchContext, ffi::PpsSearchContextClass>);

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

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

    #[doc(alias = "pps_search_context_new")]
    pub fn new(model: &papers_view::DocumentModel) -> SearchContext {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::pps_search_context_new(model.to_glib_none().0)) }
    }

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

impl Default for SearchContext {
    fn default() -> Self {
        glib::object::Object::new::<Self>()
    }
}

// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`SearchContext`] 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 SearchContextBuilder {
    builder: glib::object::ObjectBuilder<'static, SearchContext>,
}

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

    pub fn document_model(self, document_model: &papers_view::DocumentModel) -> Self {
        Self {
            builder: self
                .builder
                .property("document-model", document_model.clone()),
        }
    }

    pub fn search_term(self, search_term: impl Into<glib::GString>) -> Self {
        Self {
            builder: self.builder.property("search-term", search_term.into()),
        }
    }

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

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::SearchContext>> Sealed for T {}
}

pub trait SearchContextExt: IsA<SearchContext> + sealed::Sealed + 'static {
    #[doc(alias = "pps_search_context_get_options")]
    #[doc(alias = "get_options")]
    fn options(&self) -> papers_document::FindOptions {
        unsafe {
            from_glib(ffi::pps_search_context_get_options(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "pps_search_context_get_result_model")]
    #[doc(alias = "get_result_model")]
    fn result_model(&self) -> Option<gio::ListModel> {
        unsafe {
            from_glib_none(ffi::pps_search_context_get_result_model(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "pps_search_context_get_search_term")]
    #[doc(alias = "get_search_term")]
    #[doc(alias = "search-term")]
    fn search_term(&self) -> Option<glib::GString> {
        unsafe {
            from_glib_none(ffi::pps_search_context_get_search_term(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "pps_search_context_restart")]
    fn restart(&self) {
        unsafe {
            ffi::pps_search_context_restart(self.as_ref().to_glib_none().0);
        }
    }

    #[doc(alias = "pps_search_context_select_result")]
    fn select_result(&self, result: &impl IsA<SearchResult>) {
        unsafe {
            ffi::pps_search_context_select_result(
                self.as_ref().to_glib_none().0,
                result.as_ref().to_glib_none().0,
            );
        }
    }

    #[doc(alias = "pps_search_context_set_options")]
    fn set_options(&self, options: papers_document::FindOptions) {
        unsafe {
            ffi::pps_search_context_set_options(
                self.as_ref().to_glib_none().0,
                options.into_glib(),
            );
        }
    }

    #[doc(alias = "pps_search_context_set_search_term")]
    #[doc(alias = "search-term")]
    fn set_search_term(&self, search_term: &str) {
        unsafe {
            ffi::pps_search_context_set_search_term(
                self.as_ref().to_glib_none().0,
                search_term.to_glib_none().0,
            );
        }
    }

    #[doc(alias = "cleared")]
    fn connect_cleared<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn cleared_trampoline<P: IsA<SearchContext>, F: Fn(&P) + 'static>(
            this: *mut ffi::PpsSearchContext,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(SearchContext::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"cleared\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    cleared_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "finished")]
    fn connect_finished<F: Fn(&Self, &papers_view::JobFind, i32) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn finished_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P, &papers_view::JobFind, i32) + 'static,
        >(
            this: *mut ffi::PpsSearchContext,
            object: *mut papers_view::ffi::PpsJobFind,
            p0: libc::c_int,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                SearchContext::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(object),
                p0,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"finished\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    finished_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "result-activated")]
    fn connect_result_activated<F: Fn(&Self, i32, i32) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn result_activated_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P, i32, i32) + 'static,
        >(
            this: *mut ffi::PpsSearchContext,
            object: libc::c_int,
            p0: libc::c_int,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                SearchContext::from_glib_borrow(this).unsafe_cast_ref(),
                object,
                p0,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"result-activated\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    result_activated_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "started")]
    fn connect_started<F: Fn(&Self, &papers_view::JobFind) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn started_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P, &papers_view::JobFind) + 'static,
        >(
            this: *mut ffi::PpsSearchContext,
            object: *mut papers_view::ffi::PpsJobFind,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                SearchContext::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(object),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"started\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    started_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "search-term")]
    fn connect_search_term_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_search_term_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::PpsSearchContext,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(SearchContext::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::search-term\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_search_term_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<SearchContext>> SearchContextExt for O {}
