/* audio_player.c generated by valac 0.56.19, the Vala compiler
 * generated from audio_player.vala, do not modify */

#include <gtk/gtk.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <pango/pango.h>
#include <float.h>
#include <math.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define DC_TYPE_AUDIO_PLAYER (dc_audio_player_get_type ())
#define DC_AUDIO_PLAYER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DC_TYPE_AUDIO_PLAYER, DcAudioPlayer))
#define DC_AUDIO_PLAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DC_TYPE_AUDIO_PLAYER, DcAudioPlayerClass))
#define DC_IS_AUDIO_PLAYER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DC_TYPE_AUDIO_PLAYER))
#define DC_IS_AUDIO_PLAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DC_TYPE_AUDIO_PLAYER))
#define DC_AUDIO_PLAYER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DC_TYPE_AUDIO_PLAYER, DcAudioPlayerClass))

typedef struct _DcAudioPlayer DcAudioPlayer;
typedef struct _DcAudioPlayerClass DcAudioPlayerClass;
typedef struct _DcAudioPlayerPrivate DcAudioPlayerPrivate;
enum  {
	DC_AUDIO_PLAYER_0_PROPERTY,
	DC_AUDIO_PLAYER_NUM_PROPERTIES
};
static GParamSpec* dc_audio_player_properties[DC_AUDIO_PLAYER_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _DcAudioPlayer {
	GtkBox parent_instance;
	DcAudioPlayerPrivate * priv;
};

struct _DcAudioPlayerClass {
	GtkBoxClass parent_class;
};

struct _DcAudioPlayerPrivate {
	gchar* path;
	GtkButton* btn;
	GtkOverlay* host;
	GtkVideo* engine;
};

static gint DcAudioPlayer_private_offset;
static gpointer dc_audio_player_parent_class = NULL;

VALA_EXTERN GType dc_audio_player_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (DcAudioPlayer, g_object_unref)
VALA_EXTERN DcAudioPlayer* dc_audio_player_new (const gchar* path,
                                    const gchar* name);
VALA_EXTERN DcAudioPlayer* dc_audio_player_construct (GType object_type,
                                          const gchar* path,
                                          const gchar* name);
static void dc_audio_player_toggle (DcAudioPlayer* self);
static void _dc_audio_player_toggle_gtk_button_clicked (GtkButton* _sender,
                                                 gpointer self);
static void dc_audio_player_stop (DcAudioPlayer* self);
static void dc_audio_player_play (DcAudioPlayer* self);
static void dc_audio_player_hide_controls (GtkWidget* w);
static void ___lambda72_ (DcAudioPlayer* self);
static void dc_audio_player_set_playing_visuals (DcAudioPlayer* self,
                                          gboolean playing);
static void ____lambda72__g_object_notify (GObject* _sender,
                                    GParamSpec* pspec,
                                    gpointer self);
static void dc_audio_player_finalize (GObject * obj);
static GType dc_audio_player_get_type_once (void);

static inline gpointer
dc_audio_player_get_instance_private (DcAudioPlayer* self)
{
	return G_STRUCT_MEMBER_P (self, DcAudioPlayer_private_offset);
}

static void
_dc_audio_player_toggle_gtk_button_clicked (GtkButton* _sender,
                                            gpointer self)
{
	dc_audio_player_toggle ((DcAudioPlayer*) self);
}

DcAudioPlayer*
dc_audio_player_construct (GType object_type,
                           const gchar* path,
                           const gchar* name)
{
	DcAudioPlayer * self = NULL;
	gchar* _tmp0_;
	GtkButton* _tmp1_;
	GtkButton* _tmp2_;
	GtkButton* _tmp3_;
	GtkButton* _tmp4_;
	GtkButton* _tmp5_;
	GtkOverlay* _tmp6_;
	GtkOverlay* _tmp7_;
	GtkButton* _tmp8_;
	GtkOverlay* _tmp9_;
	GtkOverlay* _tmp10_;
	const gchar* _tmp11_ = NULL;
	gboolean _tmp12_ = FALSE;
	GtkLabel* label = NULL;
	GtkLabel* _tmp15_;
	g_return_val_if_fail (path != NULL, NULL);
	self = (DcAudioPlayer*) g_object_new (object_type, "orientation", GTK_ORIENTATION_HORIZONTAL, "spacing", 8, NULL);
	_tmp0_ = g_strdup (path);
	_g_free0 (self->priv->path);
	self->priv->path = _tmp0_;
	gtk_widget_add_css_class ((GtkWidget*) self, "message-audio");
	gtk_widget_set_halign ((GtkWidget*) self, GTK_ALIGN_START);
	_tmp1_ = (GtkButton*) gtk_button_new_from_icon_name ("media-playback-start-symbolic");
	g_object_ref_sink (_tmp1_);
	_g_object_unref0 (self->priv->btn);
	self->priv->btn = _tmp1_;
	_tmp2_ = self->priv->btn;
	gtk_widget_add_css_class ((GtkWidget*) _tmp2_, "circular");
	_tmp3_ = self->priv->btn;
	gtk_widget_set_valign ((GtkWidget*) _tmp3_, GTK_ALIGN_CENTER);
	_tmp4_ = self->priv->btn;
	gtk_widget_set_tooltip_text ((GtkWidget*) _tmp4_, "Play");
	_tmp5_ = self->priv->btn;
	g_signal_connect_object (_tmp5_, "clicked", (GCallback) _dc_audio_player_toggle_gtk_button_clicked, self, 0);
	_tmp6_ = (GtkOverlay*) gtk_overlay_new ();
	g_object_ref_sink (_tmp6_);
	_g_object_unref0 (self->priv->host);
	self->priv->host = _tmp6_;
	_tmp7_ = self->priv->host;
	_tmp8_ = self->priv->btn;
	gtk_overlay_set_child (_tmp7_, (GtkWidget*) _tmp8_);
	_tmp9_ = self->priv->host;
	gtk_widget_set_valign ((GtkWidget*) _tmp9_, GTK_ALIGN_CENTER);
	_tmp10_ = self->priv->host;
	gtk_box_append ((GtkBox*) self, (GtkWidget*) _tmp10_);
	if (name != NULL) {
		gint _tmp13_;
		gint _tmp14_;
		_tmp13_ = strlen (name);
		_tmp14_ = _tmp13_;
		_tmp12_ = _tmp14_ > 0;
	} else {
		_tmp12_ = FALSE;
	}
	if (_tmp12_) {
		_tmp11_ = name;
	} else {
		_tmp11_ = "Voice message";
	}
	_tmp15_ = (GtkLabel*) gtk_label_new (_tmp11_);
	g_object_ref_sink (_tmp15_);
	label = _tmp15_;
	gtk_widget_add_css_class ((GtkWidget*) label, "message-audio-name");
	gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_MIDDLE);
	gtk_label_set_max_width_chars (label, 22);
	gtk_label_set_xalign (label, (gfloat) 0);
	gtk_widget_set_valign ((GtkWidget*) label, GTK_ALIGN_CENTER);
	gtk_box_append ((GtkBox*) self, (GtkWidget*) label);
	_g_object_unref0 (label);
	return self;
}

