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

/*
 * Copyright © 2023 Alain M. (https://github.com/alainm23/planify)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authored by: Alain M. <alainmh23@gmail.com>
 */

#include "planify.h"
#include <glib.h>
#include <gtk/gtk.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <adwaita.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  {
	WIDGETS_STATUS_BUTTON_0_PROPERTY,
	WIDGETS_STATUS_BUTTON_IS_BOARD_PROPERTY,
	WIDGETS_STATUS_BUTTON_NUM_PROPERTIES
};
static GParamSpec* widgets_status_button_properties[WIDGETS_STATUS_BUTTON_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block18Data Block18Data;
enum  {
	WIDGETS_STATUS_BUTTON_CHANGED_SIGNAL,
	WIDGETS_STATUS_BUTTON_NUM_SIGNALS
};
static guint widgets_status_button_signals[WIDGETS_STATUS_BUTTON_NUM_SIGNALS] = {0};

struct _WidgetsStatusButtonPrivate {
	gboolean _is_board;
	GtkImage* status_image;
	GtkLabel* value_label;
};

struct _Block18Data {
	int _ref_count_;
	WidgetsStatusButton* self;
	GtkPopover* popover;
};

static gint WidgetsStatusButton_private_offset;
static gpointer widgets_status_button_parent_class = NULL;

static Block18Data* block18_data_ref (Block18Data* _data18_);
static void block18_data_unref (void * _userdata_);
static void __lambda356_ (Block18Data* _data18_);
static void ___lambda356__gtk_button_clicked (GtkButton* _sender,
                                       gpointer self);
static void __lambda357_ (Block18Data* _data18_);
static void ___lambda357__gtk_button_clicked (GtkButton* _sender,
                                       gpointer self);
static GObject * widgets_status_button_constructor (GType type,
                                             guint n_construct_properties,
                                             GObjectConstructParam * construct_properties);
static void widgets_status_button_finalize (GObject * obj);
static GType widgets_status_button_get_type_once (void);
static void _vala_widgets_status_button_get_property (GObject * object,
                                               guint property_id,
                                               GValue * value,
                                               GParamSpec * pspec);
static void _vala_widgets_status_button_set_property (GObject * object,
                                               guint property_id,
                                               const GValue * value,
                                               GParamSpec * pspec);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
widgets_status_button_get_instance_private (WidgetsStatusButton* self)
{
	return G_STRUCT_MEMBER_P (self, WidgetsStatusButton_private_offset);
}

WidgetsStatusButton*
widgets_status_button_construct (GType object_type)
{
	WidgetsStatusButton * self = NULL;
	self = (WidgetsStatusButton*) g_object_new (object_type, "tooltip-text", _ ("Set The Status"), NULL);
	return self;
}

WidgetsStatusButton*
widgets_status_button_new (void)
{
	return widgets_status_button_construct (WIDGETS_TYPE_STATUS_BUTTON);
}

static Block18Data*
block18_data_ref (Block18Data* _data18_)
{
	g_atomic_int_inc (&_data18_->_ref_count_);
	return _data18_;
}

static void
block18_data_unref (void * _userdata_)
{
	Block18Data* _data18_;
	_data18_ = (Block18Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data18_->_ref_count_)) {
		WidgetsStatusButton* self;
		self = _data18_->self;
		_g_object_unref0 (_data18_->popover);
		_g_object_unref0 (self);
		g_slice_free (Block18Data, _data18_);
	}
}

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

static void
__lambda356_ (Block18Data* _data18_)
{
	WidgetsStatusButton* self;
	self = _data18_->self;
	gtk_popover_popdown (_data18_->popover);
	g_signal_emit (self, widgets_status_button_signals[WIDGETS_STATUS_BUTTON_CHANGED_SIGNAL], 0, FALSE);
}

static void
___lambda356__gtk_button_clicked (GtkButton* _sender,
                                  gpointer self)
{
	__lambda356_ (self);
}

static void
__lambda357_ (Block18Data* _data18_)
{
	WidgetsStatusButton* self;
	self = _data18_->self;
	gtk_popover_popdown (_data18_->popover);
	g_signal_emit (self, widgets_status_button_signals[WIDGETS_STATUS_BUTTON_CHANGED_SIGNAL], 0, TRUE);
}

static void
___lambda357__gtk_button_clicked (GtkButton* _sender,
                                  gpointer self)
{
	__lambda357_ (self);
}

