package Platemaker;

use strict;
use vars qw(@ISA @EXPORT);
use Exporter;
@ISA = ('Exporter');
@EXPORT = qw(&create_a_plate);

use NewBrowseEntry;

# Fonts
my ($title_f, $entry_f, $button_f, $label_f);

# Other
my $dbh;
my $plate_win;
my $titlecolour = 'honeydew2';
my ($plate_var, $start_var, $end_var);
my ($rowcol2id_96, $id2rowcol_96, $return_code);

sub create_a_plate {
    
    my ($mainWindow);
    ($dbh, $mainWindow, $rowcol2id_96, $id2rowcol_96, $return_code, $title_f, $entry_f, $button_f, $label_f) = @_;
    if (! defined $plate_win){


	($plate_var, $start_var, $end_var) = '';
	
	# Window
	$plate_win = $mainWindow->Toplevel;
	$plate_win->wm('minsize', 500, 160); 
	$plate_win->wm('maxsize', 500, 160);
        my $f = $plate_win->Frame(-height => 160, -width => 500);
        $f->pack();
        $plate_win->idletasks();
	$plate_win->packPropagate(0);
        $f->packForget();
	$plate_win->configure (
			       #font=> $button_f, # Fix moving from 5.004 to 5.8.0
			       -title => "Plate entry tool.");

	
	# Disable the close option
	$plate_win->protocol('WM_DELETE_WINDOW' => sub { });

	# Frame
	my $top_frame = $plate_win->Frame()->pack(side => 'top', fill => 'both');
	my $middle_frame = $plate_win->Frame(-relief => 'raised', -borderwidth => '5' )->pack(side => 'left');
	my $middle_frame_1 = $middle_frame->Frame()->pack(side => 'top');
	my $middle_frame_1a = $middle_frame->Frame()->pack(side => 'top');
	my $middle_frame_2 = $middle_frame->Frame()->pack(side => 'top');
	my $middle_frame_3 = $middle_frame->Frame()->pack(side => 'top');
	my $middle_frame_4 = $middle_frame->Frame()->pack(side => 'top');
	my $middle_frame_5 = $middle_frame->Frame()->pack(side => 'top');
	my $left_frame = $plate_win->Frame()->pack(side => 'right');
	my $label = $top_frame->Label(
				      -font => $title_f, 
				      -text => 'Please enter your plate information.')->pack(-side => 'top');

	
	my %titles = (
		      "plate" => "Platename",
		      "format" => "Format",
		      "start" => "Start",
		      "end" => "End", 
		      );

	my $plate = $middle_frame_1->Entry(
					   -font => $button_f,
					   -state => 'disabled',
					   -width => 12,
					   -bg => $titlecolour,
					   -textvariable => \$titles{plate},
					   )->pack(side => 'left');
	
	my $format = $middle_frame_1a->Entry(
					     -font => $button_f,
					     -state => 'disabled',
					     -width => 12,
					     -bg => $titlecolour,
					     -textvariable => \$titles{format},
					     )->pack(side => 'left');

	my $start = $middle_frame_3->Entry(-font => $button_f,
					   -state => 'disabled',
					   -width => 12,
					   -bg => $titlecolour,
					   -textvariable => \$titles{start},
					   )->pack(side => 'left');
	
	my $end = $middle_frame_4->Entry(-font => $button_f,
					 -state => 'disabled',
					 -width => 12,
					 -bg => $titlecolour,
					 -textvariable => \$titles{end},
					 )->pack(side => 'left');
	

	my $plate_e = $middle_frame_1->Entry(-font => $entry_f,
					     -state => 'normal',
					     -width => 25,
					     -textvariable => \$plate_var,
					     )->pack(-side => 'left' );

	my $format_var = 96;
	my $format_e = $middle_frame_1a->Entry(-font => $entry_f,
					      -state => 'disabled',
					      -width => 25,
					      -textvariable => \$format_var,
					      )->pack(-side => 'left' );

	my $start_e = $middle_frame_3->Entry(-font => $entry_f,
					     -state => 'normal',
					     -width => 25,
					     -textvariable => \$start_var,
					     )->pack(side => 'left');
	
	my $end_e = $middle_frame_4->Entry(-font => $entry_f,
					   -state => 'normal',
					   -width => 25,
					   -textvariable => \$end_var,
					   )->pack(side => 'left');


	# Buttons on the right
	my $plate_ok = $left_frame->Button(-font => $button_f, 
					   -text => 'OK',
					   -width => 6,
					   -command => sub {

					       # We must have a valid entry in each box
					       if ( ($plate_var eq '') || ($start_var eq '') || ($end_var eq'') ){
						   return;
					       } else {

						   # Convert any uppercase to lowercase in
						   # $start_var and $end_var
						   $start_var = lc $start_var;
						   $end_var = lc $end_var;
						   # Want no spaces in $plate_var
						   $plate_var =~ s/\s//g;


						   # Check 'Start' nb. 384well plate is not yet valid
						   my ($start_row, $start_col);
						   if ($start_var =~ /^([a-h])(\d+)/) {
						       $start_row = $1;
						       $start_col = $2;
						   } else {
						       &well("Start");
						       return();
						   }
						   
						   if ( ($start_col > 12) || ($start_col < 1) ){
						       &well("Start");
						       return();
						   }
						   
						   # Check 'End'
						   my ($end_row, $end_col);
						   if ($end_var =~ /^([a-h])(\d+)/) {
						       $end_row = $1;
						       $end_col = $2;
						   } else {
						       &well("End");
						       return();
						   }
						   if ( ($end_col > 12) || ($end_col < 1) ){
						       &well("End");
						       return();
						   }
						   
						   # Make sure 'Start' comes before 'End'
						   my ($start_no, $end_no);
						   $start_no = $rowcol2id_96->{$start_var};
						   $end_no = $rowcol2id_96->{$end_var};


						   if ($end_no < $start_no){
						       eval {
							 main::popup ("Warn", ["Sorry, 'Start' must come before 'End'."]);
						       };
						       return();
						   }

						   if ($end_no == $start_no){
						       eval {
							 main::popup ("Warn", ["Sorry, 'Start' must not be equal to 'End'."]);
						       };
						       return();
						   }

						   # We now need to make something for finch to use
						   &make_list($start_no, $end_no);
						   $plate_win->destroy();
						   undef $plate_win;
					       }
					   })->pack();
	
	my $plate_cancel = $left_frame->Button(-font => $button_f, 
					       -text => 'Cancel',
					       -width => 6,
					       -command => sub {
						   $plate_win->destroy();
						   undef $plate_win;
#							&unmonitor(\$chem_var, 'chem_var', \&chem_changed);
					       })->pack();
    } else {
	warn "Nah ! already active !\n";
    }
}

sub make_list {
    my ($start_no, $end_no) = @_;
    my @platedata;
    foreach my $next ($start_no .. $end_no){
	# convert $next to rowcol
	my $rowcol = $id2rowcol_96->{$next};
	push @platedata, "$plate_var$rowcol";
    }

    &$return_code(\@platedata);
}


sub well {
    my ($problem) = @_;

    eval {
      main::popup ("Warn", ["Sorry, $problem must be in rowcolumn format (eg. a12 - p24)"]);
    };
    
}


1;
