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

/* -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-*/
/*-
 * Copyright (c) 2014-2015 Maya Developers (http://launchpad.net/maya)
 *
 * 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/>.
 *
 * Authored by: Corentin Noël <corentin@elementaryos.org>
 */
/* TODO: deprecate this in favor of the build-in Gtk 3.14 functions.*/

#include <glib.h>
#include "elementary-calendar.h"
#include <gdk/gdk.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <gio/gio.h>

#define MAYA_GESTURES_UTILS_INTERVAL ((guint) 500)
#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

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _MayaGesturesUtilsResetTimerData MayaGesturesUtilsResetTimerData;

struct _MayaGesturesUtilsResetTimerData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
};

VALA_EXTERN gboolean maya_gestures_utils_has_scrolled;
gboolean maya_gestures_utils_has_scrolled = FALSE;

static void maya_gestures_utils_reset_timer_data_free (gpointer _data);
static gboolean maya_gestures_utils_reset_timer_co (MayaGesturesUtilsResetTimerData* _data_);
static gboolean __lambda20_ (void);
static gboolean ___lambda20__gsource_func (gpointer self);

gboolean
maya_gestures_utils_on_scroll_event (GdkEventScroll* event)
{
	gdouble delta_x = 0.0;
	gdouble delta_y = 0.0;
	gdouble _tmp0_ = 0.0;
	gdouble _tmp1_ = 0.0;
	gdouble choice = 0.0;
	gboolean _tmp2_ = FALSE;
	gboolean result;
	g_return_val_if_fail (event != NULL, FALSE);
	gdk_event_get_scroll_deltas ((GdkEvent*) event, &_tmp0_, &_tmp1_);
	delta_x = _tmp0_;
	delta_y = _tmp1_;
	choice = delta_x;
	if (abs ((gint) delta_x) < abs ((gint) delta_y)) {
		choice = delta_y;
	}
	if (choice == ((gdouble) 1)) {
		_tmp2_ = TRUE;
	} else {
		_tmp2_ = choice == ((gdouble) -1);
	}
	if (_tmp2_) {
		CalendarEventStore* _tmp3_;
		CalendarEventStore* _tmp4_;
		_tmp3_ = calendar_event_store_get_default ();
		_tmp4_ = _tmp3_;
		calendar_event_store_change_month (_tmp4_, (gint) choice);
		_g_object_unref0 (_tmp4_);
		result = TRUE;
		return result;
	}
	if (maya_gestures_utils_has_scrolled == TRUE) {
		result = TRUE;
		return result;
	}
	if (choice > 0.3) {
		CalendarEventStore* _tmp5_;
		CalendarEventStore* _tmp6_;
		maya_gestures_utils_reset_timer (NULL, NULL);
		_tmp5_ = calendar_event_store_get_default ();
		_tmp6_ = _tmp5_;
		calendar_event_store_change_month (_tmp6_, 1);
		_g_object_unref0 (_tmp6_);
		result = TRUE;
		return result;
	}
	if (choice < (-0.3)) {
		CalendarEventStore* _tmp7_;
		CalendarEventStore* _tmp8_;
		maya_gestures_utils_reset_timer (NULL, NULL);
		_tmp7_ = calendar_event_store_get_default ();
		_tmp8_ = _tmp7_;
		calendar_event_store_change_month (_tmp8_, -1);
		_g_object_unref0 (_tmp8_);
		result = TRUE;
		return result;
	}
	result = FALSE;
	return result;
}

static void
maya_gestures_utils_reset_timer_data_free (gpointer _data)
{
	MayaGesturesUtilsResetTimerData* _data_;
	_data_ = _data;
	g_slice_free (MayaGesturesUtilsResetTimerData, _data_);
}

void
maya_gestures_utils_reset_timer (GAsyncReadyCallback _callback_,
                                 gpointer _user_data_)
{
	MayaGesturesUtilsResetTimerData* _data_;
	_data_ = g_slice_new0 (MayaGesturesUtilsResetTimerData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, maya_gestures_utils_reset_timer_data_free);
	maya_gestures_utils_reset_timer_co (_data_);
}

void
maya_gestures_utils_reset_timer_finish (GAsyncResult* _res_)
{
	MayaGesturesUtilsResetTimerData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static gboolean
__lambda20_ (void)
{
	gboolean result;
	maya_gestures_utils_has_scrolled = FALSE;
	result = FALSE;
	return result;
}

static gboolean
___lambda20__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda20_ ();
	return result;
}

static gboolean
maya_gestures_utils_reset_timer_co (MayaGesturesUtilsResetTimerData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	maya_gestures_utils_has_scrolled = TRUE;
	g_timeout_add_full (G_PRIORITY_DEFAULT, MAYA_GESTURES_UTILS_INTERVAL, ___lambda20__gsource_func, NULL, NULL);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

