#######################################################################
# 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_summary.pm,v 1.3 1999/01/27 10:21:36 dn1 Exp $
# $Log: gull_summary.pm,v $
# Revision 1.3  1999/01/27 10:21:36  dn1
# *** empty log message ***
#

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

use strict;

my $entrytitle = 'Lightgoldenrod';
my $entrycolour = 'white';

package project_summary;

sub start {
    my ($type, $db, $mw, $project) = @_;
#    warn "Inside project_summary -> $type, $db, $mw, $project \n";
    my $window = MySummary->new($db, $mw, $project);
}

package MySummary;

sub new {
    my ($type, $db, $mw, $project) = @_;
    my $self = {};
    bless($self, $type);
    $self->{Original_mw} = $mw;
    $self->{Project_name} = $project;
    $self->{Database} = $db;
    $self->{this_hit} = 0;
    $self->{total_hits} = 0;
    $self->setwhere();

    # get the project-info # 
    my $dbh = WrapDBI->connect("$self->{Database}", {RaiseError => 1});
    my $data = summary->get_entries($dbh, $self->{Project_name});
    $dbh->disconnect();
    $self->{summary_data} = $data;

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

    my @count = keys(%{$self->{summary_data}});
#    warn "@count";
#    warn ($#count + 1);
    $self->{total_hits} = ($#count + 1);
    unless ($self->{total_hits} == 0){ 
	$self->{this_hit} = 1;
	$self->setwhere();
    }

    $self->makeWindow($mw);
    $self->makeFrames();
    $self->makeTitles();
    $self->makeMenu();
    $self->bottom_buttons();
    $self->makeEntries();

    unless ( ($self->{total_hits} == 0) || ($self->{total_hits} == 1)){ 
	$self->turn_up_and_down_on();
    }

    return $self;
}

sub makeWindow {
    my ($self, $mw) = @_;
    $self->{original_mw} = $mw;
    $self->{mainWindow} = $self->{original_mw}->Toplevel;
    $self->{mainWindow}->configure(cursor  => 'hand2');
    $self->{mainWindow}->wm('minsize', 500, 350); 
    $self->{mainWindow}->wm('maxsize', 500, 350);
    $self->{mainWindow}->packPropagate(0);
    $self->{mainWindow}->idletasks();
    $self->{mainWindow}->configure (-title => "Project summary information.");
}

sub makeFrames {
    my ($self) = @_;
    $self->{title_frame} = $self->{mainWindow}->Frame()->pack(-side => 'top');
    $self->{menu_frame} = $self->{mainWindow}->Frame()->pack(side => 'top', fill => 'x');
    $self->{main_frame} = $self->{mainWindow}->Frame(-relief => 'groove', -borderwidth => '6')->pack(-side => 'top', -expand => 'yes', -fill => 'both');

    # Need a bottom section for some buttons plus a next/prev clone section
    $self->{main_frame_top} = $self->{main_frame}->Frame()->pack(-side => 'top');
    $self->{main_frame_bottom} = $self->{main_frame}->Frame()->pack(-side => 'bottom');

    # Split the main screen into 2
    $self->{main_frame_top_left} = $self->{main_frame_top}->Frame()->pack(-side => 'left');
    $self->{main_frame_top_right} = $self->{main_frame_top}->Frame()->pack(-side => 'right');
}


sub makeTitles {
    my ($self) = @_;
    $self->{clone_label} = $self->{title_frame}->Label(font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
						      text => '  Project:')->pack(-side => 'left');
    $self->{clone_entry} = $self->{title_frame}->Entry(
					 -state => 'disabled',
					 -width => 15,
					 -font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
					 -bg => $entrycolour,
					 -textvariable => \$self->{Project_name},
					 )->pack(side => 'left');
#    $self->{data_type} = $self->{title_frame}->Label(font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
#						     text => "Project Summary")->pack(-side => 'right');
}


sub makeMenu {
    my ($self) = @_;
    $self->{menu} = $self->{menu_frame}->Menubutton(bg => 'grey77', text => 'File', underline => 0);

    # File
    $self->{fileMenu1} = $self->{menu_frame}->Menubutton(bg => 'grey77', text => 'File', underline => 0);
    $self->{fileMenu1}->configure(-menu => $self->{fileMenu1} ->Menu(tearoff => 0));
    $self->{fileMenu1}->command(-label => 'Quit', 
				underline => 0,
				command => sub{ 
				    $self->{mainWindow}->destroy();
				});
    $self->{fileMenu1}->pack(-side => 'left');
}

sub bottom_buttons {
    my ($self) = @_;
    $self->{up_button} = $self->{main_frame_bottom}->Button( 
							    -state => 'disabled',
							    -image => 'up', 
							    -command => sub { $self->goup();}
							    )->pack(-side => 'left');

    $self->{down_button} = $self->{main_frame_bottom}->Button( 
							      -state => 'disabled',
							      -image => 'down', 
							      -command => sub { $self->godown();}
							      )->pack(-side => 'left');

    $self->{which_entry} = $self->{main_frame_bottom}->Entry(
				      -bg => $entrycolour, 
				      -width => 10, 
				      -textvariable => \$self->{where}, 
				      -state => 'disabled'
				      )->pack(-side => 'left');

}
sub makeEntries {
    my ($self) = @_;
    my $a = 'Date';
    $self->{date} = $self->{main_frame_top_left}->Entry(
							-state => 'disabled',
							-width => 20,
							#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							-bg => $entrytitle,
							-textvariable => \$a,
							)->pack(side => 'top');
    
    $self->{date_value} = $self->{main_frame_top_right}->Entry(
							       -state => 'disabled',
							       -width => 20,
							       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							       #-bg => $entrytitle,
							       -textvariable => \$self->{summary_data}->{($self->{this_hit})}->{SUMMARYDATE},
							       )->pack(side => 'top');

    my $b = 'No. of contigs';
    $self->{number_contigs} = $self->{main_frame_top_left}->Entry(
								  -state => 'disabled',
								  -width => 20,
								  #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								  -bg => $entrytitle,
								  -textvariable => \$b,
								  )->pack(side => 'top');
    
    $self->{number_contigs_value} = $self->{main_frame_top_right}->Entry(
									 -state => 'disabled',
									 -width => 20,
									 #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
									 #-bg => $entrytitle,
									 -textvariable => \$self->{summary_data}->{($self->{this_hit})}->{NUM_CONTIGS},
									 )->pack(side => 'top');


    my $c = 'Total contig length (bp)';
    $self->{total_contig_length} = $self->{main_frame_top_left}->Entry(
								       -state => 'disabled',
								       -width => 20,
								       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								       -bg => $entrytitle,
								       -textvariable => \$c,
								       )->pack(side => 'top');
    
    $self->{total_contig_length_value} = $self->{main_frame_top_right}->Entry(
									      -state => 'disabled',
									      -width => 20,
									      #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
									      #-bg => $entrytitle,
									      -textvariable => \$self->{summary_data}->{($self->{this_hit})}->{TOTAL_CTG_LEN},
									      )->pack(side => 'top');

    my $d = 'No. of contigs > 1000bp';
    $self->{BIG_contigs} = $self->{main_frame_top_left}->Entry(
								-state => 'disabled',
								-width => 20,
								#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								-bg => $entrytitle,
								-textvariable => \$d,
								)->pack(side => 'top');
    
    $self->{BIG_contigs_value} = $self->{main_frame_top_right}->Entry(
								       -state => 'disabled',
								       -width => 20,
								       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								       #-bg => $entrytitle,
								       -textvariable => \$self->{summary_data}->{($self->{this_hit})}->{NUM_LGE_CONTIGS},
								       )->pack(side => 'top');


    
    my $dd = 'Large contig total (bp)';
    $self->{large_contig} = $self->{main_frame_top_left}->Entry(
								-state => 'disabled',
								-width => 20,
								#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								-bg => $entrytitle,
								-textvariable => \$dd,
								)->pack(side => 'top');
    
    $self->{large_contig_value} = $self->{main_frame_top_right}->Entry(
								       -state => 'disabled',
								       -width => 20,
								       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								       #-bg => $entrytitle,
								       -textvariable => \$self->{summary_data}->{($self->{this_hit})}->{LGE_CTG_LEN},
								       )->pack(side => 'top');


    my $e = 'No. of reads';
    $self->{number_reads} = $self->{main_frame_top_left}->Entry(
								-state => 'disabled',
								-width => 20,
								#-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								-bg => $entrytitle,
								-textvariable => \$e,
								)->pack(side => 'top');
    
    $self->{number_reads_value} = $self->{main_frame_top_right}->Entry(
								       -state => 'disabled',
								       -width => 20,
								       #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								       #-bg => $entrytitle,
								       -textvariable => \$self->{summary_data}->{($self->{this_hit})}->{NUM_READS},
								       )->pack(side => 'top');

    my $f = 'Coverage';
    $self->{coverage} = $self->{main_frame_top_left}->Entry(
							    -state => 'disabled',
							    -width => 20,
							    #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
							    -bg => $entrytitle,
							    -textvariable => \$f,
							    )->pack(side => 'top');
    
    $self->{coverage_value} = $self->{main_frame_top_right}->Entry(
								   -state => 'disabled',
								   -width => 20,
								   #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								   #-bg => $entrytitle,
								   -textvariable => \$self->{summary_data}->{($self->{this_hit})}->{COVERAGE},
								   )->pack(side => 'top');

    my $g = 'Finished length (bp)';
    $self->{finished_length} = $self->{main_frame_top_left}->Entry(
								   -state => 'disabled',
								   -width => 20,
								   #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
								   -bg => $entrytitle,
								   -textvariable => \$g,
								   )->pack(side => 'top');
    
    $self->{finished_length_value} = $self->{main_frame_top_right}->Entry(
									  -state => 'disabled',
									  -width => 20,
									  #-font => '*-times-bold-r-normal--*-180-*-*-p-*-*-*',
									  #-bg => $entrytitle,
									  -textvariable => \$self->{summary_data}->{($self->{this_hit})}->{FINISHED_LENGTH_BP},
									  )->pack(side => 'top');

    
}


### @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


sub turn_up_and_down_on {
    my ($self) = @_;
    $self->{up_button}->configure(-state=>'active');
    $self->{down_button}->configure(-state=>'active');
#    warn "%%%%%% $self->{total_hits} %%%%%%% \n";
}

sub setwhere {
    my ($self) = @_;
    $self->{where} = "$self->{this_hit}" . " of " . "$self->{total_hits}";
}

sub godown {
    my ($self) = @_;
    
#    warn "DEBUG !!! $self->{this_hit} vs. $self->{total_hits} \n";
    unless ($self->{this_hit} == 1){

#	warn "yup ! In here ! warn $self->{type}\n";
	
	
	$self->{this_hit}--;

	$self->{date_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{SUMMARYDATE});
	$self->{number_contigs_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{NUM_CONTIGS});
	$self->{total_contig_length_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{TOTAL_CTG_LEN});
	$self->{BIG_contigs_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{NUM_LGE_CONTIGS});
	$self->{large_contig_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{LGE_CTG_LEN});
	$self->{number_reads_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{NUM_READS});
	$self->{coverage_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{COVERAGE});
	$self->{finished_length_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{FINISHED_LENGTH_BP});

	$self->setwhere();

    }
}

sub goup {
    my ($self) = @_;
#    warn "DEBUG !!! $self->{this_hit} vs. $self->{total_hits} \n";
    unless ($self->{this_hit} == $self->{total_hits}){
	
#	warn "yup ! In here ! warn $self->{type}\n";
	$self->{this_hit}++;

	$self->{date_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{SUMMARYDATE});
	$self->{number_contigs_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{NUM_CONTIGS});
	$self->{total_contig_length_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{TOTAL_CTG_LEN});
	$self->{BIG_contigs_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{NUM_LGE_CONTIGS});
	$self->{large_contig_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{LGE_CTG_LEN});
	$self->{number_reads_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{NUM_READS});
	$self->{coverage_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{COVERAGE});
	$self->{finished_length_value}->configure (-textvariable => \$self->{summary_data}->{($self->{this_hit})}->{FINISHED_LENGTH_BP});

	$self->setwhere();


    }
}

1;

