/*
 * 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-column.h"
#include "vn-grid.h"

G_DEFINE_ABSTRACT_TYPE (VnColumn, vn_column, GTK_TYPE_TREE_VIEW_COLUMN);

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

static void vn_column_value_changed (VnColumn * obj, const gchar * path, const GValue * value)
{
	DbIter iter;

	vn_column_get_iter (obj, path, &iter);
	db_model_set_value (vn_column_get_model (obj),
		&iter, obj->column_index, value, NULL);
}

static void vn_column_data_func (GtkTreeViewColumn * col, GtkCellRenderer * cell, 
	GtkTreeModel * model, GtkTreeIter * iter, VnColumn * obj)
{
	gchar * background;
	DbIter dbiter;
	DbModel * dbmodel;

	GValue value = {0};
	gtk_tree_model_get_value (model, iter, obj->column_index, &value);
	obj->set_value (obj, model, iter, cell, &value);
	g_value_unset (&value);

	vn_gtk_tree_iter_to_db_iter (iter, &dbiter);
	g_object_get (model, "model", &dbmodel, NULL);
	DbModelRowOp ops = db_model_get_row_operations (dbmodel, &dbiter);
	g_object_unref (dbmodel);

	if (ops & DB_MODEL_ROW_OP_DELETE)
		background = "#fcc";
	else if (ops & DB_MODEL_ROW_OP_INSERT || ops & DB_MODEL_ROW_OP_UPDATE)
		background = "#cfc";
	else
		background = NULL;

	g_object_set (cell, "cell-background", background, NULL);
}

static void vn_column_set_renderer (VnColumn * obj, GtkCellRenderer * cell)
{
	GtkTreeViewColumn * column;

	obj->cell = cell;
	obj->set_value = VN_COLUMN_GET_CLASS (obj)->set_value;
	VN_COLUMN_GET_CLASS (obj)->set_editable (obj, obj->editable);
	
	column = GTK_TREE_VIEW_COLUMN (obj);
	gtk_tree_view_column_pack_start (column, cell, TRUE);
	gtk_tree_view_column_set_cell_data_func (column, cell,
		(GtkTreeCellDataFunc) vn_column_data_func, obj, NULL);
}

static void vn_column_update_column_index (VnColumn * obj)
{
	if (obj->column_name)
	{
		DbModel * model = vn_column_get_model (obj);
		
		if (model)
		{
			obj->column_index = db_model_get_column_index (model, obj->column_name);

			if (obj->column_index >= 0)
				gtk_tree_view_column_set_sort_column_id (
					GTK_TREE_VIEW_COLUMN (obj), obj->column_index);
		}
	}		
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public

/**
 * vn_column_get_column_name:
 * @obj: the #VnColumn
 *
 * Gets the column name.
 *
 * Return value: the column name
 **/
const gchar * vn_column_get_column_name (VnColumn * obj)
{
	g_return_val_if_fail (VN_IS_COLUMN (obj), NULL);

	return obj->column_name;
}

/**
 * vn_column_set_column_name:
 * @obj: the #VnColumn
 * @name: the column name
 *
 * Sets the column name.
 **/
void vn_column_set_column_name (VnColumn * obj, const gchar * name)
{
	g_return_if_fail (VN_IS_COLUMN (obj));

	g_free (obj->column_name);
	obj->column_name = g_strdup (name);
	vn_column_update_column_index (obj);
}

/**
 * vn_column_get_column_index:
 * @obj: the #VnColumn
 *
 * Gets the column index.
 *
 * Return value: the column index
 **/
gint vn_column_get_column_index (VnColumn * obj)
{
	g_return_val_if_fail (VN_IS_COLUMN (obj), -1);

	return obj->column_index;
}

/**
 * vn_column_set_column_index:
 * @obj: the #VnColumn
 * @index: the column index
 *
 * Sets the column index.
 **/
void vn_column_set_column_index (VnColumn * obj, gint index)
{
	g_return_if_fail (VN_IS_COLUMN (obj));
	g_return_if_fail (index >= -1);

	obj->column_index = index;
	vn_column_set_column_name (obj, NULL);
	
	if (index >= 0)
		gtk_tree_view_column_set_sort_column_id (
			GTK_TREE_VIEW_COLUMN (obj), index);
}

/**
 * vn_column_get_editable:
 * @obj: the #VnColumn
 *
 * Gets if the column can be edited by the user.
 *
 * Return value: %TRUE if its editable, %FALSE otherwhise
 **/
gboolean vn_column_get_editable (VnColumn * obj)
{
	g_return_val_if_fail (VN_IS_COLUMN (obj), FALSE);

	return obj->editable;
}

/**
 * vn_column_set_editable:
 * @obj: the #VnColumn
 * @editable: a %gboolean indicating whether the column value is editable
 *
 * Sets if the column could be edited by the user.
 **/
void vn_column_set_editable (VnColumn * obj, gboolean editable)
{
	g_return_if_fail (VN_IS_COLUMN (obj));

	obj->editable = editable;
	VN_COLUMN_GET_CLASS (obj)->set_editable (obj, editable);
}

