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

use ges_sys;
use glib;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib_sys;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use gst;
use std::boxed::Box as Box_;
use std::mem::transmute;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use std::ptr;
use Asset;
use BaseEffect;
use Container;
use Extractable;
#[cfg(any(feature = "v1_18", feature = "dox"))]
use FrameNumber;
use Layer;
use TimelineElement;
use Track;
use TrackElement;
use TrackType;

glib_wrapper! {
    pub struct Clip(Object<ges_sys::GESClip, ges_sys::GESClipClass, ClipClass>) @extends Container, TimelineElement, @implements Extractable;

    match fn {
        get_type => || ges_sys::ges_clip_get_type(),
    }
}

pub const NONE_CLIP: Option<&Clip> = None;

pub trait ClipExt: 'static {
    fn add_asset<P: IsA<Asset>>(&self, asset: &P) -> Result<TrackElement, glib::BoolError>;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn add_child_to_track<P: IsA<TrackElement>, Q: IsA<Track>>(
        &self,
        child: &P,
        track: &Q,
    ) -> Result<TrackElement, glib::Error>;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn add_top_effect<P: IsA<BaseEffect>>(&self, effect: &P, index: i32)
        -> Result<(), glib::Error>;

    fn find_track_element<P: IsA<Track>>(
        &self,
        track: Option<&P>,
        type_: glib::types::Type,
    ) -> Option<TrackElement>;

    fn find_track_elements<P: IsA<Track>>(
        &self,
        track: Option<&P>,
        track_type: TrackType,
        type_: glib::types::Type,
    ) -> Vec<TrackElement>;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_duration_limit(&self) -> gst::ClockTime;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_internal_time_from_timeline_time<P: IsA<TrackElement>>(
        &self,
        child: &P,
        timeline_time: gst::ClockTime,
    ) -> Result<gst::ClockTime, glib::Error>;

    fn get_layer(&self) -> Option<Layer>;

    fn get_supported_formats(&self) -> TrackType;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_timeline_time_from_internal_time<P: IsA<TrackElement>>(
        &self,
        child: &P,
        internal_time: gst::ClockTime,
    ) -> Result<gst::ClockTime, glib::Error>;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_timeline_time_from_source_frame(
        &self,
        frame_number: FrameNumber,
    ) -> Result<gst::ClockTime, glib::Error>;

    fn get_top_effect_index<P: IsA<BaseEffect>>(&self, effect: &P) -> i32;

    fn get_top_effect_position<P: IsA<BaseEffect>>(&self, effect: &P) -> i32;

    fn get_top_effects(&self) -> Vec<TrackElement>;

    fn move_to_layer<P: IsA<Layer>>(&self, layer: &P) -> Result<(), glib::error::BoolError>;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn move_to_layer_full<P: IsA<Layer>>(&self, layer: &P) -> Result<(), glib::Error>;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn remove_top_effect<P: IsA<BaseEffect>>(&self, effect: &P) -> Result<(), glib::Error>;

    fn set_supported_formats(&self, supportedformats: TrackType);

    fn set_top_effect_index<P: IsA<BaseEffect>>(
        &self,
        effect: &P,
        newindex: u32,
    ) -> Result<(), glib::error::BoolError>;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn set_top_effect_index_full<P: IsA<BaseEffect>>(
        &self,
        effect: &P,
        newindex: u32,
    ) -> Result<(), glib::Error>;

    fn set_top_effect_priority<P: IsA<BaseEffect>>(
        &self,
        effect: &P,
        newpriority: u32,
    ) -> Result<(), glib::error::BoolError>;

    fn split(&self, position: u64) -> Result<Clip, glib::BoolError>;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn split_full(&self, position: u64) -> Result<Option<Clip>, glib::Error>;

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn connect_property_duration_limit_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;

    fn connect_property_layer_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    fn connect_property_supported_formats_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<Clip>> ClipExt for O {
    fn add_asset<P: IsA<Asset>>(&self, asset: &P) -> Result<TrackElement, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_none(ges_sys::ges_clip_add_asset(
                self.as_ref().to_glib_none().0,
                asset.as_ref().to_glib_none().0,
            ))
            .ok_or_else(|| glib_bool_error!("Failed to add asset"))
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn add_child_to_track<P: IsA<TrackElement>, Q: IsA<Track>>(
        &self,
        child: &P,
        track: &Q,
    ) -> Result<TrackElement, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ges_sys::ges_clip_add_child_to_track(
                self.as_ref().to_glib_none().0,
                child.as_ref().to_glib_none().0,
                track.as_ref().to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_none(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn add_top_effect<P: IsA<BaseEffect>>(
        &self,
        effect: &P,
        index: i32,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = ges_sys::ges_clip_add_top_effect(
                self.as_ref().to_glib_none().0,
                effect.as_ref().to_glib_none().0,
                index,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn find_track_element<P: IsA<Track>>(
        &self,
        track: Option<&P>,
        type_: glib::types::Type,
    ) -> Option<TrackElement> {
        unsafe {
            from_glib_full(ges_sys::ges_clip_find_track_element(
                self.as_ref().to_glib_none().0,
                track.map(|p| p.as_ref()).to_glib_none().0,
                type_.to_glib(),
            ))
        }
    }

    fn find_track_elements<P: IsA<Track>>(
        &self,
        track: Option<&P>,
        track_type: TrackType,
        type_: glib::types::Type,
    ) -> Vec<TrackElement> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ges_sys::ges_clip_find_track_elements(
                self.as_ref().to_glib_none().0,
                track.map(|p| p.as_ref()).to_glib_none().0,
                track_type.to_glib(),
                type_.to_glib(),
            ))
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_duration_limit(&self) -> gst::ClockTime {
        unsafe {
            from_glib(ges_sys::ges_clip_get_duration_limit(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_internal_time_from_timeline_time<P: IsA<TrackElement>>(
        &self,
        child: &P,
        timeline_time: gst::ClockTime,
    ) -> Result<gst::ClockTime, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ges_sys::ges_clip_get_internal_time_from_timeline_time(
                self.as_ref().to_glib_none().0,
                child.as_ref().to_glib_none().0,
                timeline_time.to_glib(),
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn get_layer(&self) -> Option<Layer> {
        unsafe { from_glib_full(ges_sys::ges_clip_get_layer(self.as_ref().to_glib_none().0)) }
    }

    fn get_supported_formats(&self) -> TrackType {
        unsafe {
            from_glib(ges_sys::ges_clip_get_supported_formats(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_timeline_time_from_internal_time<P: IsA<TrackElement>>(
        &self,
        child: &P,
        internal_time: gst::ClockTime,
    ) -> Result<gst::ClockTime, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ges_sys::ges_clip_get_timeline_time_from_internal_time(
                self.as_ref().to_glib_none().0,
                child.as_ref().to_glib_none().0,
                internal_time.to_glib(),
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn get_timeline_time_from_source_frame(
        &self,
        frame_number: FrameNumber,
    ) -> Result<gst::ClockTime, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret = ges_sys::ges_clip_get_timeline_time_from_source_frame(
                self.as_ref().to_glib_none().0,
                frame_number,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn get_top_effect_index<P: IsA<BaseEffect>>(&self, effect: &P) -> i32 {
        unsafe {
            ges_sys::ges_clip_get_top_effect_index(
                self.as_ref().to_glib_none().0,
                effect.as_ref().to_glib_none().0,
            )
        }
    }

    fn get_top_effect_position<P: IsA<BaseEffect>>(&self, effect: &P) -> i32 {
        unsafe {
            ges_sys::ges_clip_get_top_effect_position(
                self.as_ref().to_glib_none().0,
                effect.as_ref().to_glib_none().0,
            )
        }
    }

    fn get_top_effects(&self) -> Vec<TrackElement> {
        unsafe {
            FromGlibPtrContainer::from_glib_full(ges_sys::ges_clip_get_top_effects(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn move_to_layer<P: IsA<Layer>>(&self, layer: &P) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib_result_from_gboolean!(
                ges_sys::ges_clip_move_to_layer(
                    self.as_ref().to_glib_none().0,
                    layer.as_ref().to_glib_none().0
                ),
                "Failed to move clip to specified layer"
            )
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn move_to_layer_full<P: IsA<Layer>>(&self, layer: &P) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = ges_sys::ges_clip_move_to_layer_full(
                self.as_ref().to_glib_none().0,
                layer.as_ref().to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn remove_top_effect<P: IsA<BaseEffect>>(&self, effect: &P) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = ges_sys::ges_clip_remove_top_effect(
                self.as_ref().to_glib_none().0,
                effect.as_ref().to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn set_supported_formats(&self, supportedformats: TrackType) {
        unsafe {
            ges_sys::ges_clip_set_supported_formats(
                self.as_ref().to_glib_none().0,
                supportedformats.to_glib(),
            );
        }
    }

    fn set_top_effect_index<P: IsA<BaseEffect>>(
        &self,
        effect: &P,
        newindex: u32,
    ) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib_result_from_gboolean!(
                ges_sys::ges_clip_set_top_effect_index(
                    self.as_ref().to_glib_none().0,
                    effect.as_ref().to_glib_none().0,
                    newindex
                ),
                "Failed to move effect"
            )
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn set_top_effect_index_full<P: IsA<BaseEffect>>(
        &self,
        effect: &P,
        newindex: u32,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = ges_sys::ges_clip_set_top_effect_index_full(
                self.as_ref().to_glib_none().0,
                effect.as_ref().to_glib_none().0,
                newindex,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn set_top_effect_priority<P: IsA<BaseEffect>>(
        &self,
        effect: &P,
        newpriority: u32,
    ) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib_result_from_gboolean!(
                ges_sys::ges_clip_set_top_effect_priority(
                    self.as_ref().to_glib_none().0,
                    effect.as_ref().to_glib_none().0,
                    newpriority
                ),
                "Failed to the set top effect priority"
            )
        }
    }

    fn split(&self, position: u64) -> Result<Clip, glib::BoolError> {
        unsafe {
            Option::<_>::from_glib_none(ges_sys::ges_clip_split(
                self.as_ref().to_glib_none().0,
                position,
            ))
            .ok_or_else(|| glib_bool_error!("Failed to split clip"))
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn split_full(&self, position: u64) -> Result<Option<Clip>, glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let ret =
                ges_sys::ges_clip_split_full(self.as_ref().to_glib_none().0, position, &mut error);
            if error.is_null() {
                Ok(from_glib_none(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[cfg(any(feature = "v1_18", feature = "dox"))]
    fn connect_property_duration_limit_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_duration_limit_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ges_sys::GESClip,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Clip>,
        {
            let f: &F = &*(f as *const F);
            f(&Clip::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::duration-limit\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_duration_limit_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_layer_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_layer_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ges_sys::GESClip,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Clip>,
        {
            let f: &F = &*(f as *const F);
            f(&Clip::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::layer\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_layer_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_property_supported_formats_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn notify_supported_formats_trampoline<P, F: Fn(&P) + 'static>(
            this: *mut ges_sys::GESClip,
            _param_spec: glib_sys::gpointer,
            f: glib_sys::gpointer,
        ) where
            P: IsA<Clip>,
        {
            let f: &F = &*(f as *const F);
            f(&Clip::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::supported-formats\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_supported_formats_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}
