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

/* gcalc-gassign.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 <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>
#include "gcalc.h"

enum  {
	GCALC_ASSIGN_0_PROPERTY,
	GCALC_ASSIGN_NUM_PROPERTIES
};
static GParamSpec* gcalc_assign_properties[GCALC_ASSIGN_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

static gpointer gcalc_assign_parent_class = NULL;
static GCalcMathOperatorIface * gcalc_assign_gcalc_math_operator_parent_iface = NULL;
static GCalcMathBinaryOperatorIface * gcalc_assign_gcalc_math_binary_operator_parent_iface = NULL;
static GCalcMathAssignIface * gcalc_assign_gcalc_math_assign_parent_iface = NULL;

VALA_EXTERN gchar* gcalc_expression_to_string (GCalcExpression* self);
VALA_EXTERN GCalcMathResult* gcalc_expression_solve (GCalcExpression* self);
static gchar* gcalc_assign_real_to_string (GCalcExpression* base);
static GCalcMathResult* gcalc_assign_real_solve (GCalcExpression* base);
static GType gcalc_assign_get_type_once (void);

static gchar*
gcalc_assign_real_to_string (GCalcExpression* base)
{
	GCalcAssign * self;
	GCalcExpressionContainer* _tmp0_;
	GCalcExpressionContainer* _tmp1_;
	GCalcMathVariable* v = NULL;
	GCalcExpressionContainer* _tmp3_;
	GCalcExpressionContainer* _tmp4_;
	GObject* _tmp5_;
	GCalcMathVariable* _tmp6_;
	GCalcMathVariable* _tmp7_;
	GCalcMathExpression* e = NULL;
	GCalcExpressionContainer* _tmp9_;
	GCalcExpressionContainer* _tmp10_;
	GObject* _tmp11_;
	GCalcMathExpression* _tmp12_;
	GCalcMathExpression* _tmp13_;
	GCalcMathVariable* _tmp15_;
	gchar* _tmp16_;
	gchar* _tmp17_;
	gchar* _tmp18_;
	gchar* _tmp19_;
	GCalcMathExpression* _tmp20_;
	gchar* _tmp21_;
	gchar* _tmp22_;
	gchar* _tmp23_;
	gchar* _tmp24_;
	gchar* result;
	self = (GCalcAssign*) base;
	_tmp0_ = gcalc_math_expression_get_expressions ((GCalcMathExpression*) self);
	_tmp1_ = _tmp0_;
	if (g_list_model_get_n_items ((GListModel*) _tmp1_) != ((guint) 2)) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup ("Invalid Assigment structure");
		result = _tmp2_;
		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_VARIABLE (_tmp5_) ? ((GCalcMathVariable*) _tmp5_) : NULL;
	if (_tmp6_ == NULL) {
		_g_object_unref0 (_tmp5_);
	}
	v = _tmp6_;
	_tmp7_ = v;
	if (_tmp7_ == NULL) {
		gchar* _tmp8_;
		_tmp8_ = g_strdup ("Invalid Assigment structure. No variable is set");
		result = _tmp8_;
		_g_object_unref0 (v);
		return result;
	}
	_tmp9_ = gcalc_math_expression_get_expressions ((GCalcMathExpression*) self);
	_tmp10_ = _tmp9_;
	_tmp11_ = g_list_model_get_item ((GListModel*) _tmp10_, (guint) 1);
	_tmp12_ = GCALC_IS_MATH_EXPRESSION (_tmp11_) ? ((GCalcMathExpression*) _tmp11_) : NULL;
	if (_tmp12_ == NULL) {
		_g_object_unref0 (_tmp11_);
	}
	e = _tmp12_;
	_tmp13_ = e;
	if (_tmp13_ == NULL) {
		gchar* _tmp14_;
		_tmp14_ = g_strdup ("Invalid Assigment structure. No variable's definition is set");
		result = _tmp14_;
		_g_object_unref0 (e);
		_g_object_unref0 (v);
		return result;
	}
	_tmp15_ = v;
	_tmp16_ = gcalc_math_expression_to_string ((GCalcMathExpression*) _tmp15_);
	_tmp17_ = _tmp16_;
	_tmp18_ = g_strconcat (_tmp17_, "=", NULL);
	_tmp19_ = _tmp18_;
	_tmp20_ = e;
	_tmp21_ = gcalc_math_expression_to_string (_tmp20_);
	_tmp22_ = _tmp21_;
	_tmp23_ = g_strconcat (_tmp19_, _tmp22_, NULL);
	_tmp24_ = _tmp23_;
	_g_free0 (_tmp22_);
	_g_free0 (_tmp19_);
	_g_free0 (_tmp17_);
	result = _tmp24_;
	_g_object_unref0 (e);
	_g_object_unref0 (v);
	return result;
}

static GCalcMathResult*
gcalc_assign_real_solve (GCalcExpression* base)
{
	GCalcAssign * self;
	GCalcMathResult* res = NULL;
	GError* _inner_error0_ = NULL;
	GCalcMathResult* result;
	self = (GCalcAssign*) base;
	res = NULL;
	{
		GCalcMathExpression* _tmp0_ = NULL;
		GCalcMathExpression* _tmp1_;
		GCalcResult* _tmp2_;
		_tmp1_ = gcalc_math_assign_evaluate ((GCalcMathAssign*) self, &_inner_error0_);
		_tmp0_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = gcalc_result_new (_tmp0_);
		_g_object_unref0 (res);
		res = (GCalcMathResult*) _tmp2_;
		_g_object_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		gchar* _tmp5_;
		gchar* _tmp6_;
		GCalcErrorResult* _tmp7_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp3_ = e;
		_tmp4_ = _tmp3_->message;
		_tmp5_ = g_strdup_printf ("Invalid expression in Assignment: %s", _tmp4_);
		_tmp6_ = _tmp5_;
		_tmp7_ = gcalc_error_result_new (_tmp6_);
		_g_object_unref0 (res);
		res = (GCalcMathResult*) _tmp7_;
		_g_free0 (_tmp6_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (res);
		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;
	}
	result = res;
	return result;
}

GCalcAssign*
gcalc_assign_construct (GType object_type)
{
	GCalcAssign * self = NULL;
	self = (GCalcAssign*) gcalc_expression_construct (object_type);
	return self;
}

GCalcAssign*
gcalc_assign_new (void)
{
	return gcalc_assign_construct (GCALC_TYPE_ASSIGN);
}

static void
gcalc_assign_class_init (GCalcAssignClass * klass,
                         gpointer klass_data)
{
	gcalc_assign_parent_class = g_type_class_peek_parent (klass);
	((GCalcExpressionClass *) klass)->to_string = (gchar* (*) (GCalcExpression*)) gcalc_assign_real_to_string;
	((GCalcExpressionClass *) klass)->solve = (GCalcMathResult* (*) (GCalcExpression*)) gcalc_assign_real_solve;
}

static void
gcalc_assign_gcalc_math_operator_interface_init (GCalcMathOperatorIface * iface,
                                                 gpointer iface_data)
{
	gcalc_assign_gcalc_math_operator_parent_iface = g_type_interface_peek_parent (iface);
}

static void
gcalc_assign_gcalc_math_binary_operator_interface_init (GCalcMathBinaryOperatorIface * iface,
                                                        gpointer iface_data)
{
	gcalc_assign_gcalc_math_binary_operator_parent_iface = g_type_interface_peek_parent (iface);
}

static void
gcalc_assign_gcalc_math_assign_interface_init (GCalcMathAssignIface * iface,
                                               gpointer iface_data)
{
	gcalc_assign_gcalc_math_assign_parent_iface = g_type_interface_peek_parent (iface);
}

static void
gcalc_assign_instance_init (GCalcAssign * self,
                            gpointer klass)
{
}

/**
 * An implementation of {@link MathAssign}
 */
