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

/*
 * Copyright © 2025 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 "chrono.h"
#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.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

enum  {
	CHRONO_TIME_PARSER_0_PROPERTY,
	CHRONO_TIME_PARSER_NUM_PROPERTIES
};
static GParamSpec* chrono_time_parser_properties[CHRONO_TIME_PARSER_NUM_PROPERTIES];
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_match_info_unref0(var) ((var == NULL) ? NULL : (var = (g_match_info_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))

struct _ChronoTimeParserPrivate {
	GRegex* time_24h_regex;
	GRegex* time_12h_regex;
};

static gint ChronoTimeParser_private_offset;
static gpointer chrono_time_parser_parent_class = NULL;

static ChronoParseResult* chrono_time_parser_parse_12h (ChronoTimeParser* self,
                                                 const gchar* text);
static ChronoParseResult* chrono_time_parser_parse_24h (ChronoTimeParser* self,
                                                 const gchar* text);
static void chrono_time_parser_finalize (GObject * obj);
static GType chrono_time_parser_get_type_once (void);

static inline gpointer
chrono_time_parser_get_instance_private (ChronoTimeParser* self)
{
	return G_STRUCT_MEMBER_P (self, ChronoTimeParser_private_offset);
}

ChronoTimeParser*
chrono_time_parser_construct (GType object_type)
{
	ChronoTimeParser * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (ChronoTimeParser*) g_object_new (object_type, NULL);
	{
		GRegex* _tmp0_ = NULL;
		GRegex* _tmp1_;
		GRegex* _tmp2_;
		GRegex* _tmp3_ = NULL;
		GRegex* _tmp4_;
		GRegex* _tmp5_;
		_tmp1_ = g_regex_new ("(\\d{1,2}):(\\d{2})", G_REGEX_CASELESS, 0, &_inner_error0_);
		_tmp0_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		_g_regex_unref0 (self->priv->time_24h_regex);
		self->priv->time_24h_regex = _tmp2_;
		_tmp4_ = g_regex_new ("(\\d{1,2})(?::(\\d{2}))?\\s*(am|pm)", G_REGEX_CASELESS, 0, &_inner_error0_);
		_tmp3_ = _tmp4_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (_tmp0_);
			goto __catch0_g_error;
		}
		_tmp5_ = _tmp3_;
		_tmp3_ = NULL;
		_g_regex_unref0 (self->priv->time_12h_regex);
		self->priv->time_12h_regex = _tmp5_;
		_g_regex_unref0 (_tmp3_);
		_g_regex_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp6_;
		const gchar* _tmp7_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp6_ = e;
		_tmp7_ = _tmp6_->message;
		g_warning ("TimeParser.vala:51: Error creating time regex: %s", _tmp7_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	return self;
}

ChronoTimeParser*
chrono_time_parser_new (void)
{
	return chrono_time_parser_construct (CHRONO_TYPE_TIME_PARSER);
}

ChronoParseResult*
chrono_time_parser_parse (ChronoTimeParser* self,
                          const gchar* text)
{
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp0_;
	ChronoParseResult* _tmp1_;
	ChronoParseResult* _tmp2_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = chrono_time_parser_parse_12h (self, text);
	_result_ = _tmp0_;
	_tmp1_ = _result_;
	if (_tmp1_ != NULL) {
		result = _result_;
		return result;
	}
	_tmp2_ = chrono_time_parser_parse_24h (self, text);
	result = _tmp2_;
	_g_object_unref0 (_result_);
	return result;
}

static ChronoParseResult*
chrono_time_parser_parse_12h (ChronoTimeParser* self,
                              const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gint hour = 0;
	GMatchInfo* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gint _tmp6_;
	gint minute = 0;
	gchar* min_str = NULL;
	GMatchInfo* _tmp7_;
	gchar* _tmp8_;
	gboolean _tmp9_ = FALSE;
	const gchar* _tmp10_;
	gchar* period = NULL;
	GMatchInfo* _tmp15_;
	gchar* _tmp16_;
	gchar* _tmp17_;
	gchar* _tmp18_;
	gchar* _tmp19_;
	gboolean _tmp20_ = FALSE;
	const gchar* _tmp21_;
	GDateTime* now = NULL;
	GDateTime* _tmp24_;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp25_;
	ChronoParseResult* _tmp26_;
	GDateTime* _tmp27_;
	GDateTime* _tmp28_;
	GDateTime* _tmp29_;
	GDateTime* _tmp30_;
	GDateTime* _tmp31_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp32_;
	gint _tmp33_ = 0;
	gint _tmp34_ = 0;
	ChronoParseResult* _tmp35_;
	ChronoParseResult* _tmp36_;
	ChronoParseResult* _tmp37_;
	GMatchInfo* _tmp38_;
	gchar* _tmp39_;
	gchar* _tmp40_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = self->priv->time_12h_regex;
	_tmp2_ = g_regex_match (_tmp0_, text, 0, &_tmp1_);
	_g_match_info_unref0 (match);
	match = _tmp1_;
	if (!_tmp2_) {
		result = NULL;
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp3_ = match;
	_tmp4_ = g_match_info_fetch (_tmp3_, 1);
	_tmp5_ = _tmp4_;
	_tmp6_ = atoi (_tmp5_);
	_g_free0 (_tmp5_);
	hour = _tmp6_;
	minute = 0;
	_tmp7_ = match;
	_tmp8_ = g_match_info_fetch (_tmp7_, 2);
	min_str = _tmp8_;
	_tmp10_ = min_str;
	if (_tmp10_ != NULL) {
		const gchar* _tmp11_;
		gint _tmp12_;
		gint _tmp13_;
		_tmp11_ = min_str;
		_tmp12_ = strlen (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp9_ = _tmp13_ > 0;
	} else {
		_tmp9_ = FALSE;
	}
	if (_tmp9_) {
		const gchar* _tmp14_;
		_tmp14_ = min_str;
		minute = atoi (_tmp14_);
	}
	_tmp15_ = match;
	_tmp16_ = g_match_info_fetch (_tmp15_, 3);
	_tmp17_ = _tmp16_;
	_tmp18_ = g_utf8_strdown (_tmp17_, (gssize) -1);
	_tmp19_ = _tmp18_;
	_g_free0 (_tmp17_);
	period = _tmp19_;
	_tmp21_ = period;
	if (g_strcmp0 (_tmp21_, "pm") == 0) {
		_tmp20_ = hour < 12;
	} else {
		_tmp20_ = FALSE;
	}
	if (_tmp20_) {
		hour += 12;
	} else {
		gboolean _tmp22_ = FALSE;
		const gchar* _tmp23_;
		_tmp23_ = period;
		if (g_strcmp0 (_tmp23_, "am") == 0) {
			_tmp22_ = hour == 12;
		} else {
			_tmp22_ = FALSE;
		}
		if (_tmp22_) {
			hour = 0;
		}
	}
	_tmp24_ = g_date_time_new_now_local ();
	now = _tmp24_;
	_tmp25_ = chrono_parse_result_new ();
	_result_ = _tmp25_;
	_tmp26_ = _result_;
	_tmp27_ = now;
	_tmp28_ = now;
	_tmp29_ = now;
	_tmp30_ = g_date_time_new_local (g_date_time_get_year (_tmp27_), g_date_time_get_month (_tmp28_), g_date_time_get_day_of_month (_tmp29_), hour, minute, (gdouble) 0);
	_tmp31_ = _tmp30_;
	chrono_parse_result_set_date (_tmp26_, _tmp31_);
	_g_date_time_unref0 (_tmp31_);
	_tmp32_ = match;
	g_match_info_fetch_pos (_tmp32_, 0, &_tmp33_, &_tmp34_);
	start_pos = _tmp33_;
	end_pos = _tmp34_;
	_tmp35_ = _result_;
	chrono_parse_result_set_start_index (_tmp35_, start_pos);
	_tmp36_ = _result_;
	chrono_parse_result_set_end_index (_tmp36_, end_pos);
	_tmp37_ = _result_;
	_tmp38_ = match;
	_tmp39_ = g_match_info_fetch (_tmp38_, 0);
	_tmp40_ = _tmp39_;
	chrono_parse_result_set_matched_text (_tmp37_, _tmp40_);
	_g_free0 (_tmp40_);
	result = _result_;
	_g_date_time_unref0 (now);
	_g_free0 (period);
	_g_free0 (min_str);
	_g_match_info_unref0 (match);
	return result;
}

static ChronoParseResult*
chrono_time_parser_parse_24h (ChronoTimeParser* self,
                              const gchar* text)
{
	GMatchInfo* match = NULL;
	GRegex* _tmp0_;
	GMatchInfo* _tmp1_ = NULL;
	gboolean _tmp2_;
	gint hour = 0;
	GMatchInfo* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gint _tmp6_;
	gint minute = 0;
	GMatchInfo* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	gint _tmp10_;
	gboolean _tmp11_ = FALSE;
	GDateTime* now = NULL;
	GDateTime* _tmp12_;
	ChronoParseResult* _result_ = NULL;
	ChronoParseResult* _tmp13_;
	ChronoParseResult* _tmp14_;
	GDateTime* _tmp15_;
	GDateTime* _tmp16_;
	GDateTime* _tmp17_;
	GDateTime* _tmp18_;
	GDateTime* _tmp19_;
	gint start_pos = 0;
	gint end_pos = 0;
	GMatchInfo* _tmp20_;
	gint _tmp21_ = 0;
	gint _tmp22_ = 0;
	ChronoParseResult* _tmp23_;
	ChronoParseResult* _tmp24_;
	ChronoParseResult* _tmp25_;
	GMatchInfo* _tmp26_;
	gchar* _tmp27_;
	gchar* _tmp28_;
	ChronoParseResult* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = self->priv->time_24h_regex;
	_tmp2_ = g_regex_match (_tmp0_, text, 0, &_tmp1_);
	_g_match_info_unref0 (match);
	match = _tmp1_;
	if (!_tmp2_) {
		result = NULL;
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp3_ = match;
	_tmp4_ = g_match_info_fetch (_tmp3_, 1);
	_tmp5_ = _tmp4_;
	_tmp6_ = atoi (_tmp5_);
	_g_free0 (_tmp5_);
	hour = _tmp6_;
	_tmp7_ = match;
	_tmp8_ = g_match_info_fetch (_tmp7_, 2);
	_tmp9_ = _tmp8_;
	_tmp10_ = atoi (_tmp9_);
	_g_free0 (_tmp9_);
	minute = _tmp10_;
	if (hour > 23) {
		_tmp11_ = TRUE;
	} else {
		_tmp11_ = minute > 59;
	}
	if (_tmp11_) {
		result = NULL;
		_g_match_info_unref0 (match);
		return result;
	}
	_tmp12_ = g_date_time_new_now_local ();
	now = _tmp12_;
	_tmp13_ = chrono_parse_result_new ();
	_result_ = _tmp13_;
	_tmp14_ = _result_;
	_tmp15_ = now;
	_tmp16_ = now;
	_tmp17_ = now;
	_tmp18_ = g_date_time_new_local (g_date_time_get_year (_tmp15_), g_date_time_get_month (_tmp16_), g_date_time_get_day_of_month (_tmp17_), hour, minute, (gdouble) 0);
	_tmp19_ = _tmp18_;
	chrono_parse_result_set_date (_tmp14_, _tmp19_);
	_g_date_time_unref0 (_tmp19_);
	_tmp20_ = match;
	g_match_info_fetch_pos (_tmp20_, 0, &_tmp21_, &_tmp22_);
	start_pos = _tmp21_;
	end_pos = _tmp22_;
	_tmp23_ = _result_;
	chrono_parse_result_set_start_index (_tmp23_, start_pos);
	_tmp24_ = _result_;
	chrono_parse_result_set_end_index (_tmp24_, end_pos);
	_tmp25_ = _result_;
	_tmp26_ = match;
	_tmp27_ = g_match_info_fetch (_tmp26_, 0);
	_tmp28_ = _tmp27_;
	chrono_parse_result_set_matched_text (_tmp25_, _tmp28_);
	_g_free0 (_tmp28_);
	result = _result_;
	_g_date_time_unref0 (now);
	_g_match_info_unref0 (match);
	return result;
}

static void
chrono_time_parser_class_init (ChronoTimeParserClass * klass,
                               gpointer klass_data)
{
	chrono_time_parser_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChronoTimeParser_private_offset);
	G_OBJECT_CLASS (klass)->finalize = chrono_time_parser_finalize;
}

static void
chrono_time_parser_instance_init (ChronoTimeParser * self,
                                  gpointer klass)
{
	self->priv = chrono_time_parser_get_instance_private (self);
}

static void
chrono_time_parser_finalize (GObject * obj)
{
	ChronoTimeParser * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CHRONO_TYPE_TIME_PARSER, ChronoTimeParser);
	_g_regex_unref0 (self->priv->time_24h_regex);
	_g_regex_unref0 (self->priv->time_12h_regex);
	G_OBJECT_CLASS (chrono_time_parser_parent_class)->finalize (obj);
}

/**
     * Time format parser (language independent)
     * 
     * For examples:
     * - 14:30
     * - 23:45
     * - 2:30pm
     * - 3pm
     * - 15:00
     */
 G_GNUC_NO_INLINE static GType
chrono_time_parser_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChronoTimeParserClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chrono_time_parser_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChronoTimeParser), 0, (GInstanceInitFunc) chrono_time_parser_instance_init, NULL };
	GType chrono_time_parser_type_id;
	chrono_time_parser_type_id = g_type_register_static (G_TYPE_OBJECT, "ChronoTimeParser", &g_define_type_info, 0);
	ChronoTimeParser_private_offset = g_type_add_instance_private (chrono_time_parser_type_id, sizeof (ChronoTimeParserPrivate));
	return chrono_time_parser_type_id;
}

GType
chrono_time_parser_get_type (void)
{
	static gsize chrono_time_parser_type_id__once = 0;
	if (g_once_init_enter (&chrono_time_parser_type_id__once)) {
		GType chrono_time_parser_type_id;
		chrono_time_parser_type_id = chrono_time_parser_get_type_once ();
		g_once_init_leave (&chrono_time_parser_type_id__once, chrono_time_parser_type_id);
	}
	return chrono_time_parser_type_id__once;
}

