/* CalendarWeek.c generated by valac 0.56.19, the Vala compiler
 * generated from CalendarWeek.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 <gtk/gtk.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include <stdlib.h>
#include <string.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_CALENDAR_CALENDAR_WEEK_0_PROPERTY,
	WIDGETS_CALENDAR_CALENDAR_WEEK_MARGIN_HORIZONTAL_PROPERTY,
	WIDGETS_CALENDAR_CALENDAR_WEEK_NUM_PROPERTIES
};
static GParamSpec* widgets_calendar_calendar_week_properties[WIDGETS_CALENDAR_CALENDAR_WEEK_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _WidgetsCalendarCalendarWeekPrivate {
	GtkLabel** day_labels;
	gint day_labels_length1;
	gint _day_labels_size_;
};

static gint WidgetsCalendarCalendarWeek_private_offset;
static gpointer widgets_calendar_calendar_week_parent_class = NULL;

static GtkLabel** widgets_calendar_calendar_week_rotate_left (WidgetsCalendarCalendarWeek* self,
                                                       GtkLabel** array,
                                                       gint array_length1,
                                                       gint positions,
                                                       gint* result_length1);
static GObject * widgets_calendar_calendar_week_constructor (GType type,
                                                      guint n_construct_properties,
                                                      GObjectConstructParam * construct_properties);
static void widgets_calendar_calendar_week_finalize (GObject * obj);
static GType widgets_calendar_calendar_week_get_type_once (void);
static void _vala_widgets_calendar_calendar_week_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_calendar_calendar_week_get_instance_private (WidgetsCalendarCalendarWeek* self)
{
	return G_STRUCT_MEMBER_P (self, WidgetsCalendarCalendarWeek_private_offset);
}

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

void
widgets_calendar_calendar_week_update (WidgetsCalendarCalendarWeek* self)
{
	gint start_week = 0;
	ServicesSettings* _tmp10_;
	ServicesSettings* _tmp11_;
	GSettings* _tmp12_;
	gint _tmp13_;
	GtkLabel** rotated_labels = NULL;
	GtkLabel** _tmp14_;
	gint _tmp14__length1;
	gint _tmp15_ = 0;
	GtkLabel** _tmp16_;
	gint rotated_labels_length1;
	gint _rotated_labels_size_;
	GtkLabel** _tmp17_;
	gint _tmp17__length1;
	g_return_if_fail (self != NULL);
	{
		GtkWidget* child = NULL;
		GtkWidget* _tmp0_;
		GtkWidget* _tmp1_;
		_tmp0_ = gtk_widget_get_first_child ((GtkWidget*) self);
		_tmp1_ = _g_object_ref0 (_tmp0_);
		child = _tmp1_;
		{
			gboolean _tmp2_ = FALSE;
			_tmp2_ = TRUE;
			while (TRUE) {
				GtkWidget* _tmp3_;
				GtkWidget* next = NULL;
				GtkWidget* _tmp4_;
				GtkWidget* _tmp5_;
				GtkWidget* _tmp6_;
				GtkWidget* _tmp7_;
				GtkWidget* _tmp8_;
				GtkWidget* _tmp9_;
				if (!_tmp2_) {
				}
				_tmp2_ = FALSE;
				_tmp3_ = child;
				if (!(_tmp3_ != NULL)) {
					break;
				}
				_tmp4_ = child;
				_tmp5_ = gtk_widget_get_next_sibling (_tmp4_);
				_tmp6_ = _g_object_ref0 (_tmp5_);
				next = _tmp6_;
				_tmp7_ = child;
				gtk_box_remove ((GtkBox*) self, _tmp7_);
				_tmp8_ = next;
				_tmp9_ = _g_object_ref0 (_tmp8_);
				_g_object_unref0 (child);
				child = _tmp9_;
				_g_object_unref0 (next);
			}
		}
		_g_object_unref0 (child);
	}
	_tmp10_ = services_settings_get_default ();
	_tmp11_ = _tmp10_;
	_tmp12_ = _tmp11_->settings;
	_tmp13_ = g_settings_get_enum (_tmp12_, "start-week");
	_g_object_unref0 (_tmp11_);
	start_week = _tmp13_;
	_tmp14_ = self->priv->day_labels;
	_tmp14__length1 = self->priv->day_labels_length1;
	_tmp16_ = widgets_calendar_calendar_week_rotate_left (self, _tmp14_, (gint) _tmp14__length1, start_week, &_tmp15_);
	rotated_labels = _tmp16_;
	rotated_labels_length1 = _tmp15_;
	_rotated_labels_size_ = rotated_labels_length1;
	_tmp17_ = rotated_labels;
	_tmp17__length1 = rotated_labels_length1;
	{
		GtkLabel** label_collection = NULL;
		gint label_collection_length1 = 0;
		gint _label_collection_size_ = 0;
		gint label_it = 0;
		label_collection = _tmp17_;
		label_collection_length1 = _tmp17__length1;
		for (label_it = 0; label_it < label_collection_length1; label_it = label_it + 1) {
			GtkLabel* _tmp18_;
			GtkLabel* label = NULL;
			_tmp18_ = _g_object_ref0 (label_collection[label_it]);
			label = _tmp18_;
			{
				GtkLabel* _tmp19_;
				_tmp19_ = label;
				gtk_box_append ((GtkBox*) self, (GtkWidget*) _tmp19_);
				_g_object_unref0 (label);
			}
		}
	}
	rotated_labels = (_vala_array_free (rotated_labels, rotated_labels_length1, (GDestroyNotify) g_object_unref), NULL);
}

static GtkLabel**
widgets_calendar_calendar_week_rotate_left (WidgetsCalendarCalendarWeek* self,
                                            GtkLabel** array,
                                            gint array_length1,
                                            gint positions,
                                            gint* result_length1)
{
	GtkLabel** rotated = NULL;
	GtkLabel** _tmp0_;
	gint rotated_length1;
	gint _rotated_size_;
	GtkLabel** _tmp6_;
	gint _tmp6__length1;
	GtkLabel** result;
	g_return_val_if_fail (self != NULL, NULL);
	positions = ((positions % array_length1) + array_length1) % array_length1;
	_tmp0_ = g_new0 (GtkLabel*, array_length1 + 1);
	rotated = _tmp0_;
	rotated_length1 = array_length1;
	_rotated_size_ = rotated_length1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp1_ = FALSE;
			_tmp1_ = TRUE;
			while (TRUE) {
				GtkLabel** _tmp3_;
				gint _tmp3__length1;
				GtkLabel* _tmp4_;
				GtkLabel* _tmp5_;
				if (!_tmp1_) {
					gint _tmp2_;
					_tmp2_ = i;
					i = _tmp2_ + 1;
				}
				_tmp1_ = FALSE;
				if (!(i < array_length1)) {
					break;
				}
				_tmp3_ = rotated;
				_tmp3__length1 = rotated_length1;
				_tmp4_ = array[(i + positions) % array_length1];
				_tmp5_ = _g_object_ref0 (_tmp4_);
				_g_object_unref0 (_tmp3_[i]);
				_tmp3_[i] = _tmp5_;
			}
		}
	}
	_tmp6_ = rotated;
	_tmp6__length1 = rotated_length1;
	if (result_length1) {
		*result_length1 = _tmp6__length1;
	}
	result = _tmp6_;
	return result;
}

WidgetsCalendarCalendarWeek*
widgets_calendar_calendar_week_construct (GType object_type)
{
	WidgetsCalendarCalendarWeek * self = NULL;
	self = (WidgetsCalendarCalendarWeek*) g_object_new (object_type, NULL);
	return self;
}

WidgetsCalendarCalendarWeek*
widgets_calendar_calendar_week_new (void)
{
	return widgets_calendar_calendar_week_construct (WIDGETS_CALENDAR_TYPE_CALENDAR_WEEK);
}

void
widgets_calendar_calendar_week_set_margin_horizontal (WidgetsCalendarCalendarWeek* self,
                                                      gint value)
{
	g_return_if_fail (self != NULL);
	gtk_widget_set_margin_start ((GtkWidget*) self, value);
	gtk_widget_set_margin_end ((GtkWidget*) self, value);
	g_object_notify_by_pspec ((GObject *) self, widgets_calendar_calendar_week_properties[WIDGETS_CALENDAR_CALENDAR_WEEK_MARGIN_HORIZONTAL_PROPERTY]);
}

static GObject *
widgets_calendar_calendar_week_constructor (GType type,
                                            guint n_construct_properties,
                                            GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	WidgetsCalendarCalendarWeek * self;
	GtkLabel* _tmp0_;
	GtkLabel* _tmp1_;
	GtkLabel* _tmp2_;
	GtkLabel* _tmp3_;
	GtkLabel* _tmp4_;
	GtkLabel* _tmp5_;
	GtkLabel* _tmp6_;
	GtkLabel** _tmp7_;
	gchar** full_day_names = NULL;
	gchar* _tmp8_;
	gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* _tmp12_;
	gchar* _tmp13_;
	gchar* _tmp14_;
	gchar** _tmp15_;
	gint full_day_names_length1;
	gint _full_day_names_size_;
	GtkLabel** _tmp23_;
	gint _tmp23__length1;
	parent_class = G_OBJECT_CLASS (widgets_calendar_calendar_week_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, WIDGETS_CALENDAR_TYPE_CALENDAR_WEEK, WidgetsCalendarCalendarWeek);
	gtk_orientable_set_orientation ((GtkOrientable*) self, GTK_ORIENTATION_HORIZONTAL);
	gtk_box_set_homogeneous ((GtkBox*) self, TRUE);
	gtk_widget_set_valign ((GtkWidget*) self, GTK_ALIGN_CENTER);
	gtk_widget_set_margin_start ((GtkWidget*) self, 6);
	gtk_widget_set_margin_end ((GtkWidget*) self, 6);
	gtk_widget_set_margin_top ((GtkWidget*) self, 6);
	gtk_widget_set_margin_bottom ((GtkWidget*) self, 6);
	_tmp0_ = (GtkLabel*) gtk_label_new (_ ("Su"));
	g_object_ref_sink (_tmp0_);
	_tmp1_ = (GtkLabel*) gtk_label_new (_ ("Mo"));
	g_object_ref_sink (_tmp1_);
	_tmp2_ = (GtkLabel*) gtk_label_new (_ ("Tu"));
	g_object_ref_sink (_tmp2_);
	_tmp3_ = (GtkLabel*) gtk_label_new (_ ("We"));
	g_object_ref_sink (_tmp3_);
	_tmp4_ = (GtkLabel*) gtk_label_new (_ ("Th"));
	g_object_ref_sink (_tmp4_);
	_tmp5_ = (GtkLabel*) gtk_label_new (_ ("Fr"));
	g_object_ref_sink (_tmp5_);
	_tmp6_ = (GtkLabel*) gtk_label_new (_ ("Sa"));
	g_object_ref_sink (_tmp6_);
	_tmp7_ = g_new0 (GtkLabel*, 7 + 1);
	_tmp7_[0] = _tmp0_;
	_tmp7_[1] = _tmp1_;
	_tmp7_[2] = _tmp2_;
	_tmp7_[3] = _tmp3_;
	_tmp7_[4] = _tmp4_;
	_tmp7_[5] = _tmp5_;
	_tmp7_[6] = _tmp6_;
	self->priv->day_labels = (_vala_array_free (self->priv->day_labels, self->priv->day_labels_length1, (GDestroyNotify) g_object_unref), NULL);
	self->priv->day_labels = _tmp7_;
	self->priv->day_labels_length1 = 7;
	self->priv->_day_labels_size_ = self->priv->day_labels_length1;
	_tmp8_ = g_strdup (_ ("Sunday"));
	_tmp9_ = g_strdup (_ ("Monday"));
	_tmp10_ = g_strdup (_ ("Tuesday"));
	_tmp11_ = g_strdup (_ ("Wednesday"));
	_tmp12_ = g_strdup (_ ("Thursday"));
	_tmp13_ = g_strdup (_ ("Friday"));
	_tmp14_ = g_strdup (_ ("Saturday"));
	_tmp15_ = g_new0 (gchar*, 7 + 1);
	_tmp15_[0] = _tmp8_;
	_tmp15_[1] = _tmp9_;
	_tmp15_[2] = _tmp10_;
	_tmp15_[3] = _tmp11_;
	_tmp15_[4] = _tmp12_;
	_tmp15_[5] = _tmp13_;
	_tmp15_[6] = _tmp14_;
	full_day_names = _tmp15_;
	full_day_names_length1 = 7;
	_full_day_names_size_ = full_day_names_length1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp16_ = FALSE;
			_tmp16_ = TRUE;
			while (TRUE) {
				GtkLabel** _tmp18_;
				gint _tmp18__length1;
				GtkLabel** _tmp19_;
				gint _tmp19__length1;
				GtkLabel* _tmp20_;
				gchar** _tmp21_;
				gint _tmp21__length1;
				const gchar* _tmp22_;
				if (!_tmp16_) {
					gint _tmp17_;
					_tmp17_ = i;
					i = _tmp17_ + 1;
				}
				_tmp16_ = FALSE;
				_tmp18_ = self->priv->day_labels;
				_tmp18__length1 = self->priv->day_labels_length1;
				if (!(i < _tmp18__length1)) {
					break;
				}
				_tmp19_ = self->priv->day_labels;
				_tmp19__length1 = self->priv->day_labels_length1;
				_tmp20_ = _tmp19_[i];
				_tmp21_ = full_day_names;
				_tmp21__length1 = full_day_names_length1;
				_tmp22_ = _tmp21_[i];
				gtk_accessible_update_property ((GtkAccessible*) _tmp20_, GTK_ACCESSIBLE_PROPERTY_LABEL, _tmp22_, -1, -1);
			}
		}
	}
	_tmp23_ = self->priv->day_labels;
	_tmp23__length1 = self->priv->day_labels_length1;
	{
		GtkLabel** label_collection = NULL;
		gint label_collection_length1 = 0;
		gint _label_collection_size_ = 0;
		gint label_it = 0;
		label_collection = _tmp23_;
		label_collection_length1 = _tmp23__length1;
		for (label_it = 0; label_it < label_collection_length1; label_it = label_it + 1) {
			GtkLabel* _tmp24_;
			GtkLabel* label = NULL;
			_tmp24_ = _g_object_ref0 (label_collection[label_it]);
			label = _tmp24_;
			{
				GtkLabel* _tmp25_;
				GtkLabel* _tmp26_;
				_tmp25_ = label;
				gtk_widget_add_css_class ((GtkWidget*) _tmp25_, "dimmed");
				_tmp26_ = label;
				gtk_box_append ((GtkBox*) self, (GtkWidget*) _tmp26_);
				_g_object_unref0 (label);
			}
		}
	}
	widgets_calendar_calendar_week_update (self);
	full_day_names = (_vala_array_free (full_day_names, full_day_names_length1, (GDestroyNotify) g_free), NULL);
	return obj;
}

static void
widgets_calendar_calendar_week_class_init (WidgetsCalendarCalendarWeekClass * klass,
                                           gpointer klass_data)
{
	widgets_calendar_calendar_week_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &WidgetsCalendarCalendarWeek_private_offset);
	G_OBJECT_CLASS (klass)->set_property = _vala_widgets_calendar_calendar_week_set_property;
	G_OBJECT_CLASS (klass)->constructor = widgets_calendar_calendar_week_constructor;
	G_OBJECT_CLASS (klass)->finalize = widgets_calendar_calendar_week_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), WIDGETS_CALENDAR_CALENDAR_WEEK_MARGIN_HORIZONTAL_PROPERTY, widgets_calendar_calendar_week_properties[WIDGETS_CALENDAR_CALENDAR_WEEK_MARGIN_HORIZONTAL_PROPERTY] = g_param_spec_int ("margin-horizontal", "margin-horizontal", "margin-horizontal", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE));
}

static void
widgets_calendar_calendar_week_instance_init (WidgetsCalendarCalendarWeek * self,
                                              gpointer klass)
{
	self->priv = widgets_calendar_calendar_week_get_instance_private (self);
}

static void
widgets_calendar_calendar_week_finalize (GObject * obj)
{
	WidgetsCalendarCalendarWeek * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, WIDGETS_CALENDAR_TYPE_CALENDAR_WEEK, WidgetsCalendarCalendarWeek);
	g_debug ("CalendarWeek.vala:33: Destroying - Widgets.Calendar.CalendarWeek\n");
	self->priv->day_labels = (_vala_array_free (self->priv->day_labels, self->priv->day_labels_length1, (GDestroyNotify) g_object_unref), NULL);
	G_OBJECT_CLASS (widgets_calendar_calendar_week_parent_class)->finalize (obj);
}

static GType
widgets_calendar_calendar_week_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (WidgetsCalendarCalendarWeekClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) widgets_calendar_calendar_week_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (WidgetsCalendarCalendarWeek), 0, (GInstanceInitFunc) widgets_calendar_calendar_week_instance_init, NULL };
	GType widgets_calendar_calendar_week_type_id;
	widgets_calendar_calendar_week_type_id = g_type_register_static (gtk_box_get_type (), "WidgetsCalendarCalendarWeek", &g_define_type_info, 0);
	WidgetsCalendarCalendarWeek_private_offset = g_type_add_instance_private (widgets_calendar_calendar_week_type_id, sizeof (WidgetsCalendarCalendarWeekPrivate));
	return widgets_calendar_calendar_week_type_id;
}

GType
widgets_calendar_calendar_week_get_type (void)
{
	static volatile gsize widgets_calendar_calendar_week_type_id__once = 0;
	if (g_once_init_enter (&widgets_calendar_calendar_week_type_id__once)) {
		GType widgets_calendar_calendar_week_type_id;
		widgets_calendar_calendar_week_type_id = widgets_calendar_calendar_week_get_type_once ();
		g_once_init_leave (&widgets_calendar_calendar_week_type_id__once, widgets_calendar_calendar_week_type_id);
	}
	return widgets_calendar_calendar_week_type_id__once;
}

static void
_vala_widgets_calendar_calendar_week_set_property (GObject * object,
                                                   guint property_id,
                                                   const GValue * value,
                                                   GParamSpec * pspec)
{
	WidgetsCalendarCalendarWeek * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, WIDGETS_CALENDAR_TYPE_CALENDAR_WEEK, WidgetsCalendarCalendarWeek);
	switch (property_id) {
		case WIDGETS_CALENDAR_CALENDAR_WEEK_MARGIN_HORIZONTAL_PROPERTY:
		widgets_calendar_calendar_week_set_margin_horizontal (self, g_value_get_int (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;
}

