/***************************************************************************
 *            gui_favorites.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 "favorites.h"
#include "memoryleak.h"
#include "system.h"

void gui_fav_close ();
void gui_fav_add ();
void gui_fav_del ();
void gui_fav_ren ();
void gui_fav_refresh ();
void gui_fav_callback_close ();
void gui_fav_selitem (int nr);

void gui_fav_fl_create ();
void gui_fav_fl_free ();

GUIList *favlist = NULL;
GUIEntry *faventry_name = NULL;
GUIButton *favbtn_close = NULL;
GUIButton *favbtn_add = NULL;
GUIButton *favbtn_ren = NULL;
GUIButton *favbtn_del = NULL;

char **fl_array = NULL;	// array to all elements
int fl_array_cnt = 0;

static GUIWindow wfav = {0};


void gui_fav_close () {
	gui_close ();
};


void gui_fav_add () {
	struct favorite f;
	if (faventry_name->text[0] == 0) return;
	d_printf ("gui_fav_add: '%s'", faventry_name->text);
	strncpy (f.name, faventry_name->text, FAV_NAME_LEN);
	f.pos.lon = view_lon;
	f.pos.lat = view_lat;
	fav_add (-1, &f);
	gui_fav_refresh ();
};


void gui_fav_del () {
	if (favlist->selected < 0) return;
	fav_del (favlist->selected);
	gui_fav_refresh ();
};


void gui_fav_ren () {
	if (favlist->selected < 0 || faventry_name->text[0] == 0) return;
	strncpy (favorites[favlist->selected].name, faventry_name->text, FAV_NAME_LEN);
	gui_fav_refresh ();
};


void gui_fav_selitem (int nr) {
	if (nr < favorites_cnt) {
		strncpy (faventry_name->text, favorites[nr].name, GUI_TEXTLEN);
		view_lon = favorites[nr].pos.lon;
		view_lat = favorites[nr].pos.lat;
		gui_close ();
		draw_del_flag (DRAW_GPSFOLLOW);
	}
	else faventry_name->text[0] = 0;
};


void gui_fav_callback_close () {
	fav_save (fav_getfilename ());
	gui_fav_fl_free ();
};


void gui_fav_refresh () {
	gui_fav_fl_create ();
	faventry_name->text[0] = 0;
	favlist->data = fl_array;
};


void gui_fav_fl_create () {
	int i;

	if ((fl_array_cnt-1) < favorites_cnt) { 
		gui_fav_fl_free ();
		fl_array_cnt = favorites_cnt + 8;
		fl_array = (char **) ml_malloc (sizeof (char *) * fl_array_cnt);
	}

	for (i = 0; i < favorites_cnt; i++) {
		fl_array[i] = favorites[i].name;
	}
	fl_array[favorites_cnt] = NULL;
};


void gui_fav_fl_free () {
	if (fl_array) ml_free (fl_array);
	fl_array = NULL;
	fl_array_cnt = 0;
};


void gui_fav_show () {
	fav_new ();
	fav_load (fav_getfilename());
	
	if (wfav.screen == NULL) gui_window_new (&wfav, 220, 240, _("Favorites"));
	wfav.callback_close = (void*)gui_fav_callback_close;
	wfav.screen_changed = 1;
	wfav.style = WGUI_S_VCENTER | WGUI_S_HCENTER;

	/* favotires buttons */
	if (favbtn_close == NULL) {
		favbtn_close = gui_button_new (_("Close"), 5, 210, 60, 20);
		favbtn_close->callback_clicked = (void*)gui_fav_close;
		gui_window_item_add (&wfav, GUI_BUTTON, favbtn_close);
	}

	if (favbtn_add == NULL) {
		favbtn_add = gui_button_new (_("Add"), 5, 15, 60, 20);
		favbtn_add->callback_clicked = (void*)gui_fav_add;
		gui_window_item_add (&wfav, GUI_BUTTON, favbtn_add);
	}

	if (favbtn_del == NULL) {
		favbtn_del = gui_button_new (_("Del"), 70, 15, 60, 20);
		favbtn_del->callback_clicked = (void*)gui_fav_ren;
		gui_window_item_add (&wfav, GUI_BUTTON, favbtn_del);
	}

	if (favbtn_ren == NULL) {
		favbtn_ren = gui_button_new (_("Ren"), 135, 15, 60, 20);
		favbtn_ren->callback_clicked = (void*)gui_fav_ren;
		gui_window_item_add (&wfav, GUI_BUTTON, favbtn_ren);
	}

	if (favlist == NULL) {
		favlist = gui_list_new (5, 45, 210, 130);
		favlist->callback_selectitem = (void*)gui_fav_selitem;
		gui_window_item_add (&wfav, GUI_LIST, favlist);
	}

	if (faventry_name == NULL) {
		faventry_name = gui_entry_new (NULL, 5, 180, 210, 20);
		gui_window_item_add (&wfav, GUI_ENTRY, faventry_name);
	}

	gui_fav_refresh ();
	gui_show (&wfav);
};
