/*
 * 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-completion.h"
#include <db/db.h>
#include <vn/vn-grid-model.h>
#include <string.h>

#define set_icon(self,icon_name)	(gtk_entry_set_icon_from_icon_name (self->entry, GTK_ENTRY_ICON_SECONDARY, icon_name))

/**
 * SECTION:vn-completion
 * @Short_description: an auto-completable text box
 * @Title: VnCompletion
 * @See_also: #VnField
 * @Image: completion.png
 *
 * A text box widget that auto-completes as the user writes using the data
 * retrieved from a database.
 */

static void vn_completion_model_holder_init (DbModelHolderInterface * iface);

G_DEFINE_TYPE_WITH_CODE (VnCompletion, vn_completion, VN_TYPE_FIELD,
	G_IMPLEMENT_INTERFACE (DB_TYPE_MODEL_HOLDER,
		vn_completion_model_holder_init)
);

/**
 * vn_completion_new:
 * @model: a #DbModel with the contents for the new completion.
 * @field: the name of the field to search the values in.
 *
 * Creates a new #VnCompletion that will complete text with the data contained
 * in the field named @field of @model.
 *
 * Return value: a #VnCompletion.
 **/
VnField * vn_completion_new (DbModel * model, const gchar * field)
{
	return g_object_new (VN_TYPE_COMPLETION, "model", model, "field", field, NULL);
}

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

static void vn_completion_on_status_changed (DbModel * model,
	DbModelStatus status, VnCompletion * self)
{
	const gchar * icon_name;

	if (status == DB_MODEL_STATUS_READY)
	{
		GtkTreeModel * tree = GTK_TREE_MODEL (vn_grid_model_new (model));
		gtk_entry_completion_set_model (self->completion, tree);
		g_signal_emit_by_name (self->entry, "changed");
		g_object_unref (tree);
		icon_name = NULL;
	}
	else
	{
		switch (status)
		{
			case DB_MODEL_STATUS_LOADING:
				icon_name = "edit-find-symbolic";
				break;
			case DB_MODEL_STATUS_ERROR:
				icon_name = "edit-delete-symbolic";
				break;
			default:
				icon_name = NULL;
		}

		gtk_entry_completion_set_model (self->completion, NULL);
	}
	
	set_icon (self, icon_name);
}

static void vn_completion_on_changed (GtkEditable * entry, VnCompletion * self)
{
	const gchar * text = gtk_entry_get_text (GTK_ENTRY (entry));

	if (self->invalid)
	{
		self->invalid = FALSE;
		set_icon (self, NULL);
	}		
	
	if (self->model && strlen (text) == 1
	&& (!self->last_match || g_strcmp0 (text, self->last_match)))
	{
		gchar * pattern;
		GValue value = G_VALUE_INIT;

		set_icon (self, "edit-find-symbolic");
	
		g_free (self->last_match);
		self->last_match = g_strdup (text);
		pattern = g_strconcat (text, "%", NULL);
		
		g_value_init (&value, G_TYPE_STRING);
		g_value_set_string (&value, pattern);
		gvn_param_set_value (GVN_PARAM (self->value), &value);
		g_value_unset (&value);
	
		g_free (pattern);
	}
}

static void vn_completion_iter_changed (VnCompletion * self, DbIter * iter)
{
	const GValue * value = db_model_get_value (self->model, iter, 0, NULL);

	if (value)
		VN_FIELD_GET_CLASS (self)->value_changed (VN_FIELD (self), value);

	set_icon (self, "gtk-apply");
}

static void vn_completion_on_activate (GtkEntry * entry, VnCompletion * self)
{
	gboolean ok = FALSE;
	const gchar * text = gtk_entry_get_text (entry);

	if (text && g_strcmp0 (text, ""))
	{
		DbModel * model = self->model;

		if (db_model_get_column_type (model, self->column) == G_TYPE_STRING)
		{	
			DbIter iter;
			const GValue * v;

			if (db_model_get_iter_first (model, &iter))
			{
				do {
					v = db_model_get_value (model, &iter, self->column, NULL);

					if (!gvn_value_is_null (v)
					&& !g_ascii_strcasecmp (g_value_get_string (v), text))
					{
						vn_completion_iter_changed (self, &iter);
						ok = TRUE;
					}
				}
				while (!ok && db_model_iter_next (model, &iter));
			}
		}
	}
	
	if (!ok)
	{
		GValue value = G_VALUE_INIT;

		g_value_init (&value, GVN_TYPE_NULL);
		VN_FIELD_GET_CLASS (self)->value_changed (VN_FIELD (self), &value);
		g_value_unset (&value);
		
		if (g_strcmp0 (text, ""))
		{
			self->invalid = TRUE;
			set_icon (self, "edit-delete");
		}
		else	
			set_icon (self, NULL);
	}
}

