/* gcalc-math-group.c generated by valac 0.56.0, the Vala compiler
 * generated from gcalc-math-group.vala, do not modify */

/* gcalc-math-group.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/gi18n-lib.h>
#include <glib-object.h>
#include "gcalc.h"

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

static GCalcMathExpression* gcalc_math_group_real_evaluate (GCalcMathGroup* self,
                                                     GError** error);
static GType gcalc_math_group_get_type_once (void);

static GType
gcalc_math_group_level_get_type_once (void)
{
	static const GEnumValue values[] = {{GCALC_MATH_GROUP_LEVEL_ONE, "GCALC_MATH_GROUP_LEVEL_ONE", "one"}, {GCALC_MATH_GROUP_LEVEL_TWO, "GCALC_MATH_GROUP_LEVEL_TWO", "two"}, {GCALC_MATH_GROUP_LEVEL_THREE, "GCALC_MATH_GROUP_LEVEL_THREE", "three"}, {0, NULL, NULL}};
	GType gcalc_math_group_level_type_id;
	gcalc_math_group_level_type_id = g_enum_register_static ("GCalcMathGroupLevel", values);
	return gcalc_math_group_level_type_id;
}

GType
gcalc_math_group_level_get_type (void)
{
	static volatile gsize gcalc_math_group_level_type_id__once = 0;
	if (g_once_init_enter (&gcalc_math_group_level_type_id__once)) {
		GType gcalc_math_group_level_type_id;
		gcalc_math_group_level_type_id = gcalc_math_group_level_get_type_once ();
		g_once_init_leave (&gcalc_math_group_level_type_id__once, gcalc_math_group_level_type_id);
	}
	return gcalc_math_group_level_type_id__once;
}

static GCalcMathExpression*
gcalc_math_group_real_evaluate (GCalcMathGroup* self,
                                GError** error)
{
	GCalcExpressionContainer* _tmp0_;
	GCalcExpressionContainer* _tmp1_;
	GCalcMathPolynomial* e = NULL;
	GCalcExpressionContainer* _tmp3_;
	GCalcExpressionContainer* _tmp4_;
	GObject* _tmp5_;
	GCalcMathPolynomial* _tmp6_;
	GCalcMathPolynomial* _tmp7_;
	GCalcMathExpression* _tmp9_ = NULL;
	GCalcMathPolynomial* _tmp10_;
	GCalcMathExpression* _tmp11_;
	GCalcMathExpression* _tmp12_;
	GError* _inner_error0_ = NULL;
	GCalcMathExpression* result;
	_tmp0_ = gcalc_math_expression_get_expressions ((GCalcMathExpression*) self);
	_tmp1_ = _tmp0_;
	if (g_list_model_get_n_items ((GListModel*) _tmp1_) == ((guint) 0)) {
		GError* _tmp2_;
		_tmp2_ = g_error_new_literal (GCALC_GROUP_ERROR, GCALC_GROUP_ERROR_INVALID_POLYNOMIAL, _ ("No internal polynomial in group"));
		_inner_error0_ = _tmp2_;
		g_propagate_error (error, _inner_error0_);
		return NULL;
	}
	_tmp3_ = gcalc_math_expression_get_expressions ((GCalcMathExpression*) self);
	_tmp4_ = _tmp3_;
	_tmp5_ = g_list_model_get_item ((GListModel*) _tmp4_, (guint) 0);
	_tmp6_ = GCALC_IS_MATH_POLYNOMIAL (_tmp5_) ? ((GCalcMathPolynomial*) _tmp5_) : NULL;
	if (_tmp6_ == NULL) {
		_g_object_unref0 (_tmp5_);
	}
	e = _tmp6_;
	_tmp7_ = e;
	if (_tmp7_ == NULL) {
		GError* _tmp8_;
		_tmp8_ = g_error_new_literal (GCALC_GROUP_ERROR, GCALC_GROUP_ERROR_INVALID_POLYNOMIAL, _ ("Invalid internal polynomial in group"));
		_inner_error0_ = _tmp8_;
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (e);
		return NULL;
	}
	_tmp10_ = e;
	_tmp11_ = gcalc_math_polynomial_evaluate (_tmp10_, &_inner_error0_);
	_tmp9_ = _tmp11_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (e);
		return NULL;
	}
	_tmp12_ = _tmp9_;
	_tmp9_ = NULL;
	result = _tmp12_;
	_g_object_unref0 (_tmp9_);
	_g_object_unref0 (e);
	return result;
}

GCalcMathExpression*
gcalc_math_group_evaluate (GCalcMathGroup* self,
                           GError** error)
{
	GCalcMathGroupIface* _iface_;
	g_return_val_if_fail (self != NULL, NULL);
	_iface_ = GCALC_MATH_GROUP_GET_INTERFACE (self);
	if (_iface_->evaluate) {
		return _iface_->evaluate (self, error);
	}
	return NULL;
}

GCalcMathGroupLevel
gcalc_math_group_get_level (GCalcMathGroup* self)
{
	GCalcMathGroupIface* _iface_;
	g_return_val_if_fail (self != NULL, 0);
	_iface_ = GCALC_MATH_GROUP_GET_INTERFACE (self);
	if (_iface_->get_level) {
		return _iface_->get_level (self);
	}
	return 0;
}

void
gcalc_math_group_set_level (GCalcMathGroup* self,
                            GCalcMathGroupLevel value)
{
	GCalcMathGroupIface* _iface_;
	g_return_if_fail (self != NULL);
	_iface_ = GCALC_MATH_GROUP_GET_INTERFACE (self);
	if (_iface_->set_level) {
		_iface_->set_level (self, value);
	}
}

gboolean
gcalc_math_group_get_closed (GCalcMathGroup* self)
{
	GCalcMathGroupIface* _iface_;
	g_return_val_if_fail (self != NULL, FALSE);
	_iface_ = GCALC_MATH_GROUP_GET_INTERFACE (self);
	if (_iface_->get_closed) {
		return _iface_->get_closed (self);
	}
	return FALSE;
}

void
gcalc_math_group_set_closed (GCalcMathGroup* self,
                             gboolean value)
{
	GCalcMathGroupIface* _iface_;
	g_return_if_fail (self != NULL);
	_iface_ = GCALC_MATH_GROUP_GET_INTERFACE (self);
	if (_iface_->set_closed) {
		_iface_->set_closed (self, value);
	}
}

static void
gcalc_math_group_default_init (GCalcMathGroupIface * iface,
                               gpointer iface_data)
{
	g_object_interface_install_property (iface, g_param_spec_enum ("level", "level", "level", GCALC_MATH_GROUP_TYPE_LEVEL, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_interface_install_property (iface, g_param_spec_boolean ("closed", "closed", "closed", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	iface->evaluate = gcalc_math_group_real_evaluate;
}

/**
 * A grouping math expression like parentesis
 */
