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

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

glib::wrapper! {
    #[doc(alias = "PpsUndoContext")]
    pub struct UndoContext(Object<ffi::PpsUndoContext, ffi::PpsUndoContextClass>);

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

impl UndoContext {
    #[doc(alias = "pps_undo_context_new")]
    pub fn new(document_model: &DocumentModel) -> UndoContext {
        skip_assert_initialized!();
        unsafe { from_glib_full(ffi::pps_undo_context_new(document_model.to_glib_none().0)) }
    }

    //#[doc(alias = "pps_undo_context_add_action")]
    //pub fn add_action(&self, handler: &impl IsA<UndoHandler>, data: /*Unimplemented*/Option<Basic: Pointer>) {
    //    unsafe { TODO: call ffi:pps_undo_context_add_action() }
    //}

    //#[doc(alias = "pps_undo_context_get_last_action")]
    //#[doc(alias = "get_last_action")]
    //pub fn last_action(&self) -> /*Unimplemented*/Option<Basic: Pointer> {
    //    unsafe { TODO: call ffi:pps_undo_context_get_last_action() }
    //}

    #[doc(alias = "pps_undo_context_get_last_handler")]
    #[doc(alias = "get_last_handler")]
    pub fn last_handler(&self) -> Option<UndoHandler> {
        unsafe {
            from_glib_none(ffi::pps_undo_context_get_last_handler(
                self.to_glib_none().0,
            ))
        }
    }

    #[doc(alias = "pps_undo_context_redo")]
    pub fn redo(&self) {
        unsafe {
            ffi::pps_undo_context_redo(self.to_glib_none().0);
        }
    }

    #[doc(alias = "pps_undo_context_undo")]
    pub fn undo(&self) {
        unsafe {
            ffi::pps_undo_context_undo(self.to_glib_none().0);
        }
    }

    #[doc(alias = "document-model")]
    pub fn document_model(&self) -> Option<DocumentModel> {
        ObjectExt::property(self, "document-model")
    }

    #[doc(alias = "stacks-changed")]
    pub fn connect_stacks_changed<F: Fn(&Self, &UndoHandler) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn stacks_changed_trampoline<
            F: Fn(&UndoContext, &UndoHandler) + 'static,
        >(
            this: *mut ffi::PpsUndoContext,
            handler: *mut ffi::PpsUndoHandler,
            f: glib::ffi::gpointer,
        ) {
            unsafe {
                let f: &F = &*(f as *const F);
                f(&from_glib_borrow(this), &from_glib_borrow(handler))
            }
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                c"stacks-changed".as_ptr(),
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    stacks_changed_trampoline::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}