/**
 * vn_column_get_model:
 * @obj: the #VnColumn
 *
 * Gets the model used by the main #VnGrid.
 *
 * Return value: (transfer none): the #DbModel if has it, %NULL otherwise.
 **/
DbModel * vn_column_get_model (VnColumn * obj)
{
	g_return_val_if_fail (VN_IS_COLUMN (obj), NULL);

	GtkWidget * grid = gtk_tree_view_column_get_tree_view (
		GTK_TREE_VIEW_COLUMN (obj));
		
	if (grid)
		return vn_grid_get_model (VN_GRID (grid));
		
	return NULL;
}

/**
 * vn_column_get_iter:
 * @obj: the #VnColumn
 * @path: the path string
 * @iter: (out): the #DbIter to be set
 *
 * Gets a #DbIter from a #GtkTreePath string.
 *
 * Return value: %TRUE if the iter could be obtained, %FALSE otherwise.
 **/
gboolean vn_column_get_iter (VnColumn * obj, const gchar * path, DbIter * iter)
{
	DbModel * model;
	GtkTreePath * tree_path;
	
	g_return_val_if_fail (VN_IS_COLUMN (obj), FALSE);
	g_return_val_if_fail (iter, FALSE);

	tree_path = gtk_tree_path_new_from_string (path);
	model = vn_column_get_model (obj);
	
	if (!tree_path)
		g_warning ("VnColumn: Invalid path.");
	else if (gtk_tree_path_get_depth (tree_path) < 1)
		g_warning ("VnColumn: Path has no indices.");
	else if (!model)
		g_warning ("VnColumn: Tree view has no model assigned.");
	else
	{
		db_model_get_iter (model, iter, gtk_tree_path_get_indices (tree_path)[0]);
		return TRUE;
	}

	gtk_tree_path_free (tree_path);
	return FALSE;
}

/**
 * vn_column_model_changed:
 * @obj: the #VnColumn
 *
 * This function is used primarily by the #VnGrid. Warns the column that the
 * model has changed.
 **/
void vn_column_model_changed (VnColumn * obj)
{
	VnColumnClass * klass;

	g_return_if_fail (VN_IS_COLUMN (obj));
	
	klass = VN_COLUMN_GET_CLASS (obj);
	vn_column_update_column_index (obj);

	if (klass->model_changed)
		klass->model_changed (obj);
}

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

enum
{
	 PROP_COLUMN_INDEX = 1
	,PROP_COLUMN_NAME
	,PROP_EDITABLE
};

static void vn_column_set_property (VnColumn * obj, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_COLUMN_INDEX:
			vn_column_set_column_index (obj, g_value_get_int (value));
			break;
		case PROP_COLUMN_NAME:
			vn_column_set_column_name (obj, g_value_get_string (value));
			break;
		case PROP_EDITABLE:
			vn_column_set_editable (obj, g_value_get_boolean (value));
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

static void vn_column_get_property (VnColumn * obj, guint id,
	GValue * value, GParamSpec * pspec)
{	
	switch (id)
	{
		case PROP_COLUMN_INDEX:
			g_value_set_int (value, obj->column_index);
			break;
		case PROP_COLUMN_NAME:
			g_value_set_string (value, obj->column_name);
			break;
		case PROP_EDITABLE:
			g_value_set_boolean (value, obj->editable);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
	}
}

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

static void vn_column_init (VnColumn * obj)
{
	obj->column_index = -1;
	obj->column_name = NULL;
}

static void vn_column_finalize (VnColumn * obj)
{
	G_OBJECT_CLASS (vn_column_parent_class)->finalize (G_OBJECT (obj));
}

static void vn_column_class_init (VnColumnClass * klass)
{
	GObjectClass * k = G_OBJECT_CLASS (klass);
	k->finalize = (GObjectFinalizeFunc) vn_column_finalize;
	k->set_property = (GObjectSetPropertyFunc) vn_column_set_property;
	k->get_property = (GObjectGetPropertyFunc) vn_column_get_property;
	klass->value_changed = vn_column_value_changed;
	klass->set_renderer = vn_column_set_renderer;
	klass->model_changed = NULL;

	g_object_class_install_property (k, PROP_COLUMN_INDEX,
		g_param_spec_int ("column-index"
			,_("Column index")
			,_("The column index in the model")
			,-1, 255, -1
			,G_PARAM_READWRITE
	));
	g_object_class_install_property (k, PROP_COLUMN_NAME,
		g_param_spec_string ("column-name"
			,_("Column name")
			,_("The referenced column name")
			,NULL
			,G_PARAM_READWRITE
	));
	g_object_class_install_property (k, PROP_EDITABLE,
		g_param_spec_boolean ("editable"
			,_("Editable")
			,_("Whether the column values are editable")
			,FALSE
			,G_PARAM_CONSTRUCT | G_PARAM_READWRITE
	));
}