#include "ecdb.h"

static void _destroy_cb(Ewl_Widget *w, void *ev, void *data);
static void _destroy_data_page_cb(Ewl_Widget *w, void *ev, void *data);
static void _data_disc_cb(Ewl_Widget *w, void *ev, void *data);
static void _burn_image_cb(Ewl_Widget *w, void *ev, void *data);
static void _copy_disc_cb(Ewl_Widget *w, void *ev, void *data);
static void _audio_disc_cb(Ewl_Widget *w, void *ev, void *data);

static Ewl_Widget *_page_start(void);
static Ewl_Widget *_page_data(void);

static void
_destroy_cb(Ewl_Widget *w, void *ev, void *data)
{
	ewl_widget_destroy(w);
	ecore_event_add(ECORE_EVENT_SIGNAL_EXIT, NULL, NULL, NULL);
}

static void
_destroy_data_page_cb(Ewl_Widget *w, void *ev, void *data)
{
	Ecdb_Burn_Project *proj = data;
	printf("Destroy burn project data\n");
	ecdb_burn_project_destroy(proj);
}

void
ecdb_create_data_tab(void)
{
	Ewl_Widget *note_page, *notebook;

	notebook = ewl_widget_name_find("main_notebook");

	note_page = _page_data();
	ewl_container_child_append(EWL_CONTAINER(notebook), note_page);
	ewl_notebook_page_tab_text_set(EWL_NOTEBOOK(notebook), note_page,
								"Data Disc");
	ewl_notebook_visible_page_set(EWL_NOTEBOOK(notebook), note_page);
	ewl_widget_show(note_page);
}

void
ecdb_create_main_gui(void)
{
	Ewl_Widget *main_win, *vbox, *filelist, *note, *fn;

	main_win = ewl_window_new();
	ewl_window_name_set(EWL_WINDOW(main_win), "ECDB");
	ewl_window_title_set(EWL_WINDOW(main_win), "ECDB");
	ewl_callback_append(main_win, EWL_CALLBACK_DELETE_WINDOW,
			_destroy_cb, NULL);
	ewl_object_w_request(EWL_OBJECT(main_win), 640);
	ewl_object_h_request(EWL_OBJECT(main_win), 480);
	ewl_widget_name_set(main_win, "main_win");
	ewl_widget_show(main_win);

	vbox = ewl_vbox_new();
	ewl_container_child_append(EWL_CONTAINER(main_win), vbox);
	ewl_widget_show(vbox);

	filelist = ewl_filelist_new();
	ewl_container_child_append(EWL_CONTAINER(vbox), filelist);
	ewl_filelist_multiselect_set(EWL_FILELIST(filelist), TRUE);
	ewl_filelist_directory_set(EWL_FILELIST(filelist), getenv("HOME"));
	ewl_object_fill_policy_set(EWL_OBJECT(filelist), EWL_FLAG_FILL_ALL);
	ewl_object_maximum_h_set(EWL_OBJECT(filelist), 300);
	ewl_widget_show(filelist);

	note = ewl_notebook_new();
	ewl_object_fill_policy_set(EWL_OBJECT(note), EWL_FLAG_FILL_ALL);
	ewl_container_child_append(EWL_CONTAINER(vbox), note);
	ewl_notebook_tabbar_alignment_set(EWL_NOTEBOOK(note),
			EWL_FLAG_ALIGN_LEFT);
	ewl_widget_name_set(note, "main_notebook");
	ewl_widget_show(note);

	fn = _page_start();
	ewl_container_child_append(EWL_CONTAINER(note), fn);
	ewl_notebook_page_tab_text_set(EWL_NOTEBOOK(note), fn, "Start");
	ewl_widget_show(fn);
}

static Ewl_Widget
*_page_start(void)
{
	int i;
	Ewl_Widget *box, *border[3], *buttons[3];
	char *bdr_titles[] = {"Create Data Disc",
			      "Burn Image",
			      "Copy Disc",
			      "Create Audio Disc",
			      NULL};

	char *but_titles[] = {"Data Disc",
			      "Image",
			      "Copy",
			      "Audio Disc",
			      NULL};

	char *images[] = {EWL_ICON_DRIVE_CDROM,
			  EWL_ICON_MEDIA_CDROM,
			  EWL_ICON_DRIVE_HARDDISK,
			  EWL_ICON_AUDIO_X_GENERIC,
			  NULL};

	static void (*funs[]) (Ewl_Widget *w, void *ev, void *data) = 
			{_data_disc_cb,
			 _burn_image_cb,
			 _copy_disc_cb,
			 _audio_disc_cb,
			 NULL};

	box = ewl_hbox_new();
	ewl_object_fill_policy_set(EWL_OBJECT(box), EWL_FLAG_FILL_NONE);
	ewl_widget_show(box);
	
	for (i = 0; i < 4; i++)
	{
		border[i] = ewl_border_new();
		ewl_border_label_set(EWL_BORDER(border[i]), bdr_titles[i]);
		ewl_container_child_append(EWL_CONTAINER(box), border[i]);
		ewl_widget_show(border[i]);

		buttons[i] = ewl_button_new();
		ewl_container_child_append(EWL_CONTAINER(border[i]),
				buttons[i]);
		ewl_button_label_set(EWL_BUTTON(buttons[i]), but_titles[i]);
		ewl_button_image_set(EWL_BUTTON(buttons[i]),
				ewl_icon_theme_icon_path_get(images[i], 0),
				NULL);
		ewl_callback_append(buttons[i], EWL_CALLBACK_CLICKED,
				funs[i], NULL);
		ewl_widget_show(buttons[i]);
	}

	return box;
}

static Ewl_Widget 
*_page_data(void)
{
	Ewl_Widget *box, *filelist;
	Ecdb_Burn_Project *proj;
	const char *dnd_types[] = {"text/uri-list", NULL};

	proj = ecdb_burn_project_new();
	
	box = ewl_vbox_new();
	ewl_widget_data_set(box, "proj_data", proj);
	ewl_callback_prepend(box, EWL_CALLBACK_DESTROY, 
			_destroy_data_page_cb, proj);
	ewl_widget_show(box);

	filelist = ewl_filelist_new();
	ewl_filelist_multiselect_set(EWL_FILELIST(filelist), TRUE);
	ewl_dnd_accepted_types_set(filelist, dnd_types);
	ewl_callback_append(filelist, EWL_CALLBACK_DND_DATA_RECEIVED,
			ecdb_filelist_dnd_dropped_cb, NULL);
	ewl_container_child_append(EWL_CONTAINER(box), filelist);
	ewl_widget_data_set(filelist, "source", proj->files);
	ewl_widget_show(filelist);

	return box;
}

static void
_data_disc_cb(Ewl_Widget *w, void *ev, void *data)
{
	printf("Create Data Disc\n");
	ecdb_create_data_tab();
}

static void
_burn_image_cb(Ewl_Widget *w, void *ev, void *data)
{
	printf("Burn Image to Disc\n");
}

static void
_copy_disc_cb(Ewl_Widget *w, void *ev, void *data)
{
	printf("Copy Disc\n");
}

static void
_audio_disc_cb(Ewl_Widget *w, void *ev, void *data)
{
	printf("Create Audio Disc\n");
}

