/***************************************************************************
 *            gtk_wnd_mapinfo.c
 *
 *  Thu Jul 19 19:00:10 2007
 *  Copyright  2007  Steffen Pohle
 *  steffen@gulpe.de
 ****************************************************************************/

/*
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <gtk/gtk.h>
#include "map.h"
#include "gtk_port.h"

enum {
	HLIST_C_NR,
	HLIST_C_FLAGS,
	HLIST_C_LON,
	HLIST_C_LAT,
	HLIST_C_WAYS,
	HLIST_C_AREAS,
	HLIST_C_PLACES,
	HLIST_C_POIS,
	HLIST_C_MEMORY,
	
	HLIST_C_MAX
};


enum {
	WLIST_C_ID,
	WLIST_C_SUBID,
	WLIST_C_TYPE,
	WLIST_C_NAME,
	
	WLIST_C_MAX
};


GtkWidget *mapinfo_wnd = NULL;
GtkWidget *hash_list;
GtkWidget *wa_list;
GtkWidget *wa_info;


void hash_list_refresh ();
struct map_hashpos hash_list_getselected();
void wa_list_refresh (struct map_hashpos hpos);
int wa_list_getselected (unsigned long long int *id, short int *subid);

static gboolean mapinfo_wnd_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) {
	mapinfo_wnd = NULL;
    return FALSE; /* we can quit */
};


void mapinfo_refresh_click () {
	hash_list_refresh ();
};


gboolean hash_list_click (GtkWidget *widget, GdkEventButton *event, gpointer *data) {
	struct map_hashpos hpos = hash_list_getselected();
	
	wa_list_refresh (hpos);

	//map_ls_get_filename (fname, LEN_FILENAME, hpos.ilon, hpos.ilat);
	//if ((lsstat = map_ls_statget (fname)) == NULL) return FALSE;
	//d_printf ("%p : loaded.", lsstat);

	return TRUE;
};

#define MAXTEXT 64000
gboolean wa_list_click (GtkWidget *widget, GdkEventButton *event, gpointer *data) {
	unsigned long long int id;
	short int subid;
	GtkTextBuffer *tbuf;
	char text1[MAXTEXT];
	char text2[MAXTEXT];
	char *texts = text1, *textd = text2, *textt;
	struct map_hash *mh = NULL;
	struct map_way *mw = NULL;
	int i;
	struct map_hashpos hpos = hash_list_getselected();

	mh = map_hash_geti (hpos.ilon, hpos.ilat, MHLOAD_RELOAD);

	if (mh && wa_list_getselected (&id, &subid)) {
		for (mw = mh->ways; mw && (mw->subid != subid || mw->id != id); mw = mw->next);

		snprintf (textd, MAXTEXT, "Hash lon: %d   lat: %d\nWay ID: %lld : %d\n",
		          mh->pos.ilon, mh->pos.ilat, id, subid);
		textt = textd; textd = texts; texts = textt;
		snprintf (textd, MAXTEXT, "%s  Name:%s\n", texts, mw->name);
		textt = textd; textd = texts; texts = textt;
		snprintf (textd, MAXTEXT, "%s  Ref:%s\n", texts, mw->ref);
		textt = textd; textd = texts; texts = textt;
		snprintf (textd, MAXTEXT, "%s  Nodes:%d\n", texts, mw->n_cnt);
		textt = textd; textd = texts; texts = textt;
		for (i = 0; i < mw->n_cnt; i++) {
			snprintf (textd, MAXTEXT, "%s     %d  pnr:%d d.lon:%3.6f d.lat:%3.6f igeo:%d,%d d_wayid:%lld:%d d_pnr:%d\n", 
			          texts, i, mw->n[i].pnr, mw->n[i].d.lon, mw->n[i].d.lat, 
			          map_geo2igeo (mw->n[i].d.lon), map_geo2igeo (mw->n[i].d.lat),  
			          mw->n[i].d_id, mw->n[i].d_subid, mw->n[i].d_pnr);
			textt = textd; textd = texts; texts = textt;
		}
		snprintf (textd, MAXTEXT, "%s  Points:%d\n", texts, mw->p_cnt);
		textt = textd; textd = texts; texts = textt;
		for (i = 0; i < mw->p_cnt; i++) {
			snprintf (textd, MAXTEXT, "%s     %d  lon:%3.6f lat:%3.6f\n", 
			          texts, i, mw->p[i].lon, mw->p[i].lat);
			textt = textd; textd = texts; texts = textt;
		}

		tbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (wa_info));
		gtk_text_buffer_set_text (tbuf, texts, -1);
	}
	else {
		sprintf (text1, "no data");
		tbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (wa_info));
		gtk_text_buffer_set_text (tbuf, text1, -1);
	}

	return TRUE;
};


/*
 * refresh way list
 */