GtkPopover*
widgets_status_button_build_popover (WidgetsStatusButton* self)
{
	Block18Data* _data18_;
	WidgetsContextMenuMenuItem* todo_item = NULL;
	WidgetsContextMenuMenuItem* _tmp0_;
	WidgetsContextMenuMenuItem* complete_item = NULL;
	WidgetsContextMenuMenuItem* _tmp1_;
	GtkBox* menu_box = NULL;
	GtkBox* _tmp2_;
	GtkWidget* _tmp3_;
	GtkPopover* _tmp4_ = NULL;
	GtkPopover* _tmp5_;
	GtkPopover* result;
	g_return_val_if_fail (self != NULL, NULL);
	_data18_ = g_slice_new0 (Block18Data);
	_data18_->_ref_count_ = 1;
	_data18_->self = g_object_ref (self);
	_tmp0_ = widgets_context_menu_menu_item_new (_ ("To Do"), "check-round-outline-symbolic");
	g_object_ref_sink (_tmp0_);
	todo_item = _tmp0_;
	_tmp1_ = widgets_context_menu_menu_item_new (_ ("Complete"), "check-round-outline-symbolic");
	g_object_ref_sink (_tmp1_);
	complete_item = _tmp1_;
	gtk_widget_add_css_class ((GtkWidget*) complete_item, "completed-color");
	_tmp2_ = (GtkBox*) gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
	g_object_ref_sink (_tmp2_);
	menu_box = _tmp2_;
	gtk_widget_set_margin_bottom ((GtkWidget*) menu_box, 3);
	gtk_widget_set_margin_top ((GtkWidget*) menu_box, 3);
	gtk_box_append (menu_box, (GtkWidget*) todo_item);
	gtk_box_append (menu_box, (GtkWidget*) complete_item);
	_tmp3_ = _g_object_ref0 ((GtkWidget*) menu_box);
	_tmp4_ = (GtkPopover*) gtk_popover_new ();
	gtk_popover_set_has_arrow (_tmp4_, FALSE);
	gtk_popover_set_child (_tmp4_, _tmp3_);
	_g_object_unref0 (_tmp3_);
	gtk_popover_set_position (_tmp4_, GTK_POS_BOTTOM);
	g_object_ref_sink (_tmp4_);
	_data18_->popover = _tmp4_;
	g_signal_connect_data ((GtkButton*) todo_item, "clicked", (GCallback) ___lambda356__gtk_button_clicked, block18_data_ref (_data18_), (GClosureNotify) block18_data_unref, 0);
	g_signal_connect_data ((GtkButton*) complete_item, "clicked", (GCallback) ___lambda357__gtk_button_clicked, block18_data_ref (_data18_), (GClosureNotify) block18_data_unref, 0);
	_tmp5_ = _g_object_ref0 (_data18_->popover);
	result = _tmp5_;
	_g_object_unref0 (menu_box);
	_g_object_unref0 (complete_item);
	_g_object_unref0 (todo_item);
	block18_data_unref (_data18_);
	_data18_ = NULL;
	return result;
}

void
widgets_status_button_update_from_item (WidgetsStatusButton* self,
                                        ObjectsItem* item)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (item != NULL);
	_tmp0_ = objects_item_get_completed (item);
	_tmp1_ = _tmp0_;
	widgets_status_button_set_status (self, _tmp1_);
}

void
widgets_status_button_set_status (WidgetsStatusButton* self,
                                  gboolean active)
{
	g_return_if_fail (self != NULL);
	if (active) {
		GtkImage* _tmp0_;
		GtkLabel* _tmp1_;
		_tmp0_ = self->priv->status_image;
		gtk_widget_add_css_class ((GtkWidget*) _tmp0_, "completed-image-color");
		_tmp1_ = self->priv->value_label;
		gtk_label_set_label (_tmp1_, _ ("Complete"));
	} else {
		GtkImage* _tmp2_;
		GtkLabel* _tmp3_;
		_tmp2_ = self->priv->status_image;
		gtk_widget_remove_css_class ((GtkWidget*) _tmp2_, "completed-image-color");
		_tmp3_ = self->priv->value_label;
		gtk_label_set_label (_tmp3_, _ ("To Do"));
	}
}

gboolean
widgets_status_button_get_is_board (WidgetsStatusButton* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_is_board;
	return result;
}

static void
widgets_status_button_set_is_board (WidgetsStatusButton* self,
                                    gboolean value)
{
	gboolean old_value;
	g_return_if_fail (self != NULL);
	old_value = widgets_status_button_get_is_board (self);
	if (old_value != value) {
		self->priv->_is_board = value;
		g_object_notify_by_pspec ((GObject *) self, widgets_status_button_properties[WIDGETS_STATUS_BUTTON_IS_BOARD_PROPERTY]);
	}
}

