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

/*
 * Copyright (C) 2012-2014 Daiki Ueno <ueno@gnu.org>
 * Copyright (C) 2012-2014 Red Hat, Inc.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "libkkc/libkkc.h"
#include <gee.h>
#include <glib-object.h>
#include <glib.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  {
	KKC_SEGMENT_LIST_0_PROPERTY,
	KKC_SEGMENT_LIST_CURSOR_POS_PROPERTY,
	KKC_SEGMENT_LIST_SIZE_PROPERTY,
	KKC_SEGMENT_LIST_NUM_PROPERTIES
};
static GParamSpec* kkc_segment_list_properties[KKC_SEGMENT_LIST_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))

struct _KkcSegmentListPrivate {
	GeeList* segments;
	GeeList* offsets;
	gint _cursor_pos;
};

static gint KkcSegmentList_private_offset;
static gpointer kkc_segment_list_parent_class = NULL;

static void kkc_segment_list_finalize (GObject * obj);
static GType kkc_segment_list_get_type_once (void);
static void _vala_kkc_segment_list_get_property (GObject * object,
                                          guint property_id,
                                          GValue * value,
                                          GParamSpec * pspec);
static void _vala_kkc_segment_list_set_property (GObject * object,
                                          guint property_id,
                                          const GValue * value,
                                          GParamSpec * pspec);

static inline gpointer
kkc_segment_list_get_instance_private (KkcSegmentList* self)
{
	return G_STRUCT_MEMBER_P (self, KkcSegmentList_private_offset);
}

void
kkc_segment_list_clear (KkcSegmentList* self)
{
	GeeList* _tmp0_;
	GeeList* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->segments;
	gee_collection_clear ((GeeCollection*) _tmp0_);
	_tmp1_ = self->priv->offsets;
	gee_collection_clear ((GeeCollection*) _tmp1_);
	kkc_segment_list_set_cursor_pos (self, -1);
}

void
kkc_segment_list_add (KkcSegmentList* self,
                      KkcSegment* segment)
{
	GeeList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	GeeList* _tmp3_;
	gint _tmp4_;
	gint _tmp5_;
	GeeList* _tmp6_;
	gint _tmp7_;
	gint _tmp8_;
	GeeList* _tmp24_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (segment != NULL);
	_tmp0_ = self->priv->offsets;
	_tmp1_ = gee_collection_get_size ((GeeCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = self->priv->segments;
	_tmp4_ = gee_collection_get_size ((GeeCollection*) _tmp3_);
	_tmp5_ = _tmp4_;
	g_return_if_fail (_tmp2_ == _tmp5_);
	_tmp6_ = self->priv->offsets;
	_tmp7_ = gee_collection_get_size ((GeeCollection*) _tmp6_);
	_tmp8_ = _tmp7_;
	if (_tmp8_ > 0) {
		gint last_offset = 0;
		GeeList* _tmp9_;
		GeeList* _tmp10_;
		gint _tmp11_;
		gint _tmp12_;
		gpointer _tmp13_;
		KkcSegment* last_segment = NULL;
		GeeList* _tmp14_;
		GeeList* _tmp15_;
		gint _tmp16_;
		gint _tmp17_;
		gpointer _tmp18_;
		GeeList* _tmp19_;
		KkcSegment* _tmp20_;
		const gchar* _tmp21_;
		const gchar* _tmp22_;
		_tmp9_ = self->priv->offsets;
		_tmp10_ = self->priv->offsets;
		_tmp11_ = gee_collection_get_size ((GeeCollection*) _tmp10_);
		_tmp12_ = _tmp11_;
		_tmp13_ = gee_list_get (_tmp9_, _tmp12_ - 1);
		last_offset = (gint) ((gintptr) _tmp13_);
		_tmp14_ = self->priv->segments;
		_tmp15_ = self->priv->segments;
		_tmp16_ = gee_collection_get_size ((GeeCollection*) _tmp15_);
		_tmp17_ = _tmp16_;
		_tmp18_ = gee_list_get (_tmp14_, _tmp17_ - 1);
		last_segment = (KkcSegment*) _tmp18_;
		_tmp19_ = self->priv->offsets;
		_tmp20_ = last_segment;
		_tmp21_ = kkc_segment_get_input (_tmp20_);
		_tmp22_ = _tmp21_;
		gee_collection_add ((GeeCollection*) _tmp19_, (gpointer) ((gintptr) (last_offset + g_utf8_strlen (_tmp22_, (gssize) -1))));
		_g_object_unref0 (last_segment);
	} else {
		GeeList* _tmp23_;
		_tmp23_ = self->priv->offsets;
		gee_collection_add ((GeeCollection*) _tmp23_, (gpointer) ((gintptr) 0));
	}
	_tmp24_ = self->priv->segments;
	gee_collection_add ((GeeCollection*) _tmp24_, segment);
}

KkcSegment*
kkc_segment_list_get (KkcSegmentList* self,
                      gint index)
{
	GeeList* _tmp0_;
	gpointer _tmp1_;
	KkcSegment* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->segments;
	_tmp1_ = gee_list_get (_tmp0_, index);
	result = (KkcSegment*) _tmp1_;
	return result;
}

gint
kkc_segment_list_get_offset (KkcSegmentList* self,
                             gint index)
{
	GeeList* _tmp0_;
	gpointer _tmp1_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	_tmp0_ = self->priv->offsets;
	_tmp1_ = gee_list_get (_tmp0_, index);
	result = (gint) ((gintptr) _tmp1_);
	return result;
}

void
kkc_segment_list_set_segments (KkcSegmentList* self,
                               KkcSegment* segment)
{
	gint offset = 0;
	GeeList* _tmp0_;
	GeeList* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (segment != NULL);
	offset = 0;
	_tmp0_ = self->priv->segments;
	gee_collection_clear ((GeeCollection*) _tmp0_);
	_tmp1_ = self->priv->offsets;
	gee_collection_clear ((GeeCollection*) _tmp1_);
	while (TRUE) {
		GeeList* _tmp2_;
		const gchar* _tmp3_;
		const gchar* _tmp4_;
		const gchar* _tmp5_;
		const gchar* _tmp6_;
		KkcSegment* _tmp7_;
		KkcSegment* _tmp8_;
		GeeList* _tmp9_;
		const gchar* _tmp10_;
		const gchar* _tmp11_;
		KkcSegment* _tmp12_;
		if (!(segment != NULL)) {
			break;
		}
		_tmp2_ = self->priv->segments;
		_tmp3_ = kkc_segment_get_input (segment);
		_tmp4_ = _tmp3_;
		_tmp5_ = kkc_segment_get_output (segment);
		_tmp6_ = _tmp5_;
		_tmp7_ = kkc_segment_new (_tmp4_, _tmp6_);
		_tmp8_ = _tmp7_;
		gee_collection_add ((GeeCollection*) _tmp2_, _tmp8_);
		_g_object_unref0 (_tmp8_);
		_tmp9_ = self->priv->offsets;
		gee_collection_add ((GeeCollection*) _tmp9_, (gpointer) ((gintptr) offset));
		_tmp10_ = kkc_segment_get_input (segment);
		_tmp11_ = _tmp10_;
		offset += g_utf8_strlen (_tmp11_, (gssize) -1);
		_tmp12_ = segment->next;
		segment = _tmp12_;
	}
}

gboolean
kkc_segment_list_first_segment (KkcSegmentList* self)
{
	GeeList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = self->priv->segments;
	_tmp1_ = gee_collection_get_size ((GeeCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	if (_tmp2_ > 0) {
		kkc_segment_list_set_cursor_pos (self, 0);
		result = TRUE;
		return result;
	}
	result = FALSE;
	return result;
}

gboolean
kkc_segment_list_last_segment (KkcSegmentList* self)
{
	GeeList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = self->priv->segments;
	_tmp1_ = gee_collection_get_size ((GeeCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	if (_tmp2_ > 0) {
		GeeList* _tmp3_;
		gint _tmp4_;
		gint _tmp5_;
		_tmp3_ = self->priv->segments;
		_tmp4_ = gee_collection_get_size ((GeeCollection*) _tmp3_);
		_tmp5_ = _tmp4_;
		kkc_segment_list_set_cursor_pos (self, _tmp5_ - 1);
		result = TRUE;
		return result;
	}
	result = FALSE;
	return result;
}

void
kkc_segment_list_next_segment (KkcSegmentList* self)
{
	gint _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	gint _tmp4_;
	gint _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = kkc_segment_list_get_cursor_pos (self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == -1) {
		return;
	}
	_tmp2_ = kkc_segment_list_get_cursor_pos (self);
	_tmp3_ = _tmp2_;
	_tmp4_ = kkc_segment_list_get_size (self);
	_tmp5_ = _tmp4_;
	kkc_segment_list_set_cursor_pos (self, CLAMP (_tmp3_ + 1, 0, _tmp5_ - 1));
}

void
kkc_segment_list_previous_segment (KkcSegmentList* self)
{
	gint _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	gint _tmp4_;
	gint _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = kkc_segment_list_get_cursor_pos (self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == -1) {
		return;
	}
	_tmp2_ = kkc_segment_list_get_cursor_pos (self);
	_tmp3_ = _tmp2_;
	_tmp4_ = kkc_segment_list_get_size (self);
	_tmp5_ = _tmp4_;
	kkc_segment_list_set_cursor_pos (self, CLAMP (_tmp3_ - 1, 0, _tmp5_ - 1));
}

gchar*
kkc_segment_list_get_output (KkcSegmentList* self)
{
	GString* builder = NULL;
	GString* _tmp0_;
	GString* _tmp13_;
	const gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_string_new ("");
	builder = _tmp0_;
	{
		GeeList* _segment_list = NULL;
		GeeList* _tmp1_;
		gint _segment_size = 0;
		GeeList* _tmp2_;
		gint _tmp3_;
		gint _tmp4_;
		gint _segment_index = 0;
		_tmp1_ = self->priv->segments;
		_segment_list = _tmp1_;
		_tmp2_ = _segment_list;
		_tmp3_ = gee_collection_get_size ((GeeCollection*) _tmp2_);
		_tmp4_ = _tmp3_;
		_segment_size = _tmp4_;
		_segment_index = -1;
		while (TRUE) {
			gint _tmp5_;
			gint _tmp6_;
			KkcSegment* segment = NULL;
			GeeList* _tmp7_;
			gpointer _tmp8_;
			GString* _tmp9_;
			KkcSegment* _tmp10_;
			const gchar* _tmp11_;
			const gchar* _tmp12_;
			_segment_index = _segment_index + 1;
			_tmp5_ = _segment_index;
			_tmp6_ = _segment_size;
			if (!(_tmp5_ < _tmp6_)) {
				break;
			}
			_tmp7_ = _segment_list;
			_tmp8_ = gee_list_get (_tmp7_, _segment_index);
			segment = (KkcSegment*) _tmp8_;
			_tmp9_ = builder;
			_tmp10_ = segment;
			_tmp11_ = kkc_segment_get_output (_tmp10_);
			_tmp12_ = _tmp11_;
			g_string_append (_tmp9_, _tmp12_);
			_g_object_unref0 (segment);
		}
	}
	_tmp13_ = builder;
	_tmp14_ = _tmp13_->str;
	_tmp15_ = g_strdup (_tmp14_);
	result = _tmp15_;
	_g_string_free0 (builder);
	return result;
}

gchar*
kkc_segment_list_get_input (KkcSegmentList* self)
{
	GString* builder = NULL;
	GString* _tmp0_;
	GString* _tmp13_;
	const gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_string_new ("");
	builder = _tmp0_;
	{
		GeeList* _segment_list = NULL;
		GeeList* _tmp1_;
		gint _segment_size = 0;
		GeeList* _tmp2_;
		gint _tmp3_;
		gint _tmp4_;
		gint _segment_index = 0;
		_tmp1_ = self->priv->segments;
		_segment_list = _tmp1_;
		_tmp2_ = _segment_list;
		_tmp3_ = gee_collection_get_size ((GeeCollection*) _tmp2_);
		_tmp4_ = _tmp3_;
		_segment_size = _tmp4_;
		_segment_index = -1;
		while (TRUE) {
			gint _tmp5_;
			gint _tmp6_;
			KkcSegment* segment = NULL;
			GeeList* _tmp7_;
			gpointer _tmp8_;
			GString* _tmp9_;
			KkcSegment* _tmp10_;
			const gchar* _tmp11_;
			const gchar* _tmp12_;
			_segment_index = _segment_index + 1;
			_tmp5_ = _segment_index;
			_tmp6_ = _segment_size;
			if (!(_tmp5_ < _tmp6_)) {
				break;
			}
			_tmp7_ = _segment_list;
			_tmp8_ = gee_list_get (_tmp7_, _segment_index);
			segment = (KkcSegment*) _tmp8_;
			_tmp9_ = builder;
			_tmp10_ = segment;
			_tmp11_ = kkc_segment_get_input (_tmp10_);
			_tmp12_ = _tmp11_;
			g_string_append (_tmp9_, _tmp12_);
			_g_object_unref0 (segment);
		}
	}
	_tmp13_ = builder;
	_tmp14_ = _tmp13_->str;
	_tmp15_ = g_strdup (_tmp14_);
	result = _tmp15_;
	_g_string_free0 (builder);
	return result;
}

KkcSegment**
kkc_segment_list_to_array (KkcSegmentList* self,
                           gint* result_length1)
{
	GeeList* _tmp0_;
	gint _tmp1_ = 0;
	gpointer* _tmp2_;
	KkcSegment** _tmp3_;
	gint _tmp3__length1;
	KkcSegment** result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->segments;
	_tmp2_ = gee_collection_to_array ((GeeCollection*) _tmp0_, &_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp3__length1 = _tmp1_;
	if (result_length1) {
		*result_length1 = _tmp3__length1;
	}
	result = _tmp3_;
	return result;
}

KkcSegmentList*
kkc_segment_list_construct (GType object_type)
{
	KkcSegmentList * self = NULL;
	self = (KkcSegmentList*) g_object_new (object_type, NULL);
	return self;
}

KkcSegmentList*
kkc_segment_list_new (void)
{
	return kkc_segment_list_construct (KKC_TYPE_SEGMENT_LIST);
}

gint
kkc_segment_list_get_cursor_pos (KkcSegmentList* self)
{
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	result = self->priv->_cursor_pos;
	return result;
}

void
kkc_segment_list_set_cursor_pos (KkcSegmentList* self,
                                 gint value)
{
	g_return_if_fail (self != NULL);
	self->priv->_cursor_pos = value;
	g_object_notify_by_pspec ((GObject *) self, kkc_segment_list_properties[KKC_SEGMENT_LIST_CURSOR_POS_PROPERTY]);
}

gint
kkc_segment_list_get_size (KkcSegmentList* self)
{
	gint result;
	GeeList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	g_return_val_if_fail (self != NULL, -1);
	_tmp0_ = self->priv->segments;
	_tmp1_ = gee_collection_get_size ((GeeCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	result = _tmp2_;
	return result;
}

static void
kkc_segment_list_class_init (KkcSegmentListClass * klass,
                             gpointer klass_data)
{
	kkc_segment_list_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &KkcSegmentList_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_kkc_segment_list_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_kkc_segment_list_set_property;
	G_OBJECT_CLASS (klass)->finalize = kkc_segment_list_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), KKC_SEGMENT_LIST_CURSOR_POS_PROPERTY, kkc_segment_list_properties[KKC_SEGMENT_LIST_CURSOR_POS_PROPERTY] = g_param_spec_int ("cursor-pos", "cursor-pos", "cursor-pos", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), KKC_SEGMENT_LIST_SIZE_PROPERTY, kkc_segment_list_properties[KKC_SEGMENT_LIST_SIZE_PROPERTY] = g_param_spec_int ("size", "size", "size", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
kkc_segment_list_instance_init (KkcSegmentList * self,
                                gpointer klass)
{
	GeeArrayList* _tmp0_;
	GeeArrayList* _tmp1_;
	self->priv = kkc_segment_list_get_instance_private (self);
	_tmp0_ = gee_array_list_new (KKC_TYPE_SEGMENT, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	self->priv->segments = (GeeList*) _tmp0_;
	_tmp1_ = gee_array_list_new (G_TYPE_INT, NULL, NULL, NULL, NULL, NULL);
	self->priv->offsets = (GeeList*) _tmp1_;
	self->priv->_cursor_pos = -1;
}

static void
kkc_segment_list_finalize (GObject * obj)
{
	KkcSegmentList * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, KKC_TYPE_SEGMENT_LIST, KkcSegmentList);
	_g_object_unref0 (self->priv->segments);
	_g_object_unref0 (self->priv->offsets);
	G_OBJECT_CLASS (kkc_segment_list_parent_class)->finalize (obj);
}

static GType
kkc_segment_list_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (KkcSegmentListClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) kkc_segment_list_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (KkcSegmentList), 0, (GInstanceInitFunc) kkc_segment_list_instance_init, NULL };
	GType kkc_segment_list_type_id;
	kkc_segment_list_type_id = g_type_register_static (G_TYPE_OBJECT, "KkcSegmentList", &g_define_type_info, 0);
	KkcSegmentList_private_offset = g_type_add_instance_private (kkc_segment_list_type_id, sizeof (KkcSegmentListPrivate));
	return kkc_segment_list_type_id;
}

GType
kkc_segment_list_get_type (void)
{
	static volatile gsize kkc_segment_list_type_id__once = 0;
	if (g_once_init_enter (&kkc_segment_list_type_id__once)) {
		GType kkc_segment_list_type_id;
		kkc_segment_list_type_id = kkc_segment_list_get_type_once ();
		g_once_init_leave (&kkc_segment_list_type_id__once, kkc_segment_list_type_id);
	}
	return kkc_segment_list_type_id__once;
}

static void
_vala_kkc_segment_list_get_property (GObject * object,
                                     guint property_id,
                                     GValue * value,
                                     GParamSpec * pspec)
{
	KkcSegmentList * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, KKC_TYPE_SEGMENT_LIST, KkcSegmentList);
	switch (property_id) {
		case KKC_SEGMENT_LIST_CURSOR_POS_PROPERTY:
		g_value_set_int (value, kkc_segment_list_get_cursor_pos (self));
		break;
		case KKC_SEGMENT_LIST_SIZE_PROPERTY:
		g_value_set_int (value, kkc_segment_list_get_size (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_kkc_segment_list_set_property (GObject * object,
                                     guint property_id,
                                     const GValue * value,
                                     GParamSpec * pspec)
{
	KkcSegmentList * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, KKC_TYPE_SEGMENT_LIST, KkcSegmentList);
	switch (property_id) {
		case KKC_SEGMENT_LIST_CURSOR_POS_PROPERTY:
		kkc_segment_list_set_cursor_pos (self, g_value_get_int (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