void wa_list_refresh (struct map_hashpos hpos) {
	GtkTreeStore *store;
	GtkTreeIter toplevel;
	struct map_way *mw;
	struct map_hash *mh = NULL;
	char wayid[32], *type, waysid[32];
	char *way_types[] = {"MWAY_residential","MWAY_highway","MWAY_tertiary",
		"MWAY_cycleway","MWAY_footway","MWAY_track","MWAY_service","MWAY_unknown","MWAY_MAX"};

	/* add elements... */
	store = gtk_tree_store_new (WLIST_C_MAX, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
	if ((mh = map_hash_geti (hpos.ilon, hpos.ilat, MHLOAD_RELOAD))) {
		mw = mh->ways;
		
		while (mw) {
			gtk_tree_store_append (store, &toplevel, NULL);
			sprintf (wayid, "%lld", mw->id);
			sprintf (waysid, "%d", mw->subid);
			type = way_types[mw->type];
			gtk_tree_store_set (store, &toplevel, WLIST_C_ID, wayid, WLIST_C_SUBID, waysid, WLIST_C_TYPE, type,
						WLIST_C_NAME, mw->name, -1);
			mw = mw->next;
		}
	}
	
	gtk_tree_view_set_model (GTK_TREE_VIEW(wa_list), GTK_TREE_MODEL(store));
	g_object_unref(store);
};


/*
 * refresh the hash list
 */
void hash_list_refresh () {
	GtkTreeStore *store;
	GtkTreeIter toplevel;
	int i;
	char lon[16], lat[16], ways[16], areas[16], flags[16], memory[16], places[16], pois[16];
	
	/* add elements... */
	store = gtk_tree_store_new (HLIST_C_MAX, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, 
	                            G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);

	for (i = 0; i < mhash_max; i++) if (mhash[i]) {
		gtk_tree_store_append (store, &toplevel, NULL);
		sprintf (lon, "%d", mhash[i]->pos.ilon);
		sprintf (lat, "%d", mhash[i]->pos.ilat);
		sprintf (ways, "%d", mhash[i]->wayscnt);
		sprintf (areas, "%d", mhash[i]->areascnt);
		sprintf (places, "%d", mhash[i]->placescnt);
		sprintf (pois, "%d", mhash[i]->poiscnt);
		sprintf (flags, "%x", mhash[i]->status);
		sprintf (memory, "%d", mhash[i]->datasize);
		gtk_tree_store_set (store, &toplevel, HLIST_C_NR, i, HLIST_C_LON, lon, HLIST_C_LAT, lat, 
		                    HLIST_C_WAYS, ways, HLIST_C_AREAS, areas,
		                    HLIST_C_PLACES, places, HLIST_C_POIS, pois,
		                    HLIST_C_FLAGS, flags, HLIST_C_MEMORY, memory, -1);
	}
	gtk_tree_view_set_model (GTK_TREE_VIEW(hash_list), GTK_TREE_MODEL(store));
	g_object_unref(store);
};


struct map_hashpos hash_list_getselected () {
	GtkTreeSelection *selection;
	GtkTreeModel     *model;
	GtkTreeIter       iter;
	gchar *ilon, *ilat;
	struct map_hashpos ret;
	
	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(hash_list));
	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get (model, &iter, HLIST_C_LON, &ilon, -1);
		gtk_tree_model_get (model, &iter, HLIST_C_LAT, &ilat, -1);

		ret.ilon = atoi (ilon);
		ret.ilat = atoi (ilat);
		g_free (ilon);
		g_free (ilat);
	}
	
	return ret;
}


int wa_list_getselected (unsigned long long int *id, short int *subid) {
	GtkTreeSelection *selection;
	GtkTreeModel     *model;
	GtkTreeIter       iter;
	int reti = 0;
	gchar *sid;
	gchar *ssubid;

	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(wa_list));
	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get (model, &iter, WLIST_C_ID, &sid, -1);
		gtk_tree_model_get (model, &iter, WLIST_C_SUBID, &ssubid, -1);
		*id = atoll(sid);
		*subid = atoi (ssubid);
		g_free (sid);
		g_free (ssubid);
		reti = 1;
	}
	
	return reti;
}


/*
 * creates the main window 
 */
