/*
 *  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"
#include "draw.h"
#include "system.h"
#include "routing.h"

GtkActionGroup *main_menu_ac;
int gtk_mapdownload_value = MAPDOWNLOAD_MAP;

/* 
 * quit the programm 
 */
void main_menu_quit () {
	gtk_running = 0;
	gtk_main_quit ();
};

/* 
 * preferences
 */
void main_menu_pref () {
};


/*
 * map download settings.
 */
void main_menu_mapdownload (GtkRadioAction *action, guint callback_action, GtkWidget *widget) {
	gtk_mapdownload_value = gtk_radio_action_get_current_value (action);
};


void main_menu_refresh () {
	if (gtk_mapdownload_value == MAPDOWNLOAD_MAP) {
		map_refresh (view_lon, view_lat);
		draw_redrawmap ();
		draw ();
	}
	else if (gtk_mapdownload_value == MAPDOWNLOAD_OSM) {
		struct map_pos hs, he;
		iPoint ss = gfx_screensize;

		hs.lat = view_lat - 2.0 * map_km2lat(view_scale * ((float)ss.y/10));
		hs.lon = view_lon - 2.0 * map_km2lon(view_scale * ((float)ss.x/10), hs.lat);
		he.lat = view_lat + 2.0 * map_km2lat(view_scale * ((float)ss.y/10));
		he.lon = view_lon + 2.0 * map_km2lon(view_scale * ((float)ss.x/10), he.lat);

		map_load_web (hs, he);
		app_status (-1, "", 0.0);
		draw ();
	}
};


void main_menu_mapinfo () {
	mapinfo_wnd_show ();
};


void main_menu_mapdefrag () {
	map_defrag ();
};


void main_menu_saveall () {
	app_status (-1,_("save all"), 0.0);
	map_save_all();
	app_status (-1, "", 0.0);
};


void  main_menu_gps_enabled () {
	if (gtk_toggle_action_get_active (gpsenabled))
		gps_start ();
	else 
		gps_stop ();
};


void main_menu_gps_setting () {
	gpssetting_wnd_show (main_wnd);
};


void main_menu_gps_loadlog () {
	char *fn;

	fn = dialog_getfilename ("NEMA", "nema", 0);
	if (fn)
		gps_load_log (fn);
};


void main_menu_gpxexport () {
	char *fn;

	fn = dialog_getfilename ("GPX-File", "gpx", 1);
	if (fn)
		gpsroute_exportgpx (gpsroute, fn);
};


void main_menu_osmimport () {
	char *fn;

	app.status = APPSTATUS_loadfromweb;

	fn = dialog_getfilename ("OSM-File", "osm", 0);
	if (fn) {
		osm_loadfile (fn);
		draw_redrawmap ();
	}
	app_status (APPSTATUS_nothing, "", 0.0);
};


void main_menu_map_searchsort () {
	map_search_sort ();
	app_status (-1, "", 0.0);
};


void main_menu_map_searchrefresh () {
	char lfn[LEN_FILENAME];
	char rfn[LEN_FILENAME] = "search.mapidx";

	map_search_get_filename (lfn, LEN_FILENAME);
	
	map_webload (rfn, lfn, NULL);
	app_status (-1, "", 0.0);
};


/*
 * Create the menu for the main window
 */
