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

use crate::{DocumentModel, UndoContext, UndoHandler, ffi};
use glib::{
    object::ObjectType as _,
    prelude::*,
    signal::{SignalHandlerId, connect_raw},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    #[doc(alias = "PpsAnnotationsContext")]
    pub struct AnnotationsContext(Object<ffi::PpsAnnotationsContext, ffi::PpsAnnotationsContextClass>) @implements UndoHandler;

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

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

    #[doc(alias = "pps_annotations_context_new")]
    pub fn new(model: &DocumentModel, undo_context: &UndoContext) -> AnnotationsContext {
        skip_assert_initialized!();
        unsafe {
            from_glib_full(ffi::pps_annotations_context_new(
                model.to_glib_none().0,
                undo_context.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "pps_annotations_context_register_ink_transformation")]
    pub fn register_ink_transformation<P: Fn(&papers_document::Annotation) + 'static>(
        transformation: P,
    ) {
        assert_initialized_main_thread!();
        let transformation_data: Box_<P> = Box_::new(transformation);
        unsafe extern "C" fn transformation_func<P: Fn(&papers_document::Annotation) + 'static>(
            ink_annotation: *mut papers_document::ffi::PpsAnnotation,
            user_data: glib::ffi::gpointer,
        ) {
            unsafe {
                let ink_annotation = from_glib_borrow(ink_annotation);
                let callback = &*(user_data as *mut P);
                (*callback)(&ink_annotation)
            }
        }
        let transformation = Some(transformation_func::<P> as _);
        let super_callback0: Box_<P> = transformation_data;
        unsafe {
            ffi::pps_annotations_context_register_ink_transformation(
                transformation,
                Box_::into_raw(super_callback0) as *mut _,
            );
        }
    }
}

pub trait AnnotationsContextExt: IsA<AnnotationsContext> + 'static {
    //#[cfg(feature = "v48")]
    //#[cfg_attr(docsrs, doc(cfg(feature = "v48")))]
    //#[doc(alias = "pps_annotations_context_add_annotation_sync")]
    //fn add_annotation_sync(&self, page_index: i32, type_: papers_document::AnnotationType, start: &papers_document::Point, end: &papers_document::Point, color: &gdk::RGBA, user_data: /*Unimplemented*/Option<Basic: Pointer>) -> Option<papers_document::Annotation> {
    //    unsafe { TODO: call ffi:pps_annotations_context_add_annotation_sync() }
    //}

    #[doc(alias = "pps_annotations_context_first_index_for_page")]
    fn first_index_for_page(&self, page: i32) -> i32 {
        unsafe {
            ffi::pps_annotations_context_first_index_for_page(self.as_ref().to_glib_none().0, page)
        }
    }

    #[cfg(feature = "v49")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v49")))]
    #[doc(alias = "pps_annotations_context_get_annot_at_doc_point")]
    #[doc(alias = "get_annot_at_doc_point")]
    fn annot_at_doc_point(
        &self,
        doc_point: &papers_document::DocumentPoint,
    ) -> Option<papers_document::Annotation> {
        unsafe {
            from_glib_none(ffi::pps_annotations_context_get_annot_at_doc_point(
                self.as_ref().to_glib_none().0,
                doc_point.to_glib_none().0,
            ))
        }
    }

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

    #[doc(alias = "pps_annotations_context_remove_annotation")]
    fn remove_annotation(&self, annot: &impl IsA<papers_document::Annotation>) {
        unsafe {
            ffi::pps_annotations_context_remove_annotation(
                self.as_ref().to_glib_none().0,
                annot.as_ref().to_glib_none().0,
            );
        }
    }

    #[doc(alias = "pps_annotations_context_set_color")]
    fn set_color(&self, color: &gdk::RGBA) {
        unsafe {
            ffi::pps_annotations_context_set_color(
                self.as_ref().to_glib_none().0,
                color.to_glib_none().0,
            );
        }
    }

    #[doc(alias = "annot-added")]
    fn connect_annot_added<F: Fn(&Self, &papers_document::Annotation) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn annot_added_trampoline<
            P: IsA<AnnotationsContext>,
            F: Fn(&P, &papers_document::Annotation) + 'static,
        >(
            this: *mut ffi::PpsAnnotationsContext,
            object: *mut papers_document::ffi::PpsAnnotation,
            f: glib::ffi::gpointer,
        ) {
            unsafe {
                let f: &F = &*(f as *const F);
                f(
                    AnnotationsContext::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 _,
                c"annot-added".as_ptr(),
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    annot_added_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "annot-removed")]
    fn connect_annot_removed<F: Fn(&Self, &papers_document::Annotation) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn annot_removed_trampoline<
            P: IsA<AnnotationsContext>,
            F: Fn(&P, &papers_document::Annotation) + 'static,
        >(
            this: *mut ffi::PpsAnnotationsContext,
            object: *mut papers_document::ffi::PpsAnnotation,
            f: glib::ffi::gpointer,
        ) {
            unsafe {
                let f: &F = &*(f as *const F);
                f(
                    AnnotationsContext::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 _,
                c"annot-removed".as_ptr(),
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    annot_removed_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

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

impl<O: IsA<AnnotationsContext>> AnnotationsContextExt for O {}
