/*
 * gui_opt_callbacks.c
 * 
 * Copyright (C) 2007  Josef Kufner <jk@myserver.cz>
 * 
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 *
 */

#include <stdio.h>
#include <assert.h>

#include "gui.h"
#include "gui_opt_callbacks.h"
#include "options.h"
#include "status.h"
#include "session.h"
#include "connection.h"
#include "record.h"
#include "backend.h"

#define DEBUG 5
#include "debug.h"

extern BACKEND *backend_network;
extern BACKEND *backend_game;

struct s_goc_playerlist_column_wid goc_playerlist_column_wid[GOC_PLAYERLIST_COLUMN_WID_SIZE] = {};


static void goc_label(int value_id, void *data)
{
	DBG_MSG(2, "");
	assert(data != NULL);
	gtk_label_set_text(GTK_LABEL((GtkWidget *)data), option_get_string_by_id(value_id));
	DBG_MSG(3, "done");
}


/*
static void goc_menuitem_check(int value_id, void *data)
{
	DBG_MSG(2, "");
	assert(data != NULL);
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM((GtkWidget *)data),
			option_get_integer_by_id(value_id));
	DBG_MSG(3, "done");
}
*/


static void goc_widget_visibility(int value_id, void *data)
{
	int val;

	assert(data != NULL);

	val = option_get_integer_by_id(value_id);
	if (val) {
		gtk_widget_show((GtkWidget*) data);
	} else {
		gtk_widget_hide((GtkWidget*) data);
	}
}


static void goc_playerlist_column(int value_id, void *data)
{
	int val;

	DBG_MSG(2, "%d", (int)data);
	assert((int) data < GOC_PLAYERLIST_COLUMN_WID_SIZE);

	if (goc_playerlist_column_wid[(int)data].column == NULL || goc_playerlist_column_wid[(int)data].menuitem == NULL)
		return;

	val = option_get_integer_by_id(value_id);

	/* visibility of player list columns */
	gtk_tree_view_column_set_visible(GTK_TREE_VIEW_COLUMN(goc_playerlist_column_wid[(int)data].column), val);

	/* check/uncheck menu item */
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(goc_playerlist_column_wid[(int)data].menuitem), val);
	DBG_MSG(3, "done");
}


static void goc_show_logs(int value_id, void *data)
{
	int val;

	DBG_MSG(2, "");
	val = option_get_integer_by_id(value_id);

	/* show/hide tabs */
	if (val) {
		if (!gui_page[GUI_PAGE_GAMELOG].window)
			gtk_widget_show(lookup_widget(main_window, "gamelog_vbox"));
		if (!gui_page[GUI_PAGE_NETLOG].window)
			gtk_widget_show(lookup_widget(main_window, "netlog_vbox"));
	} else {
		gtk_widget_hide(lookup_widget(main_window, "gamelog_vbox"));
		gtk_widget_hide(lookup_widget(main_window, "netlog_vbox"));
	}
	DBG_MSG(3, "done");
}

static void goc_show_record_tab(int value_id, void *data)
{
	int val;

	DBG_MSG(2, "");
	val = option_get_integer_by_id(value_id);

	if (val && !gui_page[GUI_PAGE_RECORD].window)
		gtk_widget_show(lookup_widget(main_window, "record_vbox"));
	else
		gtk_widget_hide(lookup_widget(main_window, "record_vbox"));
	DBG_MSG(3, "done");
}

static void goc_show_status_tab(int value_id, void *data)
{
	int val;

	DBG_MSG(2, "");
	val = option_get_integer_by_id(value_id);

	/* status */
	if (val && !gui_page[GUI_PAGE_STATUS].window) {
		gtk_widget_show(lookup_widget(main_window, "status_vbox"));
		gui_update_status_tab();
	} else {
		gtk_widget_hide(lookup_widget(main_window, "status_vbox"));
	}
	if (val || gui_page[GUI_PAGE_STATUS].window)
		gui_update_status_tab();
	DBG_MSG(3, "done");
}


