/*
 * Copyright (C) 2012 - Juan Ferrer Toribio
 *
 * 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/>.
 */

#include "vn-calendar.h"
#include <db/db.h>

/**
 * SECTION:vn-calendar
 * @Short_description: a calendar widget
 * @Title: VnCalendar
 * @See_also: #VnField
 * @Image: vn-calendar.png
 *
 * A calendar widget to see or set the date.
 */
G_DEFINE_TYPE (VnCalendar, vn_calendar, VN_TYPE_FIELD);

/**
 * vn_calendar_new:
 *
 * Creates a new #VnCalendar
 *
 * Return value: a #VnCalendar
 **/
VnField * vn_calendar_new ()
{
	return g_object_new (VN_TYPE_CALENDAR, NULL);
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private

static void vn_calendar_cb_day_selected (GtkCalendar * calendar, VnCalendar * obj)
{
	guint year;
	guint month;
	guint day;
	GDate new_date;
	GValue value = G_VALUE_INIT;

	gtk_calendar_clear_marks (calendar);
	gtk_calendar_get_date (calendar, &year, &month, &day);
	month++;

	g_date_clear (&new_date, 1);
	g_date_set_dmy (&new_date, day, month, year);
	
	if (!g_date_valid (&obj->date) || g_date_compare (&new_date, &obj->date))
	{
		g_date_set_julian (&obj->date, g_date_get_julian (&new_date));
		gtk_calendar_mark_day (calendar, day);
		g_value_init (&value, G_TYPE_DATE);
		g_value_set_boxed (&value, &obj->date);
	}
	else
	{
		g_date_clear (&obj->date, 1);
		g_value_init (&value, GVN_TYPE_NULL);
	}

	VN_FIELD_GET_CLASS (obj)->value_changed (VN_FIELD (obj), &value);
	g_value_unset (&value);
}

void vn_calendar_cb_month_changed (GtkCalendar * calendar, VnCalendar * obj)
{
	guint year;
	guint month;
	GDate * date = &obj->date;

	gtk_calendar_clear_marks (calendar);
	gtk_calendar_get_date (calendar, &year, &month, NULL);

	if (g_date_valid (date)
	&& g_date_get_month (date) == month + 1
	&& g_date_get_year (date) == year)
	{
		guint day = g_date_get_day (date);
		gtk_calendar_mark_day (calendar, day);
		gtk_calendar_select_day (obj->calendar, day);
	}
	else
		gtk_calendar_select_day (obj->calendar, 0);
}

static void vn_calendar_set_value (VnCalendar * obj, const GValue * value)
{
	GDate * date = NULL;

	if (G_VALUE_TYPE (value) != G_TYPE_DATE)
	{
		GValue new_value = G_VALUE_INIT;
		g_value_init (&new_value, G_TYPE_DATE);
		g_value_transform (value, &new_value);
		date = g_value_get_boxed (&new_value);
	}
	else if (!gvn_value_is_null (value))
		date = g_value_get_boxed (value);

	if (date)
	{
		obj->date = *date;
		gtk_calendar_select_month (obj->calendar
			,g_date_get_month (date) - 1
			,g_date_get_year (date)
		);
	}
	else
	{
		g_date_clear (&obj->date, 1);
		gtk_calendar_select_day (obj->calendar, 0);
		gtk_calendar_clear_marks (obj->calendar);
	}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class

static void vn_calendar_init (VnCalendar * obj)
{
	g_date_clear (&obj->date, 1);

	obj->calendar = GTK_CALENDAR (gtk_calendar_new ());
	gtk_calendar_set_display_options (obj->calendar,
		GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES);
	g_object_connect (obj->calendar
		,"signal::day-selected-double-click", vn_calendar_cb_day_selected, obj
		,"signal::month-changed", vn_calendar_cb_month_changed, obj
		,NULL
	);
	gtk_container_add (GTK_CONTAINER (obj), GTK_WIDGET (obj->calendar));
	VN_FIELD (obj)->field = GTK_WIDGET (obj->calendar);
}

static void vn_calendar_finalize (VnCalendar * obj)
{
	G_OBJECT_CLASS (vn_calendar_parent_class)->finalize (G_OBJECT (obj));
}

static void vn_calendar_class_init (VnCalendarClass * klass)
{
	G_OBJECT_CLASS (klass)->finalize = (GObjectFinalizeFunc) vn_calendar_finalize;
	VN_FIELD_CLASS (klass)->set_value = (VnFieldSetValueFunc) vn_calendar_set_value;
}