/* %Id: Exp $ */
/***************************************************************************
 *            gui_config.c
 *
 *  Copyright (C) 2013  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 3 of the License, or
 * (at your option) any later version.
 * 
 * main.c 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 "osmroute.h"
#include "draw.h"
#include "gui.h"
#include "system.h"

void gui_config_close ();
void gui_config_seldir ();
void gui_config_gpsfile ();
void gui_config_gpslist (int nr);
void gui_config_checkbox_changed ();

GUIWindow wcfg = {0};

GUIItem *map_path = NULL;
GUIItem *map_seldir = NULL;
GUIItem *gps_device = NULL;
GUIItem *gps_list = NULL;
GUIItem *gps_file = NULL;
GUIItem *log_file = NULL;
GUIItem *cb_debug = NULL;
GUIItem *cb_softkeyb = NULL;
GUIItem *wcfg_close = NULL;
GUIItem *label1 = NULL;
GUIItem *label2 = NULL;

char gpscfg_dev[8][LEN_FILENAME] = {
#ifdef ANDROID
		"android:",
#else
		"serial:/dev/rfcomm0",
#endif
		"serial:com1,38400",
		"serial:com1,4800",
		"",
		"",
		"",
		"",
		""};
char *gpscfg_dev_ptr[8] = {
		gpscfg_dev[0],
		gpscfg_dev[1],
		gpscfg_dev[2],
		NULL,
		NULL,
		NULL,
		NULL,
		NULL
};


void gui_config_show () {
    if (wcfg.screen == NULL) gui_window_new (&wcfg, 220, 280, _("Config"));
    wcfg.screen_changed = 1;
    wcfg.style = WGUI_S_VCENTER | WGUI_S_HCENTER;
	
	if (label1 == NULL) label1 = gui_label_new (_("mapdata path:"), 5, 20);
	gui_window_item_add (&wcfg, label1);
	if (map_path == NULL) map_path = gui_entry_new (cfg.mappath, 10, 36, 150, 20);
	gui_window_item_add (&wcfg, map_path);
	if (map_seldir == NULL) map_seldir = gui_button_new (_("Browse"), 170, 36, 40, 20);
	GUI_BUTTON_T (map_seldir)->callback_clicked = gui_config_seldir;
	gui_window_item_add (&wcfg, map_seldir);

	if (label2 == NULL) label2 = gui_label_new (_("gps device:"), 5, 66);
	gui_window_item_add (&wcfg, label2);
	if (gps_device == NULL) gps_device = gui_entry_new (cfg.gps_device, 10, 82, 150, 20);
	gui_window_item_add (&wcfg, gps_device);
	
	if (gps_list == NULL) gps_list = gui_list_new (10, 112, 150, 60);
	GUI_LIST_T (gps_list)->callback_selectitem = gui_config_gpslist;
	GUI_LIST_T (gps_list)->data = gpscfg_dev_ptr;
	gui_window_item_add (&wcfg, gps_list);
	if (gps_file == NULL) gps_file = gui_button_new (_("File"), 170, 112, 40, 20);
	GUI_BUTTON_T (gps_file)->callback_clicked = gui_config_gpsfile;
	gui_window_item_add (&wcfg, gps_file);

	if (log_file == NULL)
		log_file = gui_checkbox_new (_("Create GPS-Log"), cfg.gps_flags & GPSF_LOG, 10, 172);
	gui_window_item_add (&wcfg, log_file);
	GUI_CHECKBOX_T (log_file)->callback_changed = gui_config_checkbox_changed;

	if (cb_debug == NULL)
		cb_debug  = gui_checkbox_new (_("Debug"), cfg.debug, 5, 200);
	gui_window_item_add (&wcfg, cb_debug);
	GUI_CHECKBOX_T (cb_debug)->callback_changed = gui_config_checkbox_changed;

	if (cb_softkeyb == NULL)
		cb_softkeyb  = gui_checkbox_new (_("Softkeyboard"), cfg.softkeyboard, 100, 200);
	gui_window_item_add (&wcfg, cb_softkeyb);
	GUI_CHECKBOX_T (cb_softkeyb)->callback_changed = gui_config_checkbox_changed;

	if (wcfg_close == NULL) wcfg_close =
		gui_button_new (_("Close"), 5, wcfg.h-25, wcfg.w-10, 20);
	GUI_BUTTON_T (wcfg_close)->callback_clicked = gui_config_close;
	gui_window_item_add (&wcfg, wcfg_close);

	gui_show (&wcfg);
};


void gui_config_close () {
	strncpy (cfg.mappath, GUI_ENTRY_T(map_path)->text, LEN_FILENAME);
	strncpy (cfg.gps_device, GUI_ENTRY_T(gps_device)->text, LEN_FILENAME);
	gui_config_checkbox_changed ();
	gui_close ();
	map_clear ();
	map_init ();
	draw_redrawmap ();
};


void gui_config_checkbox_changed () {
	if ((cfg.debug = GUI_CHECKBOX_T(cb_debug)->checked)) SETFLAG(view_flags, DRAW_DEBUG);
	else DELFLAG(view_flags, DRAW_DEBUG);

	cfg.softkeyboard = GUI_CHECKBOX_T(cb_softkeyb)->checked;
	if (GUI_CHECKBOX_T(log_file)->checked) cfg.gps_flags |= GPSF_LOG;
	else cfg.gps_flags &= (0x0FFFF-GPSF_LOG);

	if (cfg.gps_flags) SETFLAG(gpsflags, GPSF_LOG);
	else DELFLAG(gpsflags, GPSF_LOG);
};


void gui_config_gpslist (int nr) {
	d_printf ("select nr:%d", nr);
	if (nr >= 0 && nr < 8)
		gui_entry_settext (gps_device, gpscfg_dev[nr]);
};


void gui_config_seldir () {
	char *new_dir = NULL;

	new_dir = gui_sdir_show (GUI_ENTRY_T(map_path)->text);
	d_printf ("new_dir:%s", new_dir);
	gui_entry_settext (map_path, new_dir);
	d_printf ("mapdir:%s", GUI_ENTRY_T(map_path)->text);
};


void gui_config_gpsfile () {
	char *new_file = NULL;
	char text[LEN_FILENAME];

	d_printf ("select gps file...");
	new_file = gui_sfile_show ("");
	if (new_file != NULL) {
		snprintf (text, LEN_FILENAME, "file:%s", new_file);
		gui_entry_settext (gps_device, text);
		d_printf ("gpsdevice:%s", GUI_ENTRY_T(gps_device)->text);
	}
};

