/* gcalc-equation.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from gcalc-equation.vala, do not modify */

/* gcalc-quation.vala
 *
 * Copyright (C) 2018  Daniel Espinosa <esodan@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *      Daniel Espinosa <esodan@gmail.com>
 */

#include "gcalc/gcalc.h"
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>
#include "gcalc.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  {
	GCALC_EQUATION_0_PROPERTY,
	GCALC_EQUATION_VARIABLES_PROPERTY,
	GCALC_EQUATION_NUM_PROPERTIES
};
static GParamSpec* gcalc_equation_properties[GCALC_EQUATION_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _GCalcEquationPrivate {
	GCalcExpressionHashMap* _variables;
};

static gint GCalcEquation_private_offset;
static gpointer gcalc_equation_parent_class = NULL;
static GCalcMathEquationIface * gcalc_equation_gcalc_math_equation_parent_iface = NULL;

VALA_EXTERN GCalcMathResult* gcalc_expression_solve (GCalcExpression* self);
static GCalcMathResult* gcalc_equation_real_solve (GCalcExpression* base);
static void gcalc_equation_finalize (GObject * obj);
static GType gcalc_equation_get_type_once (void);
static void _vala_gcalc_equation_get_property (GObject * object,
                                        guint property_id,
                                        GValue * value,
                                        GParamSpec * pspec);

static inline gpointer
gcalc_equation_get_instance_private (GCalcEquation* self)
{
	return G_STRUCT_MEMBER_P (self, GCalcEquation_private_offset);
}

static GCalcMathResult*
gcalc_equation_real_solve (GCalcExpression* base)
{
	GCalcEquation * self;
	GCalcMathResult* res = NULL;
	GCalcExpressionContainer* _tmp0_;
	GCalcExpressionContainer* _tmp1_;
	GCalcMathExpression* e = NULL;
	GCalcExpressionContainer* _tmp3_;
	GCalcExpressionContainer* _tmp4_;
	GObject* _tmp5_;
	GCalcMathExpression* _tmp6_;
	GCalcMathExpression* _tmp7_;
	GCalcMathResult* result;
	self = (GCalcEquation*) base;
	res = NULL;
	_tmp0_ = gcalc_math_expression_get_expressions ((GCalcMathExpression*) self);
	_tmp1_ = _tmp0_;
	if (g_list_model_get_n_items ((GListModel*) _tmp1_) == ((guint) 0)) {
		GCalcErrorResult* _tmp2_;
		_tmp2_ = gcalc_error_result_new ("No expressions found in equation");
		result = (GCalcMathResult*) _tmp2_;
		_g_object_unref0 (res);
		return result;
	}
	_tmp3_ = gcalc_math_expression_get_expressions ((GCalcMathExpression*) self);
	_tmp4_ = _tmp3_;
	_tmp5_ = g_list_model_get_item ((GListModel*) _tmp4_, (guint) 0);
	_tmp6_ = GCALC_IS_MATH_EXPRESSION (_tmp5_) ? ((GCalcMathExpression*) _tmp5_) : NULL;
	if (_tmp6_ == NULL) {
		_g_object_unref0 (_tmp5_);
	}
	e = _tmp6_;
	_tmp7_ = e;
	if (_tmp7_ == NULL) {
		GCalcErrorResult* _tmp8_;
		_tmp8_ = gcalc_error_result_new ("Invalid expression in equation");
		_g_object_unref0 (res);
		res = (GCalcMathResult*) _tmp8_;
	} else {
		GCalcMathExpression* _tmp9_;
		GCalcMathResult* _tmp10_;
		_tmp9_ = e;
		_tmp10_ = gcalc_math_expression_solve (_tmp9_);
		_g_object_unref0 (res);
		res = _tmp10_;
	}
	result = res;
	_g_object_unref0 (e);
	return result;
}

GCalcEquation*
gcalc_equation_construct (GType object_type)
{
	GCalcEquation * self = NULL;
	self = (GCalcEquation*) gcalc_expression_construct (object_type);
	return self;
}

GCalcEquation*
gcalc_equation_new (void)
{
	return gcalc_equation_construct (GCALC_TYPE_EQUATION);
}

static GCalcExpressionHashMap*
gcalc_equation_real_get_variables (GCalcMathEquation* base)
{
	GCalcExpressionHashMap* result;
	GCalcEquation* self;
	GCalcExpressionHashMap* _tmp0_;
	self = (GCalcEquation*) base;
	_tmp0_ = self->priv->_variables;
	result = _tmp0_;
	return result;
}

static void
gcalc_equation_class_init (GCalcEquationClass * klass,
                           gpointer klass_data)
{
	gcalc_equation_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GCalcEquation_private_offset);
	((GCalcExpressionClass *) klass)->solve = (GCalcMathResult* (*) (GCalcExpression*)) gcalc_equation_real_solve;
	G_OBJECT_CLASS (klass)->get_property = _vala_gcalc_equation_get_property;
	G_OBJECT_CLASS (klass)->finalize = gcalc_equation_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GCALC_EQUATION_VARIABLES_PROPERTY, gcalc_equation_properties[GCALC_EQUATION_VARIABLES_PROPERTY] = g_param_spec_object ("variables", "variables", "variables", GCALC_TYPE_EXPRESSION_HASH_MAP, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
gcalc_equation_gcalc_math_equation_interface_init (GCalcMathEquationIface * iface,
                                                   gpointer iface_data)
{
	gcalc_equation_gcalc_math_equation_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_variables = gcalc_equation_real_get_variables;
}

static void
gcalc_equation_instance_init (GCalcEquation * self,
                              gpointer klass)
{
	GCalcExpressionHashMap* _tmp0_;
	self->priv = gcalc_equation_get_instance_private (self);
	_tmp0_ = gcalc_expression_hash_map_new ();
	self->priv->_variables = _tmp0_;
}

static void
gcalc_equation_finalize (GObject * obj)
{
	GCalcEquation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GCALC_TYPE_EQUATION, GCalcEquation);
	_g_object_unref0 (self->priv->_variables);
	G_OBJECT_CLASS (gcalc_equation_parent_class)->finalize (obj);
}

/**
 * An implementation of {@link MathEquation}
 */
static GType
gcalc_equation_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GCalcEquationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gcalc_equation_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GCalcEquation), 0, (GInstanceInitFunc) gcalc_equation_instance_init, NULL };
	static const GInterfaceInfo gcalc_math_equation_info = { (GInterfaceInitFunc) gcalc_equation_gcalc_math_equation_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType gcalc_equation_type_id;
	gcalc_equation_type_id = g_type_register_static (GCALC_TYPE_EXPRESSION, "GCalcEquation", &g_define_type_info, 0);
	g_type_add_interface_static (gcalc_equation_type_id, GCALC_TYPE_MATH_EQUATION, &gcalc_math_equation_info);
	GCalcEquation_private_offset = g_type_add_instance_private (gcalc_equation_type_id, sizeof (GCalcEquationPrivate));
	return gcalc_equation_type_id;
}

GType
gcalc_equation_get_type (void)
{
	static volatile gsize gcalc_equation_type_id__once = 0;
	if (g_once_init_enter (&gcalc_equation_type_id__once)) {
		GType gcalc_equation_type_id;
		gcalc_equation_type_id = gcalc_equation_get_type_once ();
		g_once_init_leave (&gcalc_equation_type_id__once, gcalc_equation_type_id);
	}
	return gcalc_equation_type_id__once;
}

static void
_vala_gcalc_equation_get_property (GObject * object,
                                   guint property_id,
                                   GValue * value,
                                   GParamSpec * pspec)
{
	GCalcEquation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GCALC_TYPE_EQUATION, GCalcEquation);
	switch (property_id) {
		case GCALC_EQUATION_VARIABLES_PROPERTY:
		g_value_set_object (value, gcalc_math_equation_get_variables ((GCalcMathEquation*) self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

