######################################################################
#
# Admin_Window.pm
# $Id$
#
######################################################################

package Admin_Window;

use strict;
use CSL::Gnome2;
use Gnome2;
use Gtk2::GladeXML;
use Crashbackend;
use Log_Window;
use Date::Format;

use fields qw(row xml);

sub new
{
    my Admin_Window $self = shift;
    $self = fields::new($self) unless ref($self);

    my $GLADEDIR = $main::GLADEDIR || ".";
    $self->{xml} = Gtk2::GladeXML->new("$GLADEDIR/crashman3.glade",
                                       'admin_window') 
                                   or die "Error opening glade file";
    $self->{xml}->signal_autoconnect_from_package($self);

    return $self;
}

sub run
{
    my ($class, $row) = @_;

    Gnome2::Program->init("crashman", "3");

    my $self = $class->new;

    $self->wgt('disk_entry')->set_text($row->{disk_name});
    $self->wgt('disk_status_combo')->child->set_text($row->{disk_status});
    $self->wgt('user_status_combo')->child->set_text($row->{user_status});
    $self->wgt('key_status_combo')->child->set_text($row->{key_status});
    $self->wgt('user_entry')->set_text($row->{user_name});
    $self->wgt('due_date_entry')->set_text($row->{due_date});
    my $textbuf = $self->wgt('comment_text')->get_buffer;
    my ($start, $end) = $textbuf->get_bounds;
    $textbuf->delete($start, $end);
    $textbuf->insert($start, $row->{comment});

    $self->{row} = $row;

    Gtk2->main;
}


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

    my $disk_name     = $self->wgt('disk_entry')->get_text;
    my $disk_status   = $self->wgt('disk_status_combo')->child->get_text();
    my $user_status   = $self->wgt('user_status_combo')->child->get_text();
    my $key_status    = $self->wgt('key_status_combo')->child->get_text();
    my $user_name     = $self->wgt('user_entry')->get_text;
    my $due_date_text = $self->wgt('due_date_entry')->get_text;
    my $textbuf       = $self->wgt('comment_text')->get_buffer;
    my $comment       = $textbuf->get_text($textbuf->get_bounds, 1);

    my $row = $self->{row};
    # If no changes have been made, just quit.
    if ( ($disk_status   eq $row->{disk_status}) &&
         ($user_status   eq $row->{user_status}) &&
         ($key_status    eq $row->{key_status})  &&
         ($user_name     eq $row->{user_name})   &&
         ($due_date_text eq $row->{due_date})    &&
         ($comment       eq $row->{comment}) )
    {
        $self->wgt('admin_window')->destroy;
        Gtk2->main_quit;
        return;
    }

    $due_date_text =~ s/\s*(\S+)\s*/$1/; # Remove leading and trailing spaces
    my $due_date = Crashbackend::parse_due_date($due_date_text);
    # Check due date if non-empty
    if ($due_date_text) {
        if (!$due_date)
        {
            my $box = Gtk2::MessageDialog->new(
                        undef, 'destroy-with-parent', 'error', 'ok',
                        "$due_date_text is not a valid date.\n" .
                        "Please enter a valid date (MM/DD/YYYY)");
            $box->run;
            $box->destroy;
            
            return;
        }

        if (Crashbackend::late_due_date($due_date_text))
        {
            my $box = Gtk2::MessageDialog->new(
                        undef, 'destroy-with-parent', 'warning', 'yes-no',
                        "The date $due_date_text is later than the current ".
                        "limit of ".Crashbackend::get_end_semester_date().".\n".
                        "Are you sure you want to check this disk out to ".
                        "this date?\n");
            my $r = $box->run;
            $box->destroy;

            if ($r eq 'no')
            {
                return;
            }
        }
    }

    my $new_due_date;
    if ($due_date_text ne "") {
        $new_due_date = time2str("%D", $due_date);
    } else {
        $new_due_date = "";
    }
    my @changes = ();
    push @changes, "new disk_status $disk_status" if ($disk_status ne
                                                           $row->{disk_status});
    push @changes, "new user_status $user_status" if ($user_status ne
                                                           $row->{user_status});
    push @changes, "new key_status $key_status"   if ($key_status ne
                                                           $row->{key_status});
    push @changes, "new user_name $user_name"     if ($user_name ne
                                                           $row->{user_name});
    push @changes, "new comment $comment"         if ($comment ne
                                                           $row->{comment});
    if ($due_date_text ne $row->{due_date})
    {
        if ($due_date_text ne "") {
            push @changes, "new due_date $new_due_date";
        } else {
            push @changes, "due_date removed";
        }
    }
    
    # Get verification
    my $box = Gtk2::MessageDialog->new(
          undef, 'destroy-with-parent', 'question', 'ok-cancel',
          "Are you sure you would like to make the following changes?\n"
          . join("\n", @changes));
    my $r = $box->run;
    $box->destroy;

    if ($r eq 'cancel') {
        return;
    }

    # Do the changes
    Crashbackend::db_disk_set($disk_name, $disk_status, $user_status,
                              $key_status, $user_name, $new_due_date,
                              $comment);
    # Log stuff
    my $log_comment = "Disk manually modified: " . join("; ", @changes);
    Crashbackend::log_action($disk_name, $user_name, $log_comment);

    $self->wgt('admin_window')->destroy;
    Gtk2->main_quit;
}

sub on_cancel_button_clicked
{
    my $self = $_[0];
    $self->wgt('admin_window')->destroy;
    Gtk2->main_quit;
}

sub on_admin_window_delete_event
{
    $_[0]->on_cancel_button_clicked;
}

sub on_remove_crash_disk_button_clicked
{
    my ($self, $widget, $event) = @_;
    
    my $box = Gtk2::MessageDialog->new(
                undef, 'modal', 'question', 'yes-no',
                "Are you sure you want to delete the disk ".
                $self->wgt('disk_entry')->get_text.
                "?");
    my $ret = $box->run;
    $box->destroy;
    if ($ret ne 'yes') {
        return;
    }

    $box = Gtk2::MessageDialog->new(
             undef, 'modal', 'question', 'yes-no',
             "Are you ABSOLUTELY SURE you want to delete the disk ".
             $self->wgt('disk_entry')->get_text.
             "?");
    my $ret = $box->run;
    $box->destroy;
    if ($ret ne 'yes') {
        return;
    }
    
    Crashbackend::delete_disk($self->wgt('disk_entry')->get_text);

    $box = Gtk2::MessageDialog->new(
             undef, 'modal', 'info', 'ok',
             "Disk ".$self->wgt('disk_entry')->get_text.
             " has been deleted from the pool.");
    $box->run;
    $box->destroy;

    $self->wgt('admin_window')->destroy;
    Gtk2->main_quit;
}

sub on_view_disk_log_button_clicked
{
    my ($self, $widget, $event) = @_;
    
    Log_Window->run($self->wgt('disk_entry')->get_text);
}


sub wgt { return $_[0]->{xml}->get_widget($_[1]); }

1;

