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

/*
 * Copyright (c) 2024-2025 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "ft_ui.h"
#include <gtk/gtk.h>
#include <glib.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <glib-object.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

enum  {
	FT_VOLUME_SLIDER_0_PROPERTY,
	FT_VOLUME_SLIDER_VALUE_PROPERTY,
	FT_VOLUME_SLIDER_NUM_PROPERTIES
};
static GParamSpec* ft_volume_slider_properties[FT_VOLUME_SLIDER_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))

struct _FtVolumeSliderPrivate {
	GtkImage* volume_image;
	GtkAdjustment* adjustment;
	gdouble last_volume;
};

static gint FtVolumeSlider_private_offset;
static gpointer ft_volume_slider_parent_class = NULL;

static void ft_volume_slider_update_volume_image (FtVolumeSlider* self);
static void ft_volume_slider_on_mute_button_clicked (FtVolumeSlider* self);
static void _ft_volume_slider_on_mute_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                                  gpointer self);
static void ft_volume_slider_on_adjustment_value_changed (FtVolumeSlider* self);
static void _ft_volume_slider_on_adjustment_value_changed_gtk_adjustment_value_changed (GtkAdjustment* _sender,
                                                                                 gpointer self);
static GObject * ft_volume_slider_constructor (GType type,
                                        guint n_construct_properties,
                                        GObjectConstructParam * construct_properties);
static void ft_volume_slider_finalize (GObject * obj);
static GType ft_volume_slider_get_type_once (void);
static void _vala_ft_volume_slider_get_property (GObject * object,
                                          guint property_id,
                                          GValue * value,
                                          GParamSpec * pspec);
static void _vala_ft_volume_slider_set_property (GObject * object,
                                          guint property_id,
                                          const GValue * value,
                                          GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static const gchar* FT_VOLUME_SLIDER_VOLUME_IMAGES[4] = {"audio-volume-muted-symbolic", "audio-volume-low-symbolic", "audio-volume-medium-symbolic", "audio-volume-high-symbolic"};

static inline gpointer
ft_volume_slider_get_instance_private (FtVolumeSlider* self)
{
	return G_STRUCT_MEMBER_P (self, FtVolumeSlider_private_offset);
}

static void
ft_volume_slider_update_volume_image (FtVolumeSlider* self)
{
	gdouble value = 0.0;
	GtkAdjustment* _tmp0_;
	gdouble _tmp1_;
	gdouble _tmp2_;
	gchar* icon_name = NULL;
	GtkImage* _tmp11_;
	const gchar* _tmp12_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->adjustment;
	_tmp1_ = gtk_adjustment_get_value (_tmp0_);
	_tmp2_ = _tmp1_;
	value = _tmp2_;
	if (value == 0.0) {
		const gchar* _tmp3_;
		gchar* _tmp4_;
		_tmp3_ = FT_VOLUME_SLIDER_VOLUME_IMAGES[0];
		_tmp4_ = g_strdup (_tmp3_);
		_g_free0 (icon_name);
		icon_name = _tmp4_;
	} else {
		if (value < 0.3) {
			const gchar* _tmp5_;
			gchar* _tmp6_;
			_tmp5_ = FT_VOLUME_SLIDER_VOLUME_IMAGES[1];
			_tmp6_ = g_strdup (_tmp5_);
			_g_free0 (icon_name);
			icon_name = _tmp6_;
		} else {
			if (value < 0.7) {
				const gchar* _tmp7_;
				gchar* _tmp8_;
				_tmp7_ = FT_VOLUME_SLIDER_VOLUME_IMAGES[2];
				_tmp8_ = g_strdup (_tmp7_);
				_g_free0 (icon_name);
				icon_name = _tmp8_;
			} else {
				const gchar* _tmp9_;
				gchar* _tmp10_;
				_tmp9_ = FT_VOLUME_SLIDER_VOLUME_IMAGES[3];
				_tmp10_ = g_strdup (_tmp9_);
				_g_free0 (icon_name);
				icon_name = _tmp10_;
			}
		}
	}
	_tmp11_ = self->priv->volume_image;
	_tmp12_ = icon_name;
	g_object_set (_tmp11_, "icon-name", _tmp12_, NULL);
	_g_free0 (icon_name);
}

static void
ft_volume_slider_on_mute_button_clicked (FtVolumeSlider* self)
{
	GtkAdjustment* _tmp0_;
	gdouble _tmp1_;
	gdouble _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->adjustment;
	_tmp1_ = gtk_adjustment_get_value (_tmp0_);
	_tmp2_ = _tmp1_;
	if (_tmp2_ > 0.0) {
		GtkAdjustment* _tmp3_;
		gdouble _tmp4_;
		gdouble _tmp5_;
		GtkAdjustment* _tmp6_;
		_tmp3_ = self->priv->adjustment;
		_tmp4_ = gtk_adjustment_get_value (_tmp3_);
		_tmp5_ = _tmp4_;
		self->priv->last_volume = _tmp5_;
		_tmp6_ = self->priv->adjustment;
		gtk_adjustment_set_value (_tmp6_, 0.0);
	} else {
		gdouble _tmp7_ = 0.0;
		GtkAdjustment* _tmp8_;
		if (self->priv->last_volume > 0.0) {
			_tmp7_ = self->priv->last_volume;
		} else {
			_tmp7_ = 1.0;
		}
		_tmp8_ = self->priv->adjustment;
		gtk_adjustment_set_value (_tmp8_, _tmp7_);
	}
}

static void
_ft_volume_slider_on_mute_button_clicked_gtk_button_clicked (GtkButton* _sender,
                                                             gpointer self)
{
	ft_volume_slider_on_mute_button_clicked ((FtVolumeSlider*) self);
}

static void
ft_volume_slider_on_adjustment_value_changed (FtVolumeSlider* self)
{
	g_return_if_fail (self != NULL);
	ft_volume_slider_update_volume_image (self);
	g_object_notify ((GObject*) self, "value");
}

static void
_ft_volume_slider_on_adjustment_value_changed_gtk_adjustment_value_changed (GtkAdjustment* _sender,
                                                                            gpointer self)
{
	ft_volume_slider_on_adjustment_value_changed ((FtVolumeSlider*) self);
}

FtVolumeSlider*
ft_volume_slider_construct (GType object_type)
{
	FtVolumeSlider * self = NULL;
	self = (FtVolumeSlider*) g_object_new (object_type, NULL);
	return self;
}

FtVolumeSlider*
ft_volume_slider_new (void)
{
	return ft_volume_slider_construct (FT_TYPE_VOLUME_SLIDER);
}

gdouble
ft_volume_slider_get_value (FtVolumeSlider* self)
{
	gdouble result;
	GtkAdjustment* _tmp0_;
	gdouble _tmp1_;
	gdouble _tmp2_;
	g_return_val_if_fail (self != NULL, -1.0);
	_tmp0_ = self->priv->adjustment;
	_tmp1_ = gtk_adjustment_get_value (_tmp0_);
	_tmp2_ = _tmp1_;
	result = _tmp2_;
	return result;
}

void
ft_volume_slider_set_value (FtVolumeSlider* self,
                            gdouble value)
{
	GtkAdjustment* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->adjustment;
	gtk_adjustment_set_value (_tmp0_, value);
}

static GObject *
ft_volume_slider_constructor (GType type,
                              guint n_construct_properties,
                              GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	FtVolumeSlider * self;
	parent_class = G_OBJECT_CLASS (ft_volume_slider_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_VOLUME_SLIDER, FtVolumeSlider);
	ft_volume_slider_update_volume_image (self);
	return obj;
}

static void
ft_volume_slider_class_init (FtVolumeSliderClass * klass,
                             gpointer klass_data)
{
	ft_volume_slider_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &FtVolumeSlider_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_ft_volume_slider_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_ft_volume_slider_set_property;
	G_OBJECT_CLASS (klass)->constructor = ft_volume_slider_constructor;
	G_OBJECT_CLASS (klass)->finalize = ft_volume_slider_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), FT_VOLUME_SLIDER_VALUE_PROPERTY, ft_volume_slider_properties[FT_VOLUME_SLIDER_VALUE_PROPERTY] = g_param_spec_double ("value", "value", "value", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_EXPLICIT_NOTIFY));
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/io/github/focustimerhq/FocusTimer/ui/preferences/sounds/volume-slider.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "volume_image", FALSE, FtVolumeSlider_private_offset + G_STRUCT_OFFSET (FtVolumeSliderPrivate, volume_image));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "adjustment", FALSE, FtVolumeSlider_private_offset + G_STRUCT_OFFSET (FtVolumeSliderPrivate, adjustment));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_mute_button_clicked", G_CALLBACK(_ft_volume_slider_on_mute_button_clicked_gtk_button_clicked));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_adjustment_value_changed", G_CALLBACK(_ft_volume_slider_on_adjustment_value_changed_gtk_adjustment_value_changed));
}

static void
ft_volume_slider_instance_init (FtVolumeSlider * self,
                                gpointer klass)
{
	self->priv = ft_volume_slider_get_instance_private (self);
	self->priv->last_volume = 1.0;
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
ft_volume_slider_finalize (GObject * obj)
{
	FtVolumeSlider * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_VOLUME_SLIDER, FtVolumeSlider);
	G_OBJECT_CLASS (ft_volume_slider_parent_class)->finalize (obj);
}

static GType
ft_volume_slider_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (FtVolumeSliderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) ft_volume_slider_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FtVolumeSlider), 0, (GInstanceInitFunc) ft_volume_slider_instance_init, NULL };
	GType ft_volume_slider_type_id;
	ft_volume_slider_type_id = g_type_register_static (gtk_box_get_type (), "FtVolumeSlider", &g_define_type_info, 0);
	FtVolumeSlider_private_offset = g_type_add_instance_private (ft_volume_slider_type_id, sizeof (FtVolumeSliderPrivate));
	return ft_volume_slider_type_id;
}

GType
ft_volume_slider_get_type (void)
{
	static volatile gsize ft_volume_slider_type_id__once = 0;
	if (g_once_init_enter (&ft_volume_slider_type_id__once)) {
		GType ft_volume_slider_type_id;
		ft_volume_slider_type_id = ft_volume_slider_get_type_once ();
		g_once_init_leave (&ft_volume_slider_type_id__once, ft_volume_slider_type_id);
	}
	return ft_volume_slider_type_id__once;
}

static void
_vala_ft_volume_slider_get_property (GObject * object,
                                     guint property_id,
                                     GValue * value,
                                     GParamSpec * pspec)
{
	FtVolumeSlider * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, FT_TYPE_VOLUME_SLIDER, FtVolumeSlider);
	switch (property_id) {
		case FT_VOLUME_SLIDER_VALUE_PROPERTY:
		g_value_set_double (value, ft_volume_slider_get_value (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_ft_volume_slider_set_property (GObject * object,
                                     guint property_id,
                                     const GValue * value,
                                     GParamSpec * pspec)
{
	FtVolumeSlider * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, FT_TYPE_VOLUME_SLIDER, FtVolumeSlider);
	switch (property_id) {
		case FT_VOLUME_SLIDER_VALUE_PROPERTY:
		ft_volume_slider_set_value (self, g_value_get_double (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

