/*
 * $RCSfile: copy_scene.c,v $  $Revision: 1.2 $  $Name:  $
 * $Id: copy_scene.c,v 1.2 2006/03/07 06:42:04 bpaauwe Exp $
 * $Author: bpaauwe $
 * $Date: 2006/03/07 06:42:04 $
 * ----------------------------------------------------------------------------
 *
 *  Copyright (c) Bob Paauwe (2006)
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * ----------------------------------------------------------------------------
 *
 *  Description:
 *    Functions to handle the copy scene dialog box.
 * ----------------------------------------------------------------------------
 */

#include <string.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <glade/glade-build.h>

#ifdef WITH_GNOME
#  include <libgnomeui/libgnomeui.h>
#endif
#include "globals.h"
#include "copy_scene.h"
#include "scenes.h"


extern GtkWidget *scene_selector;
extern GtkWidget *sr_selector;

void initialize_copy_scene(GladeXML *xml)
{
	cs_dialog = glade_xml_get_widget(xml, "cs_dialog");
	cs_scene_selector = glade_xml_get_widget(xml, "cs_scene_selector");
	cs_controller = glade_xml_get_widget(xml, "cs_controller");
	cs_group = glade_xml_get_widget(xml, "cs_group");
	cs_new_name = glade_xml_get_widget(xml, "cs_new_name");
	cs_new_controller = glade_xml_get_widget(xml, "cs_new_controller");
	cs_new_group = glade_xml_get_widget(xml, "cs_new_group");
}



void on_copy_scene_activate(GtkWidget *widget, gpointer user_data)
{
	scene_t *scene;

	scene = scene_list;
	while(scene) {
		gtk_combo_box_append_text((GtkComboBox *)cs_scene_selector,
				scene->name);
		scene = scene->next;
	}
	gtk_widget_show(cs_dialog);
}

void on_cs_scene_selector_changed(GtkWidget *widget, gpointer user_data)
{
	scene_t *scene;
	int sel;
	char group_num[4];

	sel = gtk_combo_box_get_active((GtkComboBox *)cs_scene_selector);
	if (sel == -1) {
		printf("No scene selected!\n");
		return;
	}

	scene = find_scene(sel);
	if (scene) {
		if (scene->controller) {
			gtk_entry_set_text((GtkEntry *)cs_controller,
					scene->controller->device_name);
		}
		sprintf(group_num, "%d", scene->group);
		gtk_entry_set_text((GtkEntry *)cs_group, group_num);
	} else {
		printf("Invalid scene!\n");
	}
}

void on_cs_new_controller_changed(GtkWidget *widget, gpointer user_data)
{
	scene_t *scene;
	int sel;

	sel = gtk_combo_box_get_active((GtkComboBox *)cs_scene_selector);
	if (sel == -1) {
		printf("No scene selected!\n");
		return;
	}

	scene = find_scene(sel);
	if (scene) {
		switch (scene->controller->device_type) {
			case 0x0109: /* keypadlinc */
				gtk_spin_button_set_range((GtkSpinButton *)cs_new_group,
						1.0, 8.0);
				break;
			case 0x0004: /* controllinc */
				gtk_spin_button_set_range((GtkSpinButton *)cs_new_group,
						1.0, 5.0);
				break;
			case 0x0001:
			case 0x0000:
				gtk_spin_button_set_range((GtkSpinButton *)cs_new_group,
						1.0, 254.0);
				break;
			default:
				gtk_spin_button_set_range((GtkSpinButton *)cs_new_group,
						1.0, 1.0);
				break;
		}
	}
}


void on_cs_ok_clicked(GtkWidget *widget, gpointer user_data)
{
	int sel;
	scene_t *src_scene;
	scene_t *dst_scene;
	scene_t *tmp;
	device_list_t *resp;
	device_list_t *new_resp;
	device_list_t *resp_x = NULL;
	char *new_name;
	int dev;

	sel = gtk_combo_box_get_active((GtkComboBox *)cs_scene_selector);
	if (sel == -1) {
		printf("Scene Copy: No scene selected!\n");
	} else {
		dev = gtk_combo_box_get_active((GtkComboBox *)cs_new_controller);
		new_name = (char *)gtk_entry_get_text((GtkEntry *)cs_new_name);
		if ((dev == -1) || !new_name) {
			printf("Scene Copy: No destination scene!\n");
		} else {
			src_scene = find_scene(sel);
			dst_scene = (scene_t *)malloc(sizeof(scene_t));
			dst_scene->name = (char *)malloc(strlen(new_name) + 1);
			strcpy(dst_scene->name, new_name);
			dst_scene->group = (int)gtk_spin_button_get_value_as_int(
					(GtkSpinButton *)cs_new_group);
			dst_scene->controller = &device_array[dev];
			dst_scene->responders = src_scene->responders;
			dst_scene->next = NULL;
			resp = src_scene->responder_list;
			while (resp) {
				new_resp = (device_list_t *)malloc(sizeof(device_list_t));
				if (!new_resp) {
					printf("Memory allocation failure\n");
					break;
				}
				memcpy(new_resp, resp, sizeof(device_list_t));
				new_resp->next = NULL;
				if (!resp_x) {
					dst_scene->responder_list = new_resp;
				} else {
					resp_x->next = new_resp;
				}
				resp_x = new_resp;
				resp_x->next = NULL;
				resp = resp->next;
			}

			/* Add to end of scene list */
			tmp = scene_list;
			while(tmp && tmp->next) {
				tmp = tmp->next;
			}
			if (tmp) {
				tmp->next = dst_scene;
				dst_scene->index = tmp->index + 1;
				gtk_combo_box_append_text((GtkComboBox *)scene_selector,
						new_name);
				gtk_combo_box_append_text((GtkComboBox *)sr_selector, new_name);
			} else {
				printf("No scenes exist but one was copied??\n");
			}
		}
	}

	build_tables();
	gtk_widget_hide(cs_dialog);
}

void on_cs_cancel_clicked(GtkWidget *widget, gpointer user_data)
{
	gtk_widget_hide(cs_dialog);
}
