/*  HomeBank -- Free, easy, personal accounting for everyone.
 *  Copyright (C) 1995-2026 Maxime DOYEN
 *
 *  This file is part of HomeBank.
 *
 *  HomeBank 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  HomeBank 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 "homebank.h"

#include "rep-list.h"


/****************************************************************************/
/* Debug macros															 */
/****************************************************************************/
#define MYDEBUG 0

#if MYDEBUG
#define DB(x) (x);
#else
#define DB(x);
#endif


/* our global datas */
extern struct HomeBank *GLOBALS;
extern struct Preferences *PREFS;




GString *lst_replist_to_string(ToStringMode mode, GtkTreeView *treeview, gchar *title)
{
GString *node;
GtkTreeModel *model;
GtkTreeIter	iter;
gboolean valid;
gchar sep;
guint type = GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(treeview), "type_data"));

	node = g_string_new(NULL);

	sep = (mode == HB_STRING_EXPORT) ? ';' : '\t';

	// header
	//g_string_append (node, _("Date") );
	g_string_append (node, (title == NULL) ? _("Time slice") : title );	
	g_string_append_c (node, sep );
	g_string_append (node, _("Expense") );	
	g_string_append_c (node, sep );
	g_string_append (node, _("Income") );	
	g_string_append_c (node, sep );
	title = (type != LIST_TYPE_BALANCE) ? _("Total") : _("Balance");
	g_string_append (node, title );
	g_string_append (node, "\n" );
	
	// lines
	model = gtk_tree_view_get_model(treeview);
	valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &iter);
	while (valid)
	{
	gchar *intvlname;
	gdouble values[4];

		gtk_tree_model_get (model, &iter,
			LST_REPORT_LABEL, &intvlname,
			LST_REPORT_EXPENSE, &values[0],
			LST_REPORT_INCOME,  &values[1],
			LST_REPORT_TOTAL,   &values[2],
			-1);

		g_string_append (node, intvlname );	

		for(guint i=0;i<3;i++)
		{
			g_string_append_c(node, sep);
			_format_decimal(node, mode, values[i]);
		}
		g_string_append_c(node, '\n');

		//leak
		g_free(intvlname);

		valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &iter);
	}

	//DB( g_print("text is:\n%s", node->str) );

	return node;
}


static void lst_replist_cell_data_function_date (GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
{
gchar *datestr;
gint flags;
gchar *color;
gint weight;

	gtk_tree_model_get(model, iter,
		LST_REPORT_LABEL, &datestr,
		LST_REPORT_FLAGS, &flags,
		-1);

	color = NULL;
	weight = PANGO_WEIGHT_NORMAL;

	if( flags & REPORT_FLAG_OVER )
	{
		if(PREFS->custom_colors == TRUE)
			color = PREFS->color_warn;

		weight = PANGO_WEIGHT_BOLD;
	}

	g_object_set(renderer,
		"weight", weight,
		"foreground",  color,
		"text", datestr,
		NULL);

	//leak
	g_free(datestr);
}


static void lst_replist_cell_cell_data_function_amount (GtkTreeViewColumn *col,
						   GtkCellRenderer   *renderer,
						   GtkTreeModel	  *model,
						   GtkTreeIter	   *iter,
						   gpointer		   user_data)
{
gdouble  value;
gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
gint colid = GPOINTER_TO_INT(user_data);
gint flags;
gchar *color;


	//get datas
	gtk_tree_model_get(model, iter,
		LST_REPORT_FLAGS, &flags,
		GPOINTER_TO_INT(user_data), &value,
		-1);

	//#2091004 we have exact 0.0, do we force display ?
	if( (hb_amount_cmp(value, 0.0) != 0) || (colid == LST_REPORT_TOTAL) )
	{
	guint32 kcur = GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(gtk_tree_view_column_get_tree_view(col)), "kcur_data"));

		hb_strfmon(buf, G_ASCII_DTOSTR_BUF_SIZE-1, value, kcur, GLOBALS->minor);

		color = get_normal_color_amount(value);
		if( flags & REPORT_FLAG_OVER )
		{
			if(PREFS->custom_colors == TRUE)
				color = PREFS->color_warn;

			//weight = PANGO_WEIGHT_BOLD;
		}

		g_object_set(renderer,
			"foreground",  color,
			"text", buf,
			NULL);
	}
	//fix: 400483
	else
	{
		g_object_set(renderer, "text", "", NULL);
	}

}

static GtkTreeViewColumn *lst_replist_column_amount_create(gchar *name, gint id)
{
GtkTreeViewColumn  *column;
GtkCellRenderer	*renderer;

	column = gtk_tree_view_column_new();
	gtk_tree_view_column_set_title(column, name);
	renderer = gtk_cell_renderer_text_new ();
	g_object_set(renderer, "xalign", 1.0, NULL);
	gtk_tree_view_column_pack_start(column, renderer, TRUE);
	gtk_tree_view_column_set_cell_data_func(column, renderer, lst_replist_cell_cell_data_function_amount, GINT_TO_POINTER(id), NULL);
	//#2004631 date and column title alignement
	gtk_tree_view_column_set_alignment (column, 1.0);
	//gtk_tree_view_column_set_sort_column_id (column, id);
	return column;
}


void lst_replist_set_cur(GtkTreeView *treeview, guint32 kcur)
{
	g_object_set_data(G_OBJECT(treeview), "kcur_data", GUINT_TO_POINTER(kcur));
}


/*
** create our statistic list
*/
GtkWidget *lst_replist_create(HbListType type)
{
GtkListStore *store;
GtkWidget *treeview;
GtkCellRenderer	*renderer;
GtkTreeViewColumn  *column;
gchar *title;

	/* create list store */
	store = gtk_list_store_new(
	  	NUM_LST_REPORT,
		G_TYPE_INT,		//pos
		G_TYPE_INT,		//key
		G_TYPE_STRING,	//label
		G_TYPE_DOUBLE,	//exp
		G_TYPE_DOUBLE,	//inc
		G_TYPE_DOUBLE,	//total
		G_TYPE_INT		//flags
		);

	//treeview
	treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
	g_object_unref(store);

	gtk_tree_view_set_grid_lines (GTK_TREE_VIEW (treeview), PREFS->grid_lines);

	/* column: Label */
	column = gtk_tree_view_column_new();
	gtk_tree_view_column_set_title(column, _("Date"));
	renderer = gtk_cell_renderer_text_new();
	//#2004631 date and column title alignement
	//g_object_set(renderer, "xalign", 1.0, NULL);
	gtk_tree_view_column_pack_start(column, renderer, TRUE);
	gtk_tree_view_column_set_cell_data_func(column, renderer, lst_replist_cell_data_function_date, NULL, NULL);
	//gtk_tree_view_column_add_attribute(column, renderer, "text", LST_REPORT_DATE);
	gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);

	/* column: Expense */
	column = lst_replist_column_amount_create(_("Expense"), LST_REPORT_EXPENSE);
	gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);

	/* column: Income */
	column = lst_replist_column_amount_create(_("Income"), LST_REPORT_INCOME);
	gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);

	/* column: Total/Balance */
	title = (type != LIST_TYPE_BALANCE) ? _("Total") : _("Balance");

	column = lst_replist_column_amount_create(title, LST_REPORT_TOTAL);
	gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);

  /* column last: empty */
	column = gtk_tree_view_column_new();
	gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);

	gtk_tree_view_set_enable_search(GTK_TREE_VIEW(treeview), FALSE);

	g_object_set_data(G_OBJECT(treeview), "type_data", GUINT_TO_POINTER(type));

	return(treeview);
}