GtkWidget *main_menu_create (GtkWidget *wnd) {
	GtkUIManager *ui_manager;
	GtkAccelGroup *accel_group;
	GError *error;

	/* Normal items */
	static const GtkActionEntry entries[] = {
		{ "AppMenu", NULL, N_("_spOSMRoute") },
		{ "AppQuit", GTK_STOCK_QUIT, N_("_Quit"), "<control>Q", NULL, main_menu_quit },
		{ "AppPref", GTK_STOCK_PREFERENCES, N_("_Prefernces.."), "<control>P", NULL, main_menu_pref },
		{ "MapMenu", NULL, N_("_Map") },
		{ "MapDownload", NULL, N_("Download") },
		{ "MapRefresh", NULL, N_("_Refresh") , NULL, NULL, main_menu_refresh },
		{ "MapAddOSM", NULL, N_("_Import OSM"), NULL, NULL, main_menu_osmimport },
		{ "MapInfo", NULL, N_("_Map Information") , NULL, NULL, main_menu_mapinfo },
		{ "MapSaveall", NULL, N_("_Save All") , NULL, NULL, main_menu_saveall },
		{ "MapDefrag", NULL, N_("_Defrag Map") , NULL, NULL, main_menu_mapdefrag },
		{ "MapSearchRefresh", NULL, N_("Map Index Refresh"), NULL, NULL, main_menu_map_searchrefresh },
		{ "MapSearchSort", NULL, N_("Map Index Sort"), NULL, NULL, main_menu_map_searchsort },
		{ "GPS", NULL, N_("_GPS") },
		{ "GPSSetting", GTK_STOCK_PREFERENCES, N_("_Preferences") , NULL, NULL, main_menu_gps_setting },
		{ "GPSExport", NULL, N_("_Export GPX") , NULL, NULL, main_menu_gpxexport },
		{ "GPSLoadLog", NULL, N_("_Load GPSLog") , NULL, NULL, main_menu_gps_loadlog },
		{ "HelpMenu", NULL, N_("_Help") },
		{ "HelpAbout", NULL, N_("_About") , NULL, NULL, NULL }
	};

	static const GtkRadioActionEntry entries_mapdownload[] = {
		{ "MapDownloadMap", NULL, N_("_spOSMroute Map (fast)"), NULL, NULL, MAPDOWNLOAD_MAP },
		{ "MapDownloadOSM", NULL, N_("_Open Street Map (very slow)"), NULL, NULL, MAPDOWNLOAD_OSM },
	};

	static const GtkToggleActionEntry entries_toggle[] = {
		{ "GPSEnabled", NULL, N_("Enabled"), NULL, NULL, main_menu_gps_enabled, FALSE },
	};
	
	static const char *ui_description = 
		"<ui>\n"
		"  <menubar name='MainMenu'>\n"
		"    <menu action='AppMenu'>\n"
		"      <menuitem action='AppPref'/>\n"
		"      <menuitem action='AppQuit'/>\n"
		"    </menu>\n"
		"    <menu action='MapMenu'>\n"
		"      <menu action='MapDownload'>\n"
		"        <menuitem action='MapDownloadMap'/>\n"
		"        <menuitem action='MapDownloadOSM'/>\n"
		"      </menu>\n"
		"      <menuitem action='MapAddOSM'/>\n"
		"      <menuitem action='MapRefresh'/>\n"
		"      <menuitem action='MapInfo'/>\n"
		"      <menuitem action='MapSaveall'/>\n"
		"      <menuitem action='MapDefrag'/>\n"
		"	   <separator />\n"
		"      <menuitem action='MapSearchRefresh'/>\n"
		"      <menuitem action='MapSearchSort'/>\n"
		"    </menu>\n"
		"	 <menu action='GPS'>\n"
		"      <menuitem action='GPSEnabled'/>\n"
		"	   <separator />\n"
		"      <menuitem action='GPSExport'/>\n"
		"      <menuitem action='GPSLoadLog'/>\n"
		"      <menuitem action='GPSSetting'/>\n"
		"    </menu>\n"
		"	 <menu action='HelpMenu'>\n"
		"      <menuitem action='HelpAbout'/>\n"
		"    </menu>\n"
		"  </menubar>\n"
		"</ui>";

	main_menu_ac = gtk_action_group_new ("MenuActions");
	gtk_action_group_add_actions (main_menu_ac, entries, G_N_ELEMENTS (entries), wnd);
	gtk_action_group_add_radio_actions (main_menu_ac, entries_mapdownload, G_N_ELEMENTS (entries_mapdownload), MAPDOWNLOAD_MAP, G_CALLBACK (main_menu_mapdownload), wnd);
	gtk_action_group_add_toggle_actions (main_menu_ac, entries_toggle, G_N_ELEMENTS (entries_toggle), 0);
	ui_manager = gtk_ui_manager_new ();
	gtk_ui_manager_insert_action_group (ui_manager, main_menu_ac, 0);
	gpsenabled = GTK_TOGGLE_ACTION(gtk_action_group_get_action (main_menu_ac, "GPSEnabled"));
//	gpsenabled = gtk_ui_manager_get_widget (ui_manager, "/MainMenu/GPS/GPSEnabled");

	accel_group = gtk_ui_manager_get_accel_group (ui_manager);
	gtk_window_add_accel_group (GTK_WINDOW (wnd), accel_group);

	error = NULL;
	if (!gtk_ui_manager_add_ui_from_string (ui_manager, ui_description, -1, &error)) {
		g_message ("building menus failed: %s", error->message);
		g_error_free (error);
		errorexit (-1);
	}

	return gtk_ui_manager_get_widget (ui_manager, "/MainMenu");;
};

//
// 	gtk_action_set_sensitive (gtk_action_group_get_action (mainmenu_action_group, "WComputer"), enabled);
//