static GObject *
widgets_status_button_constructor (GType type,
                                   guint n_construct_properties,
                                   GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	WidgetsStatusButton * self;
	GtkImage* _tmp0_;
	GtkLabel* title_label = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar** _tmp4_;
	GtkLabel* _tmp5_ = NULL;
	gchar* _tmp6_;
	gchar** _tmp7_;
	GtkLabel* _tmp8_ = NULL;
	GtkGrid* card_grid = NULL;
	GtkGrid* _tmp9_ = NULL;
	GtkGrid* _tmp10_;
	GtkImage* _tmp11_;
	GtkGrid* _tmp12_;
	GtkLabel* _tmp13_;
	GtkGrid* _tmp14_;
	GtkLabel* _tmp15_;
	GtkMenuButton* menu_button = NULL;
	GtkPopover* _tmp16_;
	GtkGrid* _tmp17_;
	GtkWidget* _tmp18_;
	gchar* _tmp19_;
	gchar* _tmp20_;
	gchar* _tmp21_;
	gchar* _tmp22_;
	gchar** _tmp23_;
	GtkMenuButton* _tmp24_ = NULL;
	GtkMenuButton* _tmp25_;
	parent_class = G_OBJECT_CLASS (widgets_status_button_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, WIDGETS_TYPE_STATUS_BUTTON, WidgetsStatusButton);
	_tmp0_ = (GtkImage*) gtk_image_new_from_icon_name ("check-round-outline-symbolic");
	g_object_ref_sink (_tmp0_);
	_g_object_unref0 (self->priv->status_image);
	self->priv->status_image = _tmp0_;
	_tmp1_ = g_strdup ("title-4");
	_tmp2_ = g_strdup ("caption");
	_tmp3_ = g_strdup ("font-bold");
	_tmp4_ = g_new0 (gchar*, 3 + 1);
	_tmp4_[0] = _tmp1_;
	_tmp4_[1] = _tmp2_;
	_tmp4_[2] = _tmp3_;
	_tmp5_ = (GtkLabel*) gtk_label_new (_ ("Status"));
	gtk_widget_set_halign ((GtkWidget*) _tmp5_, GTK_ALIGN_START);
	gtk_widget_set_css_classes ((GtkWidget*) _tmp5_, _tmp4_);
	_tmp4_ = (_vala_array_free (_tmp4_, 3, (GDestroyNotify) g_free), NULL);
	g_object_ref_sink (_tmp5_);
	title_label = _tmp5_;
	_tmp6_ = g_strdup ("caption");
	_tmp7_ = g_new0 (gchar*, 1 + 1);
	_tmp7_[0] = _tmp6_;
	_tmp8_ = (GtkLabel*) gtk_label_new (_ ("To Do"));
	gtk_label_set_xalign (_tmp8_, (gfloat) 0);
	gtk_label_set_use_markup (_tmp8_, TRUE);
	gtk_widget_set_halign ((GtkWidget*) _tmp8_, GTK_ALIGN_START);
	gtk_widget_set_css_classes ((GtkWidget*) _tmp8_, _tmp7_);
	_tmp7_ = (_vala_array_free (_tmp7_, 1, (GDestroyNotify) g_free), NULL);
	g_object_ref_sink (_tmp8_);
	_g_object_unref0 (self->priv->value_label);
	self->priv->value_label = _tmp8_;
	_tmp9_ = (GtkGrid*) gtk_grid_new ();
	gtk_grid_set_column_spacing (_tmp9_, 12);
	gtk_widget_set_margin_start ((GtkWidget*) _tmp9_, 12);
	gtk_widget_set_margin_end ((GtkWidget*) _tmp9_, 6);
	gtk_widget_set_margin_top ((GtkWidget*) _tmp9_, 6);
	gtk_widget_set_margin_bottom ((GtkWidget*) _tmp9_, 6);
	gtk_widget_set_vexpand ((GtkWidget*) _tmp9_, TRUE);
	gtk_widget_set_hexpand ((GtkWidget*) _tmp9_, TRUE);
	g_object_ref_sink (_tmp9_);
	card_grid = _tmp9_;
	_tmp10_ = card_grid;
	_tmp11_ = self->priv->status_image;
	gtk_grid_attach (_tmp10_, (GtkWidget*) _tmp11_, 0, 0, 1, 2);
	_tmp12_ = card_grid;
	_tmp13_ = title_label;
	gtk_grid_attach (_tmp12_, (GtkWidget*) _tmp13_, 1, 0, 1, 1);
	_tmp14_ = card_grid;
	_tmp15_ = self->priv->value_label;
	gtk_grid_attach (_tmp14_, (GtkWidget*) _tmp15_, 1, 1, 1, 1);
	_tmp16_ = widgets_status_button_build_popover (self);
	_tmp17_ = card_grid;
	_tmp18_ = _g_object_ref0 ((GtkWidget*) _tmp17_);
	_tmp19_ = g_strdup ("flat");
	_tmp20_ = g_strdup ("card");
	_tmp21_ = g_strdup ("activatable");
	_tmp22_ = g_strdup ("menu-button-no-padding");
	_tmp23_ = g_new0 (gchar*, 4 + 1);
	_tmp23_[0] = _tmp19_;
	_tmp23_[1] = _tmp20_;
	_tmp23_[2] = _tmp21_;
	_tmp23_[3] = _tmp22_;
	_tmp24_ = (GtkMenuButton*) gtk_menu_button_new ();
	gtk_menu_button_set_popover (_tmp24_, _tmp16_);
	_g_object_unref0 (_tmp16_);
	gtk_menu_button_set_child (_tmp24_, _tmp18_);
	_g_object_unref0 (_tmp18_);
	gtk_widget_set_css_classes ((GtkWidget*) _tmp24_, _tmp23_);
	_tmp23_ = (_vala_array_free (_tmp23_, 4, (GDestroyNotify) g_free), NULL);
	gtk_widget_set_hexpand ((GtkWidget*) _tmp24_, TRUE);
	g_object_ref_sink (_tmp24_);
	menu_button = _tmp24_;
	_tmp25_ = menu_button;
	adw_bin_set_child ((AdwBin*) self, (GtkWidget*) _tmp25_);
	_g_object_unref0 (menu_button);
	_g_object_unref0 (card_grid);
	_g_object_unref0 (title_label);
	return obj;
}

