#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################

# $Id: gull_status.pm,v 1.8 1999/10/04 09:27:48 dn1 Exp $
# $Log: gull_status.pm,v $
# Revision 1.8  1999/10/04 09:27:48  dn1
# *** empty log message ***
#
# Revision 1.7  1999/03/22 18:16:49  dn1
# *** empty log message ***
#

# DN1
########################################
#
########################################

use strict;

package status;

sub start {
    my ($type, $mw, $this_hit, $data, $db, $code, $clone_or_project, $write) = @_;
    my $window = MyStatus->new($mw, $data, $this_hit, $db, $code, $clone_or_project, $write);
}

package MyStatus;

sub new {
    my ($type, $mw, $data, $this_hit, $db, $code, $clone_or_project, $write, $lock_code, $d) = @_;

#    warn "@@@@@@@@@@@@@@@@@@@@@ debug @@@@@@@@@@@@@@@@@@@@@\n";
#    warn $this_hit;
#    warn "@@@@@@@@@@@@@@@@@@@@@ debug @@@@@@@@@@@@@@@@@@@@@\n";

    my $self = {};
    bless($self, $type);
    $self->{hit} = $this_hit;
    $self->{db} = $db;
    $self->{code} = $code;
    $self->{type} = $clone_or_project;
    $self->{revert} = $write;
    $self->{main} = $mw;
    $self->makeWindow($mw);
    $self->makeFrames();
    $self->makeHeader();
    $self->makeButtons();
    $self->makeText($data);
    return $self;
}

sub makeFrames {
    my ($self) = @_;
    $self->{top_frame} = $self->{mainWindow}->Frame()->pack(side => 'top', fill => 'x');
    $self->{middle_frame} = $self->{mainWindow}->Frame(-relief => 'groove', -borderwidth => '3')->pack(side => 'top', fill => 'x');
    $self->{bottom_frame} = $self->{mainWindow}->Frame()->pack(side => 'top', fill => 'x');
}

sub makeWindow {
    my ($self, $mw) = @_;
    $self->{original_mw} = $mw;
    $self->{mainWindow} = $self->{original_mw}->Toplevel;
    $self->{mainWindow}->configure(cursor  => 'hand2');

    
    my $f;
    if ($self->{type} eq 'Clone'){
	$self->{mainWindow}->wm('minsize', 500, 452); 
	$self->{mainWindow}->wm('maxsize', 500, 452);
	$f = $self->{mainWindow}->Frame(-height => 452, -width => 500);
	$f->pack();
    } 
    if ($self->{type} eq 'Project'){
	$self->{mainWindow}->wm('minsize', 700, 452); 
	$self->{mainWindow}->wm('maxsize', 700, 452);
	$f = $self->{mainWindow}->Frame(-height => 452, -width => 700);
	$f->pack();
    } 
    $self->{mainWindow}->idletasks();
    $self->{mainWindow}->packPropagate(0);
    $f->packForget();

    $self->{mainWindow}->idletasks();


    $self->{mainWindow}->configure (-title => "Status history.");
}

sub makeHeader {
    my ($self) = @_;
    $self->{heading} = $self->{top_frame}->Label(-text => "$self->{type} $self->{hit}", -background => 'white');
    $self->{heading}->pack(side => 'top', fill => 'x');
}

