/*
 * $RCSfile: scene_verify.c,v $  $Revision: 1.1 $  $Name:  $
 * $Id: scene_verify.c,v 1.1 2006/04/14 17:04:23 bpaauwe Exp $
 * $Author: bpaauwe $
 * $Date: 2006/04/14 17:04:23 $
 * ----------------------------------------------------------------------------
 *
 *  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:
 *    Impliments a dialog box with a generic progress bar.
 * ----------------------------------------------------------------------------
 */
#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 "scenes.h"
#include "scene_verify.h"

void initialize_scene_verify(GladeXML *xml)
{
	sv_dialog = glade_xml_get_widget(xml, "sv_dialog");
	sv_status = glade_xml_get_widget(xml, "sv_status");
	sv_progress = glade_xml_get_widget(xml, "sv_progress");
	sv_device_name = glade_xml_get_widget(xml, "sv_device_name");
	gtk_widget_hide(sv_dialog);
}

void on_verify_scenes_activate(GtkWidget *widget, gpointer user_data)
{
	int i;
	char msg[100];
	int l;
	int d;
	unsigned char internal_ok[MAX_LINKS];
	unsigned char external_ok[MAX_LINKS];
	int fix;
	GtkTextBuffer *status;
	GtkTextIter iter;
	gdouble progress;
	gdouble inc;

	gtk_progress_bar_set_fraction((GtkProgressBar *)sv_progress, progress);
	gtk_widget_show(sv_dialog);
	while (gtk_events_pending()) {
		gtk_main_iteration();
	}

	status = gtk_text_view_get_buffer((GtkTextView *)sv_status);
	gtk_text_buffer_get_start_iter(status, &iter);

	for (i = 0; i <= last_device; i++) {
		if ((device_array[i].device_type == 0x0000) ||
				(device_array[i].device_type == 0x0001)) {
			printf("Skipping PowerLinc Controller.\n");
		} else {
			progress = 0.0;
			inc = 1.0 / (device_array[i].num_of_entries + 10);
			gtk_progress_bar_set_fraction((GtkProgressBar *)sv_progress,
					progress);
			sprintf(msg, "Verifying %s\n", device_array[i].device_name);
			gtk_label_set_text((GtkLabel *)sv_device_name, msg);
			while (gtk_events_pending()) {
				gtk_main_iteration();
			}
			l = 0;
			memset(internal_ok, 0, MAX_LINKS);
			memset(external_ok, 0, MAX_LINKS);
			gtk_progress_bar_set_text((GtkProgressBar *)sv_progress,
					"Reading Links...");
			while (gtk_events_pending()) {
				gtk_main_iteration();
			}
			while (get_device_link(iplc, device_array[i].device_id, l,
						&(device_array[i].current_lt[l])) > 0) {
				progress += inc;
				gtk_progress_bar_set_fraction((GtkProgressBar *)sv_progress,
						progress);
				while (gtk_events_pending()) {
					gtk_main_iteration();
				}
				l++;
			}
			for (; l < MAX_LINKS; l++) {
				device_array[i].current_lt[l].flags = 0;
			}

			gtk_progress_bar_set_text((GtkProgressBar *)sv_progress,
					"Comparing Links...");
			while (gtk_events_pending()) {
				gtk_main_iteration();
			}

			/* Now compare with the saved data */
			for (l = 0; l < MAX_LINKS; l++) {
				for (d = 0; d < MAX_LINKS; d++) {
					if (memcmp(&(device_array[i].current_lt[l]),
								&(device_array[i].db_records[d]),
								sizeof(link_record_t)) == 0) {
						internal_ok[d] = 1;
					}
					if (memcmp(&(device_array[i].current_lt[d]),
								&(device_array[i].db_records[l]),
								sizeof(link_record_t)) == 0) {
						external_ok[d] = 1;
					}
				}
			}

			fix = 0;
			/* List the problem links */
			for (l = 0; l < MAX_LINKS; l++) {
				if (internal_ok[l] == 0) {
					fix = 1;
					sprintf(msg,
							"%s: 0x%02x %d %02x.%02x.%02x Not in link table\n",
							device_array[i].device_name,
							device_array[i].db_records[l].flags,
							device_array[i].db_records[l].group_number,
							device_array[i].db_records[l].device_id[0],
							device_array[i].db_records[l].device_id[1],
							device_array[i].db_records[l].device_id[2]);
					gtk_text_buffer_insert(status, &iter, msg, -1);
				}
			}

			for (l = 0; l < MAX_LINKS; l++) {
				if (external_ok[l] == 0) {
					fix = 1;
					sprintf(msg,
							"%s: 0x%02x %d %02x.%02x.%02x Not in text file\n",
							device_array[i].device_name,
							device_array[i].current_lt[l].flags,
							device_array[i].current_lt[l].group_number,
							device_array[i].current_lt[l].device_id[0],
							device_array[i].current_lt[l].device_id[1],
							device_array[i].current_lt[l].device_id[2]);
					gtk_text_buffer_insert(status, &iter, msg, -1);
				}
			}

			gtk_text_buffer_insert(status, &iter, "Fix ?\n", -1);
			while (gtk_events_pending()) {
				gtk_main_iteration();
			}

			/* Fix it? */
#if 0
			if (fix) {
				for (l = 0; l < 417; l++) {
					clear_device_link(iplc, device_array[i].device_id, l);
				}
				program_device(iplc, &device_array[i]);
			}
#endif
		}
	}
}


void on_sv_cancel_clicked(GtkWidget *widget, gpointer user_data)
{
	/* Should this stop the verification? */
	gtk_widget_hide(sv_dialog);
	return;
}

void on_sv_dialog_delete_event(GtkWidget *widget, gpointer user_data)
{
	/* Should this stop the verification? */
	gtk_widget_hide(sv_dialog);
	return;
}