static GType
gcalc_assign_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GCalcAssignClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gcalc_assign_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GCalcAssign), 0, (GInstanceInitFunc) gcalc_assign_instance_init, NULL };
	static const GInterfaceInfo gcalc_math_operator_info = { (GInterfaceInitFunc) gcalc_assign_gcalc_math_operator_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	static const GInterfaceInfo gcalc_math_binary_operator_info = { (GInterfaceInitFunc) gcalc_assign_gcalc_math_binary_operator_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	static const GInterfaceInfo gcalc_math_assign_info = { (GInterfaceInitFunc) gcalc_assign_gcalc_math_assign_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType gcalc_assign_type_id;
	gcalc_assign_type_id = g_type_register_static (GCALC_TYPE_EXPRESSION, "GCalcAssign", &g_define_type_info, 0);
	g_type_add_interface_static (gcalc_assign_type_id, GCALC_TYPE_MATH_OPERATOR, &gcalc_math_operator_info);
	g_type_add_interface_static (gcalc_assign_type_id, GCALC_TYPE_MATH_BINARY_OPERATOR, &gcalc_math_binary_operator_info);
	g_type_add_interface_static (gcalc_assign_type_id, GCALC_TYPE_MATH_ASSIGN, &gcalc_math_assign_info);
	return gcalc_assign_type_id;
}

GType
gcalc_assign_get_type (void)
{
	static volatile gsize gcalc_assign_type_id__once = 0;
	if (g_once_init_enter (&gcalc_assign_type_id__once)) {
		GType gcalc_assign_type_id;
		gcalc_assign_type_id = gcalc_assign_get_type_once ();
		g_once_init_leave (&gcalc_assign_type_id__once, gcalc_assign_type_id);
	}
	return gcalc_assign_type_id__once;
}