DcAudioPlayer*
dc_audio_player_new (const gchar* path,
                     const gchar* name)
{
	return dc_audio_player_construct (DC_TYPE_AUDIO_PLAYER, path, name);
}

static void
dc_audio_player_toggle (DcAudioPlayer* self)
{
	GtkVideo* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->engine;
	if (_tmp0_ != NULL) {
		dc_audio_player_stop (self);
	} else {
		dc_audio_player_play (self);
	}
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static void
___lambda72_ (DcAudioPlayer* self)
{
	GtkVideo* _tmp0_;
	GtkMediaStream* s = NULL;
	GtkVideo* _tmp1_;
	GtkMediaStream* _tmp2_;
	GtkMediaStream* _tmp3_;
	gboolean _tmp4_ = FALSE;
	GtkMediaStream* _tmp5_;
	_tmp0_ = self->priv->engine;
	if (_tmp0_ == NULL) {
		return;
	}
	_tmp1_ = self->priv->engine;
	_tmp2_ = gtk_video_get_media_stream (_tmp1_);
	_tmp3_ = _g_object_ref0 (_tmp2_);
	s = _tmp3_;
	_tmp5_ = s;
	if (_tmp5_ == NULL) {
		_tmp4_ = TRUE;
	} else {
		GtkMediaStream* _tmp6_;
		gboolean _tmp7_;
		gboolean _tmp8_;
		_tmp6_ = s;
		_tmp7_ = gtk_media_stream_get_playing (_tmp6_);
		_tmp8_ = _tmp7_;
		_tmp4_ = !_tmp8_;
	}
	if (_tmp4_) {
		dc_audio_player_stop (self);
	} else {
		dc_audio_player_set_playing_visuals (self, TRUE);
	}
	_g_object_unref0 (s);
}

static void
____lambda72__g_object_notify (GObject* _sender,
                               GParamSpec* pspec,
                               gpointer self)
{
	___lambda72_ ((DcAudioPlayer*) self);
}

static void
dc_audio_player_play (DcAudioPlayer* self)
{
	GtkVideo* e = NULL;
	GtkVideo* _tmp0_;
	GtkVideo* _tmp1_;
	GtkVideo* _tmp2_;
	GtkVideo* _tmp3_;
	GtkVideo* _tmp4_;
	GtkVideo* _tmp5_;
	GtkVideo* _tmp6_;
	const gchar* _tmp7_;
	GtkVideo* _tmp8_;
	GtkOverlay* _tmp9_;
	GtkVideo* _tmp10_;
	GtkVideo* _tmp11_;
	GtkVideo* _tmp12_;
	GtkMediaStream* st = NULL;
	GtkVideo* _tmp13_;
	GtkMediaStream* _tmp14_;
	GtkMediaStream* _tmp15_;
	GtkMediaStream* _tmp16_;
	g_return_if_fail (self != NULL);
	dc_audio_player_stop (self);
	_tmp0_ = (GtkVideo*) gtk_video_new ();
	g_object_ref_sink (_tmp0_);
	e = _tmp0_;
	_tmp1_ = e;
	gtk_video_set_autoplay (_tmp1_, TRUE);
	_tmp2_ = e;
	gtk_video_set_loop (_tmp2_, FALSE);
	_tmp3_ = e;
	gtk_widget_set_can_target ((GtkWidget*) _tmp3_, FALSE);
	_tmp4_ = e;
	gtk_widget_set_opacity ((GtkWidget*) _tmp4_, (gdouble) 0);
	_tmp5_ = e;
	gtk_widget_set_size_request ((GtkWidget*) _tmp5_, 0, 0);
	_tmp6_ = e;
	_tmp7_ = self->priv->path;
	gtk_video_set_filename (_tmp6_, _tmp7_);
	_tmp8_ = e;
	dc_audio_player_hide_controls ((GtkWidget*) _tmp8_);
	_tmp9_ = self->priv->host;
	_tmp10_ = e;
	gtk_overlay_add_overlay (_tmp9_, (GtkWidget*) _tmp10_);
	_tmp11_ = e;
	_tmp12_ = _g_object_ref0 (_tmp11_);
	_g_object_unref0 (self->priv->engine);
	self->priv->engine = _tmp12_;
	_tmp13_ = e;
	_tmp14_ = gtk_video_get_media_stream (_tmp13_);
	_tmp15_ = _g_object_ref0 (_tmp14_);
	st = _tmp15_;
	_tmp16_ = st;
	if (_tmp16_ != NULL) {
		GtkMediaStream* _tmp17_;
		_tmp17_ = st;
		g_signal_connect_object ((GObject*) _tmp17_, "notify::playing", (GCallback) ____lambda72__g_object_notify, self, 0);
	}
	dc_audio_player_set_playing_visuals (self, TRUE);
	_g_object_unref0 (st);
	_g_object_unref0 (e);
}

static void
dc_audio_player_stop (DcAudioPlayer* self)
{
	GtkVideo* e = NULL;
	GtkVideo* _tmp0_;
	GtkVideo* _tmp1_;
	GtkVideo* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->engine;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	e = _tmp1_;
	_g_object_unref0 (self->priv->engine);
	self->priv->engine = NULL;
	_tmp2_ = e;
	if (_tmp2_ != NULL) {
		GtkMediaStream* st = NULL;
		GtkVideo* _tmp3_;
		GtkMediaStream* _tmp4_;
		GtkMediaStream* _tmp5_;
		GtkMediaStream* _tmp6_;
		GtkOverlay* _tmp8_;
		GtkVideo* _tmp9_;
		_tmp3_ = e;
		_tmp4_ = gtk_video_get_media_stream (_tmp3_);
		_tmp5_ = _g_object_ref0 (_tmp4_);
		st = _tmp5_;
		_tmp6_ = st;
		if (_tmp6_ != NULL) {
			GtkMediaStream* _tmp7_;
			_tmp7_ = st;
			gtk_media_stream_pause (_tmp7_);
		}
		_tmp8_ = self->priv->host;
		_tmp9_ = e;
		gtk_overlay_remove_overlay (_tmp8_, (GtkWidget*) _tmp9_);
		_g_object_unref0 (st);
	}
	dc_audio_player_set_playing_visuals (self, FALSE);
	_g_object_unref0 (e);
}

static void
dc_audio_player_set_playing_visuals (DcAudioPlayer* self,
                                     gboolean playing)
{
	const gchar* _tmp0_ = NULL;
	GtkButton* _tmp1_;
	const gchar* _tmp2_ = NULL;
	GtkButton* _tmp3_;
	g_return_if_fail (self != NULL);
	if (playing) {
		_tmp0_ = "media-playback-stop-symbolic";
	} else {
		_tmp0_ = "media-playback-start-symbolic";
	}
	_tmp1_ = self->priv->btn;
	gtk_button_set_icon_name (_tmp1_, _tmp0_);
	if (playing) {
		_tmp2_ = "Stop";
	} else {
		_tmp2_ = "Play";
	}
	_tmp3_ = self->priv->btn;
	gtk_widget_set_tooltip_text ((GtkWidget*) _tmp3_, _tmp2_);
}

static void
dc_audio_player_hide_controls (GtkWidget* w)
{
	g_return_if_fail (w != NULL);
	{
		GtkWidget* c = NULL;
		GtkWidget* _tmp0_;
		GtkWidget* _tmp1_;
		_tmp0_ = gtk_widget_get_first_child (w);
		_tmp1_ = _g_object_ref0 (_tmp0_);
		c = _tmp1_;
		{
			gboolean _tmp2_ = FALSE;
			_tmp2_ = TRUE;
			while (TRUE) {
				GtkWidget* _tmp6_;
				GtkWidget* _tmp7_;
				GtkWidget* _tmp9_;
				if (!_tmp2_) {
					GtkWidget* _tmp3_;
					GtkWidget* _tmp4_;
					GtkWidget* _tmp5_;
					_tmp3_ = c;
					_tmp4_ = gtk_widget_get_next_sibling (_tmp3_);
					_tmp5_ = _g_object_ref0 (_tmp4_);
					_g_object_unref0 (c);
					c = _tmp5_;
				}
				_tmp2_ = FALSE;
				_tmp6_ = c;
				if (!(_tmp6_ != NULL)) {
					break;
				}
				_tmp7_ = c;
				if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp7_, gtk_media_controls_get_type ())) {
					GtkWidget* _tmp8_;
					_tmp8_ = c;
					gtk_widget_set_visible (_tmp8_, FALSE);
				}
				_tmp9_ = c;
				dc_audio_player_hide_controls (_tmp9_);
			}
		}
		_g_object_unref0 (c);
	}
}