static void goc_game_select_button(int value_id, void *data)
{
	const char *val;
	
	DBG_MSG(2, "");
	val = option_get_string_by_id(value_id);

	if (val && val[0]) {
		gtk_label_set_text(GTK_LABEL(lookup_widget(main_window, "game_name_button_label")), val);
	} else {
		gtk_label_set_text(GTK_LABEL(lookup_widget(main_window, "game_name_button_label")), _("-- vyberte hru --"));
	}

	DBG_MSG(3, "done");
}


static void goc_game_role(int value_id, void *data)
{
	const char *val;
	
	DBG_MSG(2, "");
	val = option_get_string_by_id(value_id);

	/* TODO
	 *  - update role combobox -- id required
	 */

	DBG_MSG(3, "done");
}


static void goc_connection_profile(int value_id, void *data)
{
	const char *prof, *prof_name;
	
	DBG_MSG(2, "");

	prof = option_get_string_by_id(value_id);
	if (prof)
		prof_name = option_get_string("network", prof, "name");
	else 
		prof_name = NULL;
	
	gtk_label_set_text(GTK_LABEL((GtkWidget *) data), prof_name ? prof_name : prof);
	DBG_MSG(3, "done");
}


static void goc_disconnected(int value_id, void *data)
{
	DBG_MSG(2, "");
	// val = option_get_integer_by_id(value_id);
	gui_update_playerlist();
	DBG_MSG(3, "done");
}


static void goc_enable_record(int value_id, void *data)
{
	record_enable(option_get_integer_by_id(value_id));
}

static void goc_enable_comm_log(int value_id, void *data)
{
	int val;
	val = option_get_integer_by_id(value_id);
	
	if (backend_game)
		backend_log_enable(backend_game, val);

	if (backend_network)
		backend_log_enable(backend_network, val);
}


void gui_opt_callbacks_register_main_window(void)
{
	struct {
		char *cat, *mod, *opt;
		OPTION_ITEM_UPDATE_CALLBACK cb;
		int arg;
		char *widget_name;
	} update_widget[] = {
		{ "main", "ui",    "pl_col_connected",	goc_playerlist_column,	0, NULL },
		{ "main", "ui",    "pl_col_state",	goc_playerlist_column,	1, NULL },
		{ "main", "ui",    "pl_col_nick",	goc_playerlist_column,	2, NULL },
		{ "main", "ui",    "pl_col_address",	goc_playerlist_column,	3, NULL },
		{ "main", "ui",    "pl_col_role",	goc_playerlist_column,	4, NULL },
		{ "main", "ui",    "pl_col_score",	goc_playerlist_column,	5, NULL },
		{ "main", "ui",    "pl_col_description",goc_playerlist_column,	6, NULL },
		{ "main", "ui",    "pl_col_connection",	goc_playerlist_column,	7, NULL },
		{ "main", "game",  "backend",		goc_game_select_button,	0, "game_name_button_label" },
		{ "main", "game",  "my_role",		goc_game_role,		0, "game_role_combobox" },
		{ "main", "net",   "profile",		goc_connection_profile,	0, "connection_backend_value_label" },
		{ "main", "net",   "address",		goc_label,		0, "connection_address_value_label" },
		{ "main", "debug", "show_logs",		goc_show_logs,		0, NULL },
		{ "main", "debug", "show_record_tab",	goc_show_record_tab,	0, NULL },
		{ "main", "debug", "show_status_tab",	goc_show_status_tab,	0, NULL },
		{ "main", "debug", "record_enabled",	goc_enable_record,	0, NULL },
		{ "main", "debug", "enable_comm_log",	goc_enable_comm_log,	0, NULL },
		{ "main", "ui",    "show_disconnected",	goc_disconnected,	0, NULL },
		{ "main", "ui",    "show_debug_menu",	goc_widget_visibility,	0, "mnu_debug" },
		{ NULL, NULL, NULL, NULL, 0, NULL }
	};
	int i;
	
	DBG_MSG(1, "");

	for (i = 0; update_widget[i].cb; i++) {
		option_set_item_update_callback(update_widget[i].cat, update_widget[i].mod, update_widget[i].opt,
				update_widget[i].cb, 
				update_widget[i].widget_name ? lookup_widget(main_window, update_widget[i].widget_name) : (void *)(update_widget[i].arg));
	}
	
	DBG_MSG(1, "done");
}

