/* gstreamermm - a C++ wrapper for gstreamer
 *
 * Copyright 2008 The gstreamermm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gst/gstenumtypes.h>
#include <gst/gstinterface.h>
#include <gst/gstutils.h>
#include <gstreamermm/bus.h>
#include <gstreamermm/caps.h>
#include <gstreamermm/elementfactory.h>
#include <gstreamermm/event.h>
#include <gstreamermm/index.h>
#include <gstreamermm/pad.h>
#include <gstreamermm/padtemplate.h>
#include <gstreamermm/query.h>
#include <gstreamermm/taglist.h>
#include <gstreamermm/iterator.h>
#include <gstreamermm/handle_error.h>

_PINCLUDE(gstreamermm/private/object_p.h)

namespace Gst
{

extern "C"
{

void ElementInterfaced_WeakNotify_gstreamermm_callback(void* data, GObject*)
{
  delete static_cast<Gst::Element*>(data);
}

} // extern "C"

namespace Enums
{

Glib::ustring get_name(State s)
{
  return gst_element_state_get_name(GstState(s));
}

Glib::ustring get_name(StateChangeReturn s)
{
  return gst_element_state_change_return_get_name(GstStateChangeReturn(s));
}

} //namespace Enums

Glib::RefPtr<Gst::Element> Element::link(const Glib::RefPtr<Gst::Element>& dest)
{
  if(!dest)
    gstreamermm_handle_error("Failed to link null element.");
    
  const bool result = gst_element_link(gobj(), Glib::unwrap(dest));

  if(result)
    return dest;
  else
  {
    gstreamermm_handle_error("failed to link: " + get_name() + "->" +
      dest->get_name());
    return Glib::RefPtr<Gst::Element>();
  }
}

Glib::RefPtr<Gst::Element> Element::link(const Glib::RefPtr<Gst::Element>& dest, const Glib::RefPtr<Gst::Caps>& filter)
{
  if(!dest)
    gstreamermm_handle_error("Failed to link null element.");
    
  const bool result = gst_element_link_filtered(gobj(), Glib::unwrap(dest),
    Glib::unwrap(filter));

  if(result)
    return dest;
  else
  {
    gstreamermm_handle_error("failed to link: " + get_name() + "->" +
      dest->get_name());
    return Glib::RefPtr<Gst::Element>();
  }
}

void Element::post_message(int code, int line, MessageType type,
  const Glib::QueryQuark& domain, const Glib::ustring& text,
  const Glib::ustring& debug, const Glib::ustring& file,
  const Glib::ustring& function)
{
  gchar* gst_text = g_strdup(text.c_str());
  gchar* gst_debug = g_strdup(debug.c_str());

  gst_element_message_full(gobj(), static_cast<GstMessageType>(type),
    domain, code, gst_text, gst_debug, file.c_str(), function.c_str(), line);
}

bool Element::query_position(Format& format) const
{
  return gst_element_query_position(const_cast<GstElement*>(gobj()), reinterpret_cast<GstFormat*>(&format), 0);
}

bool Element::query_duration(Format& format) const
{
  return gst_element_query_duration(const_cast<GstElement*>(gobj()), reinterpret_cast<GstFormat*>(&format), 0);
}

// This method is written manually because an extra ref is necessary. See
// gst_element_send_event() docs
bool Element::send_event(const Glib::RefPtr<Gst::Event>& event)
{
  event->reference();
  return gst_element_send_event(gobj(), Gst::unwrap(event));
}

gboolean Element_Class::set_clock_vfunc_callback(GstElement* self, GstClock* clock)
{
  Glib::ObjectBase *const obj_base = static_cast<Glib::ObjectBase*>(
      Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if(obj_base && obj_base->is_derived_())
  {
    CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
    if(obj) // This can be NULL during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        // Call the virtual member method, which derived classes might override.
        return static_cast<int>(obj->set_clock_vfunc(Glib::wrap(clock, true)
));
      }
      catch(...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }
  
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The original underlying C class).
  );

  // Call the original underlying C function:
  if(base && base->set_clock)
    return (*base->set_clock)(self, clock);


  // Return same value as gst_element_set_clock() (base class vfunc) returns
  // when base class has no set_clock() vfunc.
  return true;
}
bool Gst::Element::set_clock_vfunc(const Glib::RefPtr<Gst::Clock>& clock)
{
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_)) // Get the parent class of the object class (The original underlying C class).
  );

  if(base && base->set_clock)
    return (*base->set_clock)(gobj(),Glib::unwrap(clock));

  typedef bool RType;
  return RType();
}
const GstQueryType* Element_Class::get_query_types_vfunc_callback(GstElement* self)
{
  Glib::ObjectBase *const obj_base = static_cast<Glib::ObjectBase*>(
      Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if(obj_base && obj_base->is_derived_())
  {
    CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
    if(obj) // This can be NULL during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        // Call the virtual member method, which derived classes might override.
        return reinterpret_cast<const GstQueryType*>((obj->get_query_types_vfunc()).data());
      }
      catch(...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }
  
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The original underlying C class).
  );

  // Call the original underlying C function:
  if(base && base->get_query_types)
    return (*base->get_query_types)(self);


  typedef const GstQueryType* RType;
  return RType();
}
Glib::ArrayHandle<QueryType> Gst::Element::get_query_types_vfunc() const
{
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_)) // Get the parent class of the object class (The original underlying C class).
  );

  if(base && base->get_query_types)
    return Glib::ArrayHandle<QueryType>(reinterpret_cast<const QueryType*>((*base->get_query_types)(const_cast<GstElement*>(gobj()))), Glib::OWNERSHIP_SHALLOW);

  typedef Glib::ArrayHandle<QueryType> RType;
  return RType(0, 0, Glib::OWNERSHIP_NONE);
}

} //namespace Gst
