package List;

sub new {
my $pkg  = shift;
my $setup = {};

$setup->{Clone}			= '';
$setup->{GridRowValue}		= 1;
$setup->{GridColumnValue}	= 1;
$setup->{Option}		= '';
$setup->{Preplist}		= '';
$setup->{Rawdata}		= [];
$setup->{Database}		= '';
$setup->{Count}			= 0;
$setup->{Chromosome}		= '';
$setup->{Enzyme}		= '';
$setup->{FILENAMES}		= {};

bless $setup,$pkg;
return $setup;
}#end new


sub Display {

my ($pkg,$frame, $mainwindow) = @_;


$pkg->GetSpecies();

$pkg->{WaitBox} = $$mainwindow->WaitBox( -title => 'Please Wait');
	
$pkg->{WaitBox}->wm('minsize', 120, 30);
$pkg->{WaitBox}->wm('maxsize', 120, 30);


my $b = $$frame->BrowseEntry(		
		-width 			=> 12,
		-fg 			=> 'blue',
		-browsecmd 		=> sub{$pkg->Query();},
		-font			=> '-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1',
		-state 			=> 'normal',
		-variable 		=> \$pkg->{Chromosome},
)->grid(	-row 			=> $pkg->{GridRowValue},
		-column 		=> $pkg->{GridColumnValue},
		-sticky			=> 'w');
					
$b->Subwidget("slistbox")->Subwidget("listbox")->configure(	
		-font 			=>'-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1',
		-fg 			=> 'blue',
		-width 			=> 5,
		-height			=> 5);	

$b->Subwidget("entry")->Subwidget("entry")->configure(		-bg 	=> 'lemonchiffon1');	
$b->Subwidget("entry")->Subwidget("label")->configure(		-text 	=> 'Species Chr',
								-fg	=> 'black');
								
$b->Subwidget("entry")->bind("<Return>",sub{	$pkg->Query();});


my @SPECIES = sort keys %{$pkg->{FILENAMES}};
$b->insert('end',@SPECIES);



$pkg->{GridColumnValue}++;

									
$pkg->{Preplist} = $$frame->BrowseEntry(
		-width 			=> 15,
		-fg 			=> 'blue',
		-listcmd 		=> sub{$pkg->Editedlist();},
		-browsecmd 		=> sub{$pkg->Clonequery();},
		-font			=> '-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso8859-1',
		-state 			=> 'normal',
		-variable 		=> \$pkg->{Clone},
)->grid(	-row 			=> $pkg->{GridRowValue},
		-column 		=> $pkg->{GridColumnValue},
		-sticky			=> 'w');
					
$pkg->{Preplist}->Subwidget("slistbox")->Subwidget("listbox")->configure(	#-font 	=>'-schumacher-clean-bold-r-normal--14-140-75-75-c-80-iso8859-1',
										-fg 	=> 'blue',
										-width 	=> 25,
										-height	=> 5);	
										
$pkg->{Preplist}->Subwidget("entry")->Subwidget("entry")->configure(		-bg 	=> 'lemonchiffon1');	

$pkg->{Preplist}->Subwidget("entry")->Subwidget("label")->configure(		-text 	=> 'Clone',
										-fg	=> 'black');
$pkg->{Preplist}->Subwidget("entry")->bind("<Return>",sub{ $pkg->Editedlist();});

$pkg->{GridColumnValue}++;
$$frame->Label(				-width 			=> 5, 
					-height 		=> -1,
					-bg			=> 'grey85',
					-textvariable		=> \$pkg->{Count},
					-anchor			=> 'center',
					-relief			=> 'sunken',
					-font 			=> '-adobe-helvetica-bold-r-normal--14-100-100-100-p-82-iso8859-1'
		)->grid(		-row			=> $pkg->{GridRowValue},
					-column			=> $pkg->{GridColumnValue},
					-pady			=> 10,
					-padx			=> 5,
					-sticky			=> 'w');
					
$pkg->{GridColumnValue}--;
$pkg->{GridRowValue}++;



}

sub Setoption {
my $pkg = shift;
if(@_)
{
	$pkg->{Option} = shift;
}
else
{
	return $pkg->{Option};
}
}



sub Setchromosome {
my $pkg = shift;
if(@_)
{
	$pkg->{Chromosome} = shift;
}
else
{
	my ($s, $c ) = split(/_/, $pkg->{Chromosome});
	return ($s, $c );
}
}

sub Setinformation {
my $pkg = shift;
if(@_)
{
	$pkg->{Information} = shift;
}
else
{
	return $pkg->{Information};
}
}



sub Setdatabase {
my $pkg = shift;
if(@_)
{
	$pkg->{Database} = shift;
}
else
{
	return $pkg->{Database};
}
}



sub Editedlist {
my $pkg = shift;


if(!$pkg->{Clone}){return;}

if($pkg->{Clone} =~ /\*/)
{
$pkg->{Count} = 0;
$pkg->{Clone} =~ s/\*| //g;

$pkg->{Clone} = uc($pkg->{Clone});

	$pkg->{Preplist}->delete(0,'end');
	if(defined $pkg->{Rawdata}->[0][0])
	{
	foreach(@{$pkg->{Rawdata}})
	{
		if(index(uc($_->[0]), $pkg->{Clone}) > -1)
		{
			if($_->[4])
			{
				$pkg->{Preplist}->insert('end',"$_->[0] $_->[4]");
			}
			else
			{
				$pkg->{Preplist}->insert('end',$_->[0]);
			}
			$pkg->{Count}++;
		}
	}
	}
	$pkg->{Clone} = '';

	if(($pkg->{Count} == 0)&&(defined $pkg->{Rawdata}->[0][0]))
	{
		$pkg->{Clone} = 'No Match';
		foreach(@{$pkg->{Rawdata}})
		{
			if($_->[4])
			{
				$pkg->{Preplist}->insert('end',"$_->[0] $_->[4]");
			}
			else
			{
				$pkg->{Preplist}->insert('end',$_->[0]);
			}
			$pkg->{Count}++;
		}
	}
		
}
}