static gboolean vn_completion_match_selected (GtkEntryCompletion * completion,
	GtkTreeModel * model, GtkTreeIter * tree_iter, VnCompletion * self)
{
	DbIter iter;
	
	vn_gtk_tree_iter_to_db_iter (tree_iter, &iter);
	vn_completion_iter_changed (self, &iter);
	
	return FALSE;
}

static void vn_completion_substitute (VnCompletion * self)
{
	SqlObject * field;
	SqlObject * like;
	SqlBatch * batch;
	SqlList * operands;

	if (!(self->model && self->field))
		return;

	like = sql_operation_new (SQL_OPERATION_TYPE_LIKE);

	operands = sql_list_new (SQL_TYPE_EXPR);
	g_object_set (like, "operands", operands, NULL);
	
	field = sql_field_new (self->field);
	sql_list_add (operands, field);

	self->value = sql_value_new ();
	sql_list_add (operands, self->value);
	
	batch = sql_batch_new ();
	sql_batch_add (batch, "field", field);
	sql_batch_add (batch, "filter", like);
	db_model_set_batch (self->model, batch);
}

static void vn_completion_set_field (VnCompletion * self, gchar * field)
{
	self->field = field;
	vn_completion_substitute (self);
}

//+++++++++++++++++++++++++++++++++++++++++++++++++ DbModelHolder implementation

static DbModel * vn_completion_get_model (VnCompletion * self)
{
	return self->model;
}

static void vn_completion_set_model (VnCompletion * self, DbModel * model)
{
	self->model = model;
	g_signal_connect (self->model, "status-changed",
		G_CALLBACK (vn_completion_on_status_changed), self);

	vn_completion_substitute (self);
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties

enum
{
	 PROP_MODEL = 1
	,PROP_FIELD
};

static void vn_completion_set_property (VnCompletion * self, guint property_id,
	const GValue * value, GParamSpec * pspec)
{
	switch (property_id)
	{
		case PROP_MODEL:
			vn_completion_set_model (self, g_value_dup_object (value));
			break;
		case PROP_FIELD:
			vn_completion_set_field (self, g_value_dup_string (value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
	}
}

static void vn_completion_get_property (VnCompletion * self, guint property_id,
	GValue * value, GParamSpec * pspec)
{	
	switch (property_id)
	{
		case PROP_MODEL:
			g_value_set_object (value, self->model);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
	}
}

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

static void vn_completion_init (VnCompletion * self)
{
	GtkEntryCompletion * completion;

	self->field = NULL;
	self->last_match = NULL;
	self->invalid = FALSE;
	self->column = 1;
	
	self->entry = GTK_ENTRY (gtk_entry_new ());
	gtk_entry_set_icon_sensitive (self->entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
	g_object_connect (self->entry,
		 "signal::changed", vn_completion_on_changed, self
		,"signal::activate", vn_completion_on_activate, self
		,NULL
	);
	gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->entry));

	completion = gtk_entry_completion_new ();
	gtk_entry_completion_set_minimum_key_length (completion, 1);
	gtk_entry_completion_set_text_column (completion, self->column);
	gtk_entry_completion_set_inline_selection (completion, FALSE);
//	gtk_entry_completion_set_inline_completion (completion, TRUE);
	g_signal_connect (completion, "match-selected",
		G_CALLBACK (vn_completion_match_selected), self);
	gtk_entry_set_completion (self->entry, completion);
	self->completion = completion;
	
	VN_FIELD_GET_CLASS (self)->set_widget (VN_FIELD (self),
		GTK_WIDGET (self->entry));
}

static void vn_completion_finalize (VnCompletion * self)
{
	g_free (self->field);
	g_free (self->last_match);
	g_object_unref (self->completion);
	g_object_unref (self->model);
	G_OBJECT_CLASS (vn_completion_parent_class)->finalize (G_OBJECT (self));
}

static void vn_completion_class_init (VnCompletionClass * klass)
{
	GObjectClass * k = G_OBJECT_CLASS (klass);
	k->finalize = (GObjectFinalizeFunc) vn_completion_finalize;
	k->set_property = (GObjectSetPropertyFunc) vn_completion_set_property;
	k->get_property = (GObjectGetPropertyFunc) vn_completion_get_property;
	
	g_object_class_override_property (k, PROP_MODEL, "model");

	g_object_class_install_property (k, PROP_FIELD,
		g_param_spec_string ("field"
			,_("Field")
			,_("The name of the field used for the search")
			,NULL
			,G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY
	));
}

static void vn_completion_model_holder_init (DbModelHolderInterface * iface)
{
	iface->get_model = (DbModelHolderGetModelFunc) vn_completion_get_model;
	iface->set_model = (DbModelHolderSetModelFunc) vn_completion_set_model;
}