/***************************************************************************
 *            gui_search.c
 *
 *  2011-03-10
 *  Copyright (C) 2011  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"

#define SEARCH_MAXRESULT 32

void gui_search_close ();
void gui_search_city ();
void gui_search_switch (int pos);

struct map_search_dataex {
	float distance;
	char text[GUI_TEXTLEN];
};

struct map_search_data search_data[SEARCH_MAXRESULT];
struct map_search_dataex search_dataex[SEARCH_MAXRESULT];
char *search_chars[SEARCH_MAXRESULT+1];
struct map_pos search_pos;
int search_data_cnt = 0;
int search_data_cur = -1;
static GUIWindow wsearch = {0};

GUIItem *ws_entryname = NULL;
GUIItem *ws_labelname = NULL;
GUIItem *ws_entrystreet = NULL;
GUIItem *ws_labelstreet = NULL;
GUIItem *ws_btnok = NULL;
GUIItem *ws_btncancel = NULL;
GUIItem *ws_list = NULL;
GUIItem *ws_map = NULL;  /* need to finish working on: gui_images and map_view */

void gui_search_show () {
	int i;

	if (wsearch.screen == NULL) gui_window_new (&wsearch, 220, 260, _("Search.."));
	wsearch.screen_changed = 1;
	wsearch.style = WGUI_S_VCENTER | WGUI_S_HCENTER;
	strncpy (wsearch.title, _("Search"), GUI_TEXTLEN);

    /* Name Ok Button */
    if (ws_labelname == NULL) ws_labelname = gui_label_new (_("City:"), 5, 10);
 	gui_window_item_add (&wsearch, ws_labelname);

    if (ws_entryname == NULL) ws_entryname = gui_entry_new (_(""), 50, 10, 100, 20);
 	gui_window_item_add (&wsearch, ws_entryname);
	GUI_ENTRY_T (ws_entryname)->callback_enter = gui_search_city;

	if (ws_btnok == NULL) ws_btnok = gui_button_new (_("Ok"), 165, 5, 50, 20);
    GUI_BUTTON_T (ws_btnok)->callback_clicked = (void*) gui_search_close;
 	gui_window_item_add (&wsearch, ws_btnok);

    /* street Cancel Button */
    if (ws_labelstreet == NULL) ws_labelstreet = gui_label_new (_("Street:"), 5, 35);
 	gui_window_item_add (&wsearch, ws_labelstreet);

    if (ws_entrystreet == NULL) ws_entrystreet = gui_entry_new (_(""), 50, 30, 100, 20);
 	gui_window_item_add (&wsearch, ws_entrystreet);

    if (ws_btncancel == NULL) ws_btncancel = gui_button_new (_("Cancel"), 165, 30, 50, 20);
    GUI_BUTTON_T (ws_btncancel)->callback_clicked = (void*) gui_search_close;
 	gui_window_item_add (&wsearch, ws_btncancel);

    /* result list */
    if (ws_list == NULL) ws_list = gui_list_new (5, 55, 210, 100);
    GUI_LIST_T(ws_list)->callback_selectitem = (void*) gui_search_switch;
 	gui_window_item_add (&wsearch, ws_list);

 	if (gps_isrunning()) {
 		struct gps_data *gpsdata = gps_getposition ();
 		search_pos.lon = gpsdata->lon;
 		search_pos.lat = gpsdata->lat;
 	}
 	else {
 		search_pos.lon = view_lon;
 		search_pos.lat = view_lat;
 	}

 	for (i = 0; i < SEARCH_MAXRESULT+1; i++) search_chars[i] = NULL;

	gui_show (&wsearch);
};


void gui_search_close () {
	gui_close ();
};


void gui_search_city () {
	int i;
	struct map_pos pos;

	search_data_cnt = map_search (GUI_ENTRY_T(ws_entryname)->text, search_data, SEARCH_MAXRESULT);
	for (i = 0; i < search_data_cnt && i < SEARCH_MAXRESULT; i++) {
		pos.lon = search_data[i].lon;
		pos.lat = search_data[i].lat;
		search_dataex[i].distance = map_getdistance (pos, search_pos);
		snprintf (search_dataex[i].text, GUI_TEXTLEN, "%4.0fkm  %s", search_dataex[i].distance, search_data[i].name);
		search_chars[i] = search_dataex[i].text;

		d_printf ("  %-3d:%s  distance:%f     '%s'",i , search_data[i].name, search_dataex[i].distance, search_dataex[i].text);
	}
	search_chars[i] = NULL;
	GUI_LIST_T(ws_list)->data = search_chars;
};


void gui_search_switch (int pos) {
	d_printf ("gui_search_switch %d to %d", search_data_cur, pos);
	search_data_cur = pos;
	if (search_data_cnt <= 0) search_data_cur = -1;
	else if (search_data_cur >= search_data_cnt) search_data_cur = 0;
	else if (search_data_cur < 0) search_data_cur = search_data_cnt - 1;

	if (search_data_cur >= 0) {
		view_lon = search_data[search_data_cur].lon;
		view_lat = search_data[search_data_cur].lat;
		wsearch.screen_changed = 1;
		draw ();
	}
};