static void
widgets_status_button_class_init (WidgetsStatusButtonClass * klass,
                                  gpointer klass_data)
{
	widgets_status_button_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &WidgetsStatusButton_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_widgets_status_button_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_widgets_status_button_set_property;
	G_OBJECT_CLASS (klass)->constructor = widgets_status_button_constructor;
	G_OBJECT_CLASS (klass)->finalize = widgets_status_button_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), WIDGETS_STATUS_BUTTON_IS_BOARD_PROPERTY, widgets_status_button_properties[WIDGETS_STATUS_BUTTON_IS_BOARD_PROPERTY] = g_param_spec_boolean ("is-board", "is-board", "is-board", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	widgets_status_button_signals[WIDGETS_STATUS_BUTTON_CHANGED_SIGNAL] = g_signal_new ("changed", WIDGETS_TYPE_STATUS_BUTTON, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN, G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
}

static void
widgets_status_button_instance_init (WidgetsStatusButton * self,
                                     gpointer klass)
{
	self->priv = widgets_status_button_get_instance_private (self);
}

static void
widgets_status_button_finalize (GObject * obj)
{
	WidgetsStatusButton * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, WIDGETS_TYPE_STATUS_BUTTON, WidgetsStatusButton);
	_g_object_unref0 (self->priv->status_image);
	_g_object_unref0 (self->priv->value_label);
	G_OBJECT_CLASS (widgets_status_button_parent_class)->finalize (obj);
}

static GType
widgets_status_button_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (WidgetsStatusButtonClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) widgets_status_button_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (WidgetsStatusButton), 0, (GInstanceInitFunc) widgets_status_button_instance_init, NULL };
	GType widgets_status_button_type_id;
	widgets_status_button_type_id = g_type_register_static (adw_bin_get_type (), "WidgetsStatusButton", &g_define_type_info, 0);
	WidgetsStatusButton_private_offset = g_type_add_instance_private (widgets_status_button_type_id, sizeof (WidgetsStatusButtonPrivate));
	return widgets_status_button_type_id;
}

GType
widgets_status_button_get_type (void)
{
	static volatile gsize widgets_status_button_type_id__once = 0;
	if (g_once_init_enter (&widgets_status_button_type_id__once)) {
		GType widgets_status_button_type_id;
		widgets_status_button_type_id = widgets_status_button_get_type_once ();
		g_once_init_leave (&widgets_status_button_type_id__once, widgets_status_button_type_id);
	}
	return widgets_status_button_type_id__once;
}

static void
_vala_widgets_status_button_get_property (GObject * object,
                                          guint property_id,
                                          GValue * value,
                                          GParamSpec * pspec)
{
	WidgetsStatusButton * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, WIDGETS_TYPE_STATUS_BUTTON, WidgetsStatusButton);
	switch (property_id) {
		case WIDGETS_STATUS_BUTTON_IS_BOARD_PROPERTY:
		g_value_set_boolean (value, widgets_status_button_get_is_board (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_widgets_status_button_set_property (GObject * object,
                                          guint property_id,
                                          const GValue * value,
                                          GParamSpec * pspec)
{
	WidgetsStatusButton * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, WIDGETS_TYPE_STATUS_BUTTON, WidgetsStatusButton);
	switch (property_id) {
		case WIDGETS_STATUS_BUTTON_IS_BOARD_PROPERTY:
		widgets_status_button_set_is_board (self, g_value_get_boolean (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

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;
}