void mapinfo_wnd_show () {
	GtkWidget *main_box, *vpaned, *hpaned, *box1, *button, *scrwnd;
	GtkTreeViewColumn *col;
	GtkCellRenderer *renderer;

	if (mapinfo_wnd == NULL) {
		mapinfo_wnd = gtk_window_new (GTK_WINDOW_TOPLEVEL);
		gtk_window_set_title (GTK_WINDOW(mapinfo_wnd), _("spOSMroute - MAPINFO"));
		gtk_window_set_default_size (GTK_WINDOW(mapinfo_wnd), 600, 450);
		gtk_container_set_border_width (GTK_CONTAINER (mapinfo_wnd), 4);
		
		g_signal_connect (G_OBJECT (mapinfo_wnd), "delete_event", G_CALLBACK (mapinfo_wnd_delete_event), NULL);
		main_box = gtk_hbox_new (FALSE, 1);
		gtk_container_add (GTK_CONTAINER (mapinfo_wnd), main_box);

		/*  button leiste */
		box1 = gtk_vbox_new (FALSE, 1);
		gtk_box_pack_start (GTK_BOX (main_box), box1, FALSE, FALSE, 4);
		button = gtk_button_new_from_stock (GTK_STOCK_REFRESH);	
		gtk_box_pack_start (GTK_BOX (box1), button, FALSE, FALSE, 8);
		g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (mapinfo_refresh_click), NULL);

		/*
		 * hash table 
		 */
		vpaned = gtk_vpaned_new ();
		gtk_box_pack_start (GTK_BOX (main_box), vpaned, TRUE, TRUE, 0);
	
		/* add the scroll windows with the treeview.. and add the button release event.. */
		scrwnd = gtk_scrolled_window_new (NULL, NULL);
		gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrwnd), GTK_SHADOW_IN);
		hash_list = gtk_tree_view_new ();
		gtk_container_add (GTK_CONTAINER (scrwnd), hash_list);
		
		gtk_signal_connect (GTK_OBJECT (hash_list), "button-release-event", GTK_SIGNAL_FUNC (hash_list_click), NULL);
		gtk_signal_connect (GTK_OBJECT (hash_list), "key-release-event", GTK_SIGNAL_FUNC (hash_list_click), NULL);

		/* add the rows */
		/* nr */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Nr"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(hash_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", HLIST_C_NR);
	
		/* Changed */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Flags"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(hash_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", HLIST_C_FLAGS);
	
		/* lon */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Lon"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(hash_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", HLIST_C_LON);
	
		/* lat */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Lat"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(hash_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", HLIST_C_LAT);
	
		/* ways */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Ways"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(hash_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", HLIST_C_WAYS);
	
		/* areas */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Areas"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(hash_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", HLIST_C_AREAS);
	
		/* places */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Places"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(hash_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", HLIST_C_PLACES);

		/* pois */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Pois"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(hash_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", HLIST_C_POIS);
	
		/* Memory */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Memory"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(hash_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", HLIST_C_MEMORY);
	
		gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW(hash_list)), GTK_SELECTION_SINGLE);
		gtk_paned_pack1 (GTK_PANED (vpaned), scrwnd, FALSE, FALSE);
		
		/*
		 * way/area list
		 */
		hpaned = gtk_hpaned_new ();
		gtk_paned_pack2 (GTK_PANED (vpaned), hpaned, FALSE, FALSE);
		
		/* add the scroll windows with the treeview.. and add the button release event.. */
		scrwnd = gtk_scrolled_window_new (NULL, NULL);
		gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrwnd), GTK_SHADOW_IN);
		wa_list = gtk_tree_view_new ();
		gtk_container_add (GTK_CONTAINER (scrwnd), wa_list);
		gtk_signal_connect (GTK_OBJECT (wa_list), "button-release-event", GTK_SIGNAL_FUNC (wa_list_click), NULL);
		gtk_signal_connect (GTK_OBJECT (wa_list), "key-release-event", GTK_SIGNAL_FUNC (wa_list_click), NULL);

		/* add the rows */
		/* id */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Id"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(wa_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", WLIST_C_ID);
	
		/* subid */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Subid"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(wa_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", WLIST_C_SUBID);
	
		/* Type */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Type"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(wa_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", WLIST_C_TYPE);
	
		/* name */
		col = gtk_tree_view_column_new ();
		gtk_tree_view_column_set_title (col, _("Name"));
		gtk_tree_view_append_column (GTK_TREE_VIEW(wa_list), col);
		renderer = gtk_cell_renderer_text_new ();
		gtk_tree_view_column_pack_start (col, renderer, TRUE);
		gtk_tree_view_column_add_attribute (col, renderer, "text", WLIST_C_NAME);
		
		gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW(wa_list)), GTK_SELECTION_SINGLE);
		gtk_paned_pack1 (GTK_PANED (hpaned), scrwnd, FALSE, TRUE);
		
		/*
		 * way/area info
		 */
		scrwnd = gtk_scrolled_window_new (NULL, NULL);
		gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrwnd), GTK_SHADOW_IN);
		wa_info = gtk_text_view_new ();
		gtk_container_add (GTK_CONTAINER (scrwnd), wa_info);
		gtk_paned_pack2 (GTK_PANED (hpaned), scrwnd, TRUE, FALSE);
		gtk_widget_show_all (mapinfo_wnd);
		
		gtk_text_view_set_editable (GTK_TEXT_VIEW(wa_info), FALSE);

	}
	else 
		gtk_widget_show_all (mapinfo_wnd);
	
	hash_list_refresh ();
};