static GType
gcalc_math_group_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GCalcMathGroupIface), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gcalc_math_group_default_init, (GClassFinalizeFunc) NULL, NULL, 0, 0, (GInstanceInitFunc) NULL, NULL };
	GType gcalc_math_group_type_id;
	gcalc_math_group_type_id = g_type_register_static (G_TYPE_INTERFACE, "GCalcMathGroup", &g_define_type_info, 0);
	g_type_interface_add_prerequisite (gcalc_math_group_type_id, G_TYPE_OBJECT);
	g_type_interface_add_prerequisite (gcalc_math_group_type_id, GCALC_TYPE_MATH_EXPRESSION);
	return gcalc_math_group_type_id;
}

GType
gcalc_math_group_get_type (void)
{
	static volatile gsize gcalc_math_group_type_id__once = 0;
	if (g_once_init_enter (&gcalc_math_group_type_id__once)) {
		GType gcalc_math_group_type_id;
		gcalc_math_group_type_id = gcalc_math_group_get_type_once ();
		g_once_init_leave (&gcalc_math_group_type_id__once, gcalc_math_group_type_id);
	}
	return gcalc_math_group_type_id__once;
}

GQuark
gcalc_group_error_quark (void)
{
	return g_quark_from_static_string ("gcalc-group-error-quark");
}

static GType
gcalc_group_error_get_type_once (void)
{
	static const GEnumValue values[] = {{GCALC_GROUP_ERROR_INVALID_POLYNOMIAL, "GCALC_GROUP_ERROR_INVALID_POLYNOMIAL", "invalid-polynomial"}, {GCALC_GROUP_ERROR_INVALID_INTERNAL_TERM, "GCALC_GROUP_ERROR_INVALID_INTERNAL_TERM", "invalid-internal-term"}, {0, NULL, NULL}};
	GType gcalc_group_error_type_id;
	gcalc_group_error_type_id = g_enum_register_static ("GCalcGroupError", values);
	return gcalc_group_error_type_id;
}

GType
gcalc_group_error_get_type (void)
{
	static volatile gsize gcalc_group_error_type_id__once = 0;
	if (g_once_init_enter (&gcalc_group_error_type_id__once)) {
		GType gcalc_group_error_type_id;
		gcalc_group_error_type_id = gcalc_group_error_get_type_once ();
		g_once_init_leave (&gcalc_group_error_type_id__once, gcalc_group_error_type_id);
	}
	return gcalc_group_error_type_id__once;
}

