#=====================================================================#
#                                                                     #
# This is the 'Edit_Window' class                                     #
#                                                                     #
#=====================================================================#
# $Id$

package Edit_Window;
use strict;
our $ADD;
use CSL::Gnome2;
use Gnome2;
use Gtk2::GladeXML;
use CSL::DBs;
use Carp;

our $GLADEPATH = '/s/lab/common/lib/library5';
sub new
{
    my $this = shift;
    my $class = ref($this) || $this;

    my $self = { };
    bless $self, $class;

# TODO Do I need to parse rc file here too?

    $self->{xml} = Gtk2::GladeXML->new("$GLADEPATH/library.glade",
                                       'edit_window');
    croak "Error opening Glade file" if (!$self->{xml});

    $self->{xml}->signal_autoconnect_from_package($self);

    return $self;
}

sub run
{
    my ($class, $isbn, $title, $author, $category, $copy, $main_window) = @_;

    Gnome2::Program->init("library", "5");

    my $self = $class->new;

    my $index;
    if ($category =~ /book/i) {
        $index = 0;
    } elsif ($category =~ /cd/i) {
        $index = 1;
    } elsif ($category =~ /laptop/i) {
        $index = 2;
    } else { # category is key
        $index = 3;
    }

    $self->wgt('old_isbn_entry')->set_text($isbn);
    $self->wgt('copy_entry')->set_text($copy);
    $self->wgt('author_entry')->set_text($author);
    $self->wgt('title_entry')->set_text($title);
    $self->wgt('category_combo')->set_active($index);
    $self->wgt('edit_window')->show;

    $self->{main_window} = $main_window;

    Glib::Timeout->add(500, \&do_scan, $self);
    Gtk2->main;

    $main_window->getbooks();
    $main_window->getcds();
    $main_window->getlaptops();
    $main_window->getkeys();
}

sub on_ok_button_clicked {
    my ($self) = @_;

    my $dbh = DBI->connect($CSL::DBs::csdb);
    $dbh->do("set search_path to library");
    my $old_isbn = $self->wgt('old_isbn_entry')->get_text();
    my $isbn = $self->wgt('isbn_entry')->get_text();
    my $author = $self->wgt('author_entry')->get_text();
    my $title = $self->wgt('title_entry')->get_text();
    my $category = $self->wgt('category_combo')->get_active_text();
    $category =~ tr/A-Z/a-z/;
    my $copy = $self->wgt('copy_entry')->get_text();

    if (!$isbn)
    {
        Alert_Window->run("Please enter an ISBN.");
        return;
    }

    $isbn =~ s/-//g;
    $isbn =~ tr/a-z/A-Z/;
    
    my $qstr = "update books set";
    my $flag = 0;

    unless ( $isbn =~ /$old_isbn/ ) {
	$qstr .= " isbn = ?, cpy = ?,";
	$flag = 1;
    }

    $qstr .= " author = ?, title = ?, cat = ? WHERE isbn = ?";

    my $query = $dbh->prepare( $qstr );

    if ( $flag ) {
	$query->execute( $isbn, $copy, $author, $title, $category, $old_isbn );
    }
    else {
	$query->execute( $author, $title, $category, $old_isbn );
    }


    $dbh->disconnect();

    $self->wgt('edit_window')->destroy;
    $self->{main_window}->getbooks();
    $self->{main_window}->getcds();
    $self->{main_window}->getlaptops();
    $self->{main_window}->getkeys();

} # End of sub do_edit

sub on_cancel_button_clicked {
    my ($self) = @_;

    $self->wgt('edit_window')->destroy();
} # End of sub do_cancel

sub do_scan {
    my ($self) = @_;

    my $isbn_entry = $self->wgt('isbn_entry');
    $isbn_entry->set_text($ADD) if ($ADD && $isbn_entry);

    1;
}

sub wgt
{
    my ($self, $widget) = @_;

    return $self->{xml}->get_widget($widget);
}

1;