sub Clonequery {
my $pkg = shift;

foreach(@{$pkg->{Rawdata}})
{
		if(index($pkg->{Clone},$_->[0]) > -1)
		{
		
			$pkg->{Prep_ID} = $_->[1];
			$pkg->{Infodata} = $pkg->{Database}->Retrieve(100 + ($pkg->{Option} * 100), $_->[2], $_->[3]);
			
			$pkg->{Information}->Setdata($pkg->{Infodata});
			last;
		}
}

}

sub GetClones {
my ($pkg, $Option, $Choice) = @_;
my $RefArray =[];
my $FILEPATH = '/usr/local/badger/lib/dodo/';




if($Option == 1)
{
	$FILEPATH = $FILEPATH.$Choice.".done";
}
else
{
	$FILEPATH = $FILEPATH.$Choice.".await";
}

if($pkg->{Refresh})
{
	$pkg->ReWriteFiles($FILEPATH);
	$pkg->RefreshFiles();
}

if(open (FHDL, "$FILEPATH"))
{
	#D.CLONENAME||'-'||D.PREP_NUMBER, 
	#D.ID_PREP, 
	#D.CLONENAME, 
	#R.ID_RDREQUEST
	#M.STATUSDATE,'DD/MM/YYYY')
	
	while(<FHDL>)
	{
		chomp $_;
		my ($ClonePrep, $Prep, $Clone, $Digest) = split(/,/, $_);
		push @$RefArray,[$ClonePrep, $Prep, $Clone, $Digest];
	}
	
close FHDL;

$pkg->{Clone} = '';

	
}

return $RefArray;
}

sub RefreshFiles {
my $pkg = shift;
if(@_)
{
	$pkg->{Refresh} = 1;
}
else
{
	$pkg->{Refresh} = '';
}

}




sub Query {
my $pkg = shift;
if(@_){$pkg->{Option} = shift;}

if($pkg->{Option} > -1)
{
	if($pkg->{Chromosome})
	{
	$pkg->{WaitBox}->Show();
	#$pkg->{Rawdata} = $pkg->{Database}->Retrieve($pkg->{Option}, $pkg->{Chromosome});
	
	$pkg->{Rawdata} = $pkg->GetClones($pkg->{Option}, $pkg->{Chromosome});
	# $pkg->{Database}->GetLock($pkg->{Rawdata});
	$pkg->Showdata();
	$pkg->{WaitBox}->unShow();
	}
}

}


sub Getid_prep {
my $pkg = shift;

foreach (@{$pkg->{Rawdata}})
{
	if(index($pkg->{Clone},$_->[0]) > -1){return $_->[1];}
}
return 0;

}


sub Getid_digest {
my $pkg = shift;

foreach (@{$pkg->{Rawdata}})
{
	if(index($pkg->{Clone},$_->[0]) > -1){return $_->[3];}
}
return 0;



}




sub Showdata {
my $pkg  = shift;

$pkg->{Information}->Clear();
$pkg->{Preplist}->delete(0,'end');
undef $pkg->{Clone};
$pkg->{Count} = 0;
if(defined $pkg->{Rawdata}->[0][0])
{
	foreach(@{$pkg->{Rawdata}})
	{
		while(length($_->[0]) < 12){$_->[0] = "$_->[0] ";}
		
		
		if($_->[4])
		{
			$pkg->{Preplist}->insert('end',"$_->[0] $_->[4]");
		}
		else
		{
			$pkg->{Preplist}->insert('end',$_->[0]);
		}
		$pkg->{Count}++;
	}
}
else
{
	$pkg->{Clone}= 'No Clones Found';
}




}


sub Setclone {
my $pkg = shift;
if(@_)
{
	$pkg->{Clone} = shift;
}
else
{
	return $pkg->{Clone};
}
}

sub Writefile {
my $pkg = shift;

open FHDL ,">Taggedclones.txt";
print FHDL "List of tagged clones from Dodo\n";
foreach(@{$pkg->{Rawdata}})
	{print FHDL "$_->[0]\n";}						
					
close FHDL
}

sub GetSpecies {
my ($pkg) = @_;
my $FILEPATH			= "$ENV{\"BADGER\"}/lib/dodo/";
my $HASHFILES			= {};

if(opendir(DIRHD, $FILEPATH ))
{
	my $file = readdir(DIRHD);
		
	while($file)
	{
		if($file =~ /.done/ || $file =~ /.await/)
		{
			$file =~ s/.done//g;
			$file =~ s/.await//g;
			
			if(!$pkg->{FILENAMES}->{$file})
			{
				$pkg->{FILENAMES}->{$file} = 1;
			}
			
		}
		
	$file = readdir(DIRHD);
	}
		
}
else
{
	print "Cannot Open Directory\n";
}

}

sub ReWriteFiles {
my ($pkg, $FILEPATH) = @_;
my $HASHFILES			= {};


if(open (FHDL, ">$FILEPATH" ))
{
	foreach (@{$pkg->{Rawdata}})
	{
	if(index($pkg->{Clone},$_->[0]) > -1){next;}
	
	print FHDL "$_->[0],$_->[1],$_->[2],$_->[3]\n";
	
	}
	
	close FHDL;
}


}

1;