static void
dc_audio_player_class_init (DcAudioPlayerClass * klass,
                            gpointer klass_data)
{
	dc_audio_player_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &DcAudioPlayer_private_offset);
	G_OBJECT_CLASS (klass)->finalize = dc_audio_player_finalize;
}

static void
dc_audio_player_instance_init (DcAudioPlayer * self,
                               gpointer klass)
{
	self->priv = dc_audio_player_get_instance_private (self);
	self->priv->engine = NULL;
}

static void
dc_audio_player_finalize (GObject * obj)
{
	DcAudioPlayer * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DC_TYPE_AUDIO_PLAYER, DcAudioPlayer);
	_g_free0 (self->priv->path);
	_g_object_unref0 (self->priv->btn);
	_g_object_unref0 (self->priv->host);
	_g_object_unref0 (self->priv->engine);
	G_OBJECT_CLASS (dc_audio_player_parent_class)->finalize (obj);
}

/**
     * Compact inline player for audio / voice attachments: a single
     * play/stop button plus the file name — no bulky media-controls bar.
     *
     * Playback quirk this works around: a bare Gtk.MediaFile never actually
     * plays (it prepares asynchronously and a manual play() is dropped), and
     * even a realized stream won't start on a manual play() — only a mapped
     * Gtk.Video with autoplay=true reliably drives the GStreamer pipeline,
     * and once paused it won't resume. So the engine is a hidden Gtk.Video
     * (controls removed, opacity 0) created fresh on each play and torn down
     * to stop. pause() does work, which lets us halt cleanly.
     */
static GType
dc_audio_player_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (DcAudioPlayerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) dc_audio_player_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DcAudioPlayer), 0, (GInstanceInitFunc) dc_audio_player_instance_init, NULL };
	GType dc_audio_player_type_id;
	dc_audio_player_type_id = g_type_register_static (gtk_box_get_type (), "DcAudioPlayer", &g_define_type_info, 0);
	DcAudioPlayer_private_offset = g_type_add_instance_private (dc_audio_player_type_id, sizeof (DcAudioPlayerPrivate));
	return dc_audio_player_type_id;
}

GType
dc_audio_player_get_type (void)
{
	static volatile gsize dc_audio_player_type_id__once = 0;
	if (g_once_init_enter (&dc_audio_player_type_id__once)) {
		GType dc_audio_player_type_id;
		dc_audio_player_type_id = dc_audio_player_get_type_once ();
		g_once_init_leave (&dc_audio_player_type_id__once, dc_audio_player_type_id);
	}
	return dc_audio_player_type_id__once;
}

