/***************************************************************************
 *            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 ();

GUIItem *favlist = NULL;
GUIItem *faventry_name = NULL;
GUIItem *favbtn_close = NULL;
GUIItem *favbtn_add = NULL;
GUIItem *favbtn_ren = NULL;
GUIItem *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 (GUI_ENTRY_T(faventry_name)->text[0] == 0) return;
	d_printf ("gui_fav_add: '%s'", GUI_ENTRY_T(faventry_name)->text);
	strncpy (f.name, GUI_ENTRY_T(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 (GUI_LIST_T(favlist)->selected < 0) return;
	fav_del (GUI_LIST_T(favlist)->selected);
	gui_fav_refresh ();
};


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


void gui_fav_selitem (int nr) {
	if (nr < favorites_cnt) {
		strncpy (GUI_ENTRY_T(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 GUI_ENTRY_T(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 ();
	GUI_ENTRY_T(faventry_name)->text[0] = 0;
	GUI_LIST_T(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);
		GUI_BUTTON_T(favbtn_close)->callback_clicked = (void*)gui_fav_close;
	}

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

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

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

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

	if (faventry_name == NULL) {
		faventry_name = gui_entry_new (NULL, 5, 180, 210, 20);
	}
	gui_window_item_add (&wfav, faventry_name);
	gui_window_item_add (&wfav, favlist);
	gui_window_item_add (&wfav, favbtn_ren);
	gui_window_item_add (&wfav, favbtn_del);
	gui_window_item_add (&wfav, favbtn_add);
	gui_window_item_add (&wfav, favbtn_close);

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