sub makeButtons {
    my ($self) = @_;
    $self->{quit} = $self->{bottom_frame}->Button( -text => 'Close', -width => '5',
						      -command => sub {
							  $self->{mainWindow}->destroy();
						      });
    $self->{quit}->pack(-side => 'left', -expand => 'no', -fill => 'x');

    if ($self->{revert} eq 'yes'){
	$self->{print} = $self->{bottom_frame}->Button( -text => 'Revert current status', -width => '18', 
							-state => 'disabled',
						       -command => sub {
							   my $dbh = WrapDBI->connect("$self->{db}", {RaiseError => 1});
							   ########
							   # Clone
							   ########
							   if ($self->{type} eq 'Clone'){

							       ##################################################
							       # First.. try and grap write-access for the clone
							       ##################################################
							       my $lock = clone_history->lock_data($self->{hit}, $dbh);
#							       warn "lock -> $lock \n";
							       ##########################################################################
							       # If $lock is not zero then there was a problem locking the table entries
							       ##########################################################################
							       if ($lock > 0){
								 main::popup ($self->{main}, "WARN", ["Sorry ! 1 or more entries for this clone are already locked. Please try again later.\n"]);
								   $dbh->disconnect();
								   return();
							       }

							       # What will we revert to >?<
							       my $new_status = status->next_best_clone_status($self->{hit}, $dbh);
							       # Are you sure >?<
							       if ($$new_status ne ''){
								   my $DIALOG1;
								   my($do, $dont) = ('Do it', 'Cancel');
								   $DIALOG1 = $self->{mainWindow}->Dialog(
													  -title          => 'Warn',
													  -text           => '',
													  -default_button => $dont,
													  -buttons        => [$do, $dont],
													  );
								   $DIALOG1->configure(
										       -wraplength => '4i',
										       -text       => "This will revert the current $self->{type} status for $self->{hit} to $$new_status !",
										       );
								   my $button = $DIALOG1->Show;
								   if ($button eq $do) {
								       status->revert_clone_current($self->{hit}, $dbh);
								   } elsif ($button eq $dont) {
								       $dbh->disconnect();
								       return();
								   }
							   
								   if ($DIALOG1 ne ''){
								       $DIALOG1->update('idletasks');
								   }
							       } else {
								   $dbh->disconnect();
								   return();
							       } 
							   }

							   ##########
							   # Project
							   ##########
							   if ($self->{type} eq 'Project'){
							       ##################################################
							       # First.. try and grap write-access for the clone
							       ##################################################
							       my $lock = project_history->lock_data($self->{hit}, $dbh);
#							       warn "lock -> $lock \n";
							       ##########################################################################
							       # If $lock is not zero then there was a problem locking the table entries
							       ##########################################################################
							       if ($lock > 0){
								 main::popup ($self->{main}, "WARN", ["Sorry ! 1 or more entries for this project are already locked. Please try again later.\n"]);
								   $dbh->disconnect();
								   return();
							       }


							       # What will we revert to >?<
							       my $new_status = status->next_best_project_status($self->{hit}, $dbh);
							       
							       # Are you sure >?<
							       if ($$new_status ne ''){
								   my $DIALOG1;
								   my($do, $dont) = ('Do it', 'Cancel');
								   $DIALOG1 = $self->{mainWindow}->Dialog(
													  -title          => 'Warn',
													  -text           => '',
													  -default_button => $dont,
													  -buttons        => [$do, $dont],
													  );
								   $DIALOG1->configure(
										       -wraplength => '4i',
										       -text       => "This will revert the current $self->{type} status for $self->{hit} to $$new_status !",
										       );
								   my $button = $DIALOG1->Show;
								   if ($button eq $do) {
								       status->revert_project_current($self->{hit}, $dbh);
								   } elsif ($button eq $dont) {
								       $dbh->disconnect();
								       return();
								   }
							   
								   if ($DIALOG1 ne ''){
								       $DIALOG1->update('idletasks');
								   }   
							       } else {
								   $dbh->disconnect();
								   return();
							       }
							   }
							   
							   $dbh->disconnect();
							   &{$self->{code}};
							   $self->{mainWindow}->destroy();
						       });
	$self->{print}->pack(-side => 'left', -expand => 'no', -fill => 'x');
						   }
    }

sub makeText {
    my ($self, $data) = @_;
    $self->{legend_text} = $self->{middle_frame}->Text(-wrap => 'none',
						       -state => 'normal',
						       -height => 25,
						       -background => 'white');
    $self->{legend_scroll} = $self->{middle_frame}->Scrollbar(-command => [$self->{legend_text} => 'yview']);
    $self->{legend_text}->configure(-yscrollcommand => [$self->{legend_scroll} => 'set']);
    $self->{legend_scroll}->pack(-side => 'right', -fill => 'y');

    $self->{legend_text}->pack(-side => 'left', -expand => 1, -fill => 'both');

#    my $capture = Data::Dumper::Dumper($data);
#    warn $capture;

    my $next;
    my $count = -1;
    foreach $next (@$data){
	$count++;
	next if ($count == 0);

	if (exists $$next{REMARK}){
	    $self->{legend_text}->insert('end', "$$next{STATUSDATE} ", "date$$next{STATUSDATE}", "$$next{OPERATOR} ", "who$$next{OPERATOR}", "     ", $$next{COLOUR}, "$$next{DESCRIPTION}", "description$count$$next{DESCRIPTION}", "     $$next{REMARK}\n", "remark$count$$next{REMARK}");

	} else {
	    $self->{legend_text}->insert('end', "$$next{STATUSDATE} ", "date$$next{STATUSDATE}", "$$next{OPERATOR} ", "who$$next{OPERATOR}", "     ", $$next{COLOUR}, "$$next{DESCRIPTION}\n", "description$count$$next{DESCRIPTION}");
	}

	$self->{legend_text}->tagConfigure($$next{COLOUR}, -background => $$next{COLOUR}, -foreground => 'white', -font => '*times*-r*20*');

	if ($$next{CURRENT} == 0){
	    $self->{legend_text}->tagConfigure("description$count$$next{DESCRIPTION}",
					       -foreground => 'black', -font => '*-times-medium-r-normal--*-140-*-*-p-*-*-*');
	} else {
	    $self->{legend_text}->tagConfigure("description$count$$next{DESCRIPTION}", -background => 'yellow',
					       -foreground => 'black', -font => '*-times-medium-r-normal--*-140-*-*-p-*-*-*');

	}
    }

    $self->{legend_text}->configure(-state => 'disabled');

}


1;

