/* %Id: Exp $ */
/***************************************************************************
 *            gui_selectdir.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"

#include <sys/types.h>
#include <dirent.h>

void gui_sdir_close ();
void gui_sdir_update ();
void gui_sdir_clear ();
void gui_sdir_clicklist (int nr);

GUIWindow wsdir = {0};
GUIItem *wsd_close = NULL;
GUIItem *wsd_path = NULL;
GUIItem *wsd_dirlist = NULL;

char **wsd_list = NULL;
int wsd_listmax = 0;
char wsd_select_dir[LEN_FILENAME];

char *gui_sdir_show (char *startpath) {
    if (wsdir.screen == NULL) gui_window_new (&wsdir, 220, 250, _("Select Dir"));
    wsdir.screen_changed = 1;
    wsdir.style = WGUI_S_VCENTER | WGUI_S_HCENTER;

    /* close Button */
    if (wsd_close == NULL) wsd_close = gui_button_new (_("Close"), 50, 220, 100, 20);
    GUI_BUTTON_T (wsd_close)->callback_clicked = (void*) gui_sdir_close;
 	gui_window_item_add (&wsdir, wsd_close);

    if (wsd_path == NULL) wsd_path = gui_entry_new (startpath, 10, 25, 200, 25);
 	gui_window_item_add (&wsdir, wsd_path);

	if (wsd_dirlist == NULL) wsd_dirlist = gui_list_new (10, 55, 200, 140);
	GUI_LIST_T (wsd_dirlist)->callback_selectitem = gui_sdir_clicklist;
	GUI_LIST_T (wsd_dirlist)->data = wsd_list;
	gui_window_item_add (&wsdir, wsd_dirlist);

	gui_sdir_update ();

	gui_show (&wsdir);
    while (currentwin == &wsdir) {
    	main_wnd_update ();
    }
    strncpy (wsd_select_dir, GUI_ENTRY_T(wsd_path)->text, LEN_FILENAME-1);

    if (wsd_select_dir[strlen(wsd_select_dir)-1] != DIR_SEP) {
    	int i = strlen(wsd_select_dir);
    	wsd_select_dir[i] = DIR_SEP;
    	wsd_select_dir[i+1] = '\0';
    }

    return wsd_select_dir;
};


void gui_sdir_close () {
	gui_close ();
};


void gui_sdir_clicklist (int nr) {
	char fname[LEN_FILENAME];
	char *seperate;

	d_printf ("selected nr:%d   '%s'", nr, nr < wsd_listmax ? wsd_list[nr] : "nr >= wsd_listmax");
	if (nr >= wsd_listmax || wsd_list[nr] == NULL) return;

	if (strcmp (wsd_list[nr], ".") == 0) return;
	if (strcmp (wsd_list[nr], "..") == 0) {
		strncpy (fname, GUI_ENTRY_T(wsd_path)->text, LEN_FILENAME);
		while (strlen (fname)>1 && fname[strlen (fname) -1] == DIR_SEP)
			fname[strlen (fname)-1] = '\0';
		if (strlen (fname) > 1) {
			seperate = strrchr(fname, DIR_SEP);
			if (seperate != NULL) {
				seperate[0] = '\0';
			}
		}
		gui_entry_settext (wsd_path, fname);
	}
	else {
		if (GUI_ENTRY_T(wsd_path)->text[strlen (GUI_ENTRY_T(wsd_path)->text)-1] == DIR_SEP)
			snprintf (fname, LEN_FILENAME, "%s%s", GUI_ENTRY_T(wsd_path)->text, wsd_list[nr]);
		else
			snprintf (fname, LEN_FILENAME, "%s%c%s", GUI_ENTRY_T(wsd_path)->text, DIR_SEP, wsd_list[nr]);

		gui_entry_settext (wsd_path, fname);
	}

	gui_sdir_update ();
//	wsdir.screen_changed = 1;
//	draw ();
};


void gui_sdir_update () {
	int cnt;
	DIR *dir = NULL;
	struct dirent *direntry = NULL;
	char fname[LEN_FILENAME];
	struct stat stbuf;

	/* clear list and reopen directory */
	gui_sdir_clear ();
	if ((dir = opendir (GUI_ENTRY_T(wsd_path)->text)) == NULL) {
		/* if open fails, reopen with ROOT_DIR which is defined in system.h */
		gui_entry_settext (wsd_path, ROOT_DIR);
		if ((dir = opendir (GUI_ENTRY_T(wsd_path)->text)) == NULL) {
			d_printf ("could not open dir for reading. errno:%d '%s'", errno, strerror(errno));
			return;
		}
	}

	/* go through the directory but only add directory entries */
	cnt = wsd_listmax = 0;

	/* need more memory? */
	while ((direntry = readdir (dir))) {
		if (GUI_ENTRY_T(wsd_path)->text[strlen (GUI_ENTRY_T(wsd_path)->text)-1] == DIR_SEP)
			snprintf (fname, LEN_FILENAME, "%s%s", GUI_ENTRY_T(wsd_path)->text, direntry->d_name);
		else
			snprintf (fname, LEN_FILENAME, "%s%c%s", GUI_ENTRY_T(wsd_path)->text, DIR_SEP, direntry->d_name);
		if (stat(fname, &stbuf) == -1) {
			d_printf ("could not read stat of file: %s", fname);
			continue;
		}
		d_printf ("dir:%s", fname);
		if (S_ISDIR(stbuf.st_mode)) {
			/* need more memory? */
			if (cnt >= wsd_listmax) {
				/* save old pointer and counter */
				char **tmp = wsd_list;
				int i = wsd_listmax-1;

				/* allocate new list */
				wsd_listmax += 512;
				wsd_list = ml_malloc (wsd_listmax * sizeof(void*));
				memset (wsd_list, 0x0, wsd_listmax * sizeof(void*));

				/* copy list */
				if (tmp) {
					for (; i >= 0; i--) wsd_list[i] = tmp[i];
					ml_free (tmp);
				}

				/* setup new list */
				GUI_LIST_T (wsd_dirlist)->data = wsd_list;
			}

			wsd_list[cnt] = ml_malloc (LEN_FILENAME);
			strncpy (wsd_list[cnt], direntry->d_name, LEN_FILENAME);
			d_printf ("added entry:%-3d '%s'", cnt, direntry->d_name);
			cnt++;
		}
	}

	closedir (dir);
};


/*
 * remove all listentrys and free list
 */
void gui_sdir_clear () {
	int i;

	if (wsd_list == NULL) return;

	for (i = 0; wsd_list[i]; i++) {
		if (wsd_list[i]) ml_free (wsd_list[i]);
		wsd_list[i] = NULL;
	}
};

