#######################################################################
# 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.                      #
#######################################################################

package Draw_Reads;

require InfoWin;
require ByTempl;

my %Good;			# Good Reads
my %GoodHid;			# Original config for GoodObjs.
my %Bad;			# Bad Reads
my %BadHid;
my %Uglys;			# Lone Reads
my %UglyHid;
my $c;
my $FONT = '8x13';
my $FONTs = '6x10';
my $MW;
my $box;
my @Data;

#########################################################################################################
sub GetGood{
    %Good;
}
sub GetBad{
    %Bad;
}
sub GetUgly{
    %Ugly;
}
sub SetData{
    my ($D) = @_;
#    undef(@Data);
    @Data = &ByTempl::Get_Assem;
}
sub ClearData{
    undef(@Data);
}
#########################################################################################################
sub SetMW{
    my ($MWIN) = @_;
    if(defined($MWIN))
    {
	$MW = $MWIN;
    }
}
##########################################################################################################
sub SetCanv{

    my($canv) = @_;
    $c = $canv;
}
##########################################################################################################
sub read_info{
    my ($c,$data) = @_;
    my @j = @{$data};
    my $left;
    my $right;
    my $fwd =0;
    my @vals;
    my $out;
    my $size;
    my $keyorinfo;
    if(&InfoWin::Create_InfoWin)
    {
	&InfoWin::ClearWin;
       
	my $canv = &InfoWin::GetInfoCanv;
       
	   if($canv)
	   {
	       if($j[5] < $j[6]){$left = 50;$right = 150;}else{$left = 150;$right=50;}
	       
	       &InfoWin::AddStuff (&InfoWin::InfoFR_box($canv,$left,20,$right,50,$j[7],$j[8],$j[9]));
	       $out = $j[0];
	       &InfoWin::AddStuff ($canv->create('text', 30, 10, -text =>$out, -font => $FONT,-fill => 'red'));
	       $out = $j[11];
	       &InfoWin::AddStuff ($canv->create('text', 90, 10, -text =>$out, -font => $FONTs,-fill => 'black'));
	       $out = $j[10];
	       &InfoWin::AddStuff ($canv->create('text', 160, 10, -text =>$out, -font => $FONT,-fill => 'black'));
	       $out = "\tfwd =>>\tfwd <<=\trev =>>\trev <<=";
	       &InfoWin::AddStuff ($canv->create('text', 80, 60, -text =>$out, -font => $FONTs,-fill => 'black'));
	       $out = "\t$j[1]\t$j[2]\t$j[3]\t$j[4]";
	       &InfoWin::AddStuff ($canv->create('text', 80, 70, -text =>$out, -font => $FONTs,-fill => 'dark green'));
	       $out = "\tSize = $j[12] bp";
	       &InfoWin::AddStuff ($canv->create('text',40, 90, -text =>$out, -font => $FONTs,-fill => 'dark green'));
	       $canv->pack;
	   }
       
    }
}

##########################################################################################################

sub Draw_good {
    my ($y, $xxt, $yxt,$xmax) =@_;
    my $line;
    my @rows;
    my $left;
    my $right;
    my $lastright = 50;
#    my $xmax;
    my @arr;
    my $slot;
    my $l;
    my $r;
    my $l1;
    my $r1;
    my $maxy =0;
    my $sy;
    my $lpr;
    my $rpr;
    my @tData = grep ((($_->[7] == 1) && ($_->[6] == 1) && ($_->[5] ==  1) && ($_->[8] == 0)),@Data);

    
    @tData = reverse sort by_rs2 @tData;
    @arr = @{$tData[0]};
    undef(%GoodObjs);
#    if($arr[11] < $arr[12]){$xmax = $arr[12];}else{$xmax = $arr[11];}
 
    @tData = sort by_ls1 @tData;
    &Qplot::Set_xxt_yxt($xxt,$yxt);
    
    
    foreach $line (@tData)
    {

 
       $left = &Qplot::dmapx($$line[9],$xmax,0);
       $l1 = &Qplot::dmapx($$line[10],$xmax,0);
       $right = &Qplot::dmapx($$line[11],$xmax,0);
       $r1 = &Qplot::dmapx($$line[12],$xmax,0);
 
        
       if (($slot = &Slot_fit($left,@rows)) != -1)
          {
	   $rows[$slot] = $right;
	   $sy= $y + ($slot * 10);
          }else {
	         push(@rows,$right);
	         $sy = $y + (($#rows) * 10);
                 }
      my $fwd =0;

      if($$line[15] eq "forward"){$fwd = 1;}

      my $l = $c->create('line', $left, $sy, $right,$sy, -width => 2,-fill => 'green');
      $c->addtag($$line[0],withtag => $l);

      my $id;
      my @Linfo;
      my @Rinfo;

      $id = &FRead_box($left,$sy-3,$l1,$sy+3,$fwd,$c,$$line[5],$$line[8]);
 
      push (@Linfo,$$line[0],$$line[1],$$line[2],$$line[3],$$line[4],
                   $left,$l1,$fwd,$$line[5],$$line[8],$$line[13],$$line[15],int(abs($$line[9]-$$line[10]))); 
      $c->bind($id,'<Any-Enter>'=>[\&read_info,\@Linfo]);
 
      if($$line[16] eq "forward"){$fwd = 1;}else{$fwd =0;}
 
      $id = &FRead_box($right,$sy-3,$r1,$sy+3,$fwd,$c,$$line[5],$$line[8]);

      push (@Rinfo,$$line[0],$$line[1],$$line[2],$$line[3],$$line[4],
                    $right,$r1,$fwd,$$line[5],$$line[8],$$line[14],$$line[16],int(abs($$line[11]-$$line[12]))); 
      $c->bind($id,'<Any-Enter>'=>[\&read_info,\@Rinfo]);
      if ($maxy < $sy){$maxy = $sy;}
       $c->update;
  #    }
    
    } 
 $lasty = $maxy;    
 $maxy;
}

#####################################################################################################
sub Draw_bad {
    my ($y, $xxt,$yxt,$xmax) =@_;
    my $line;
    my @rows;
    my $left;
    my $right;
    my $lastright = 50;
#    my $xmax;
    my @arr;
    my $slot;
    my $maxy;
    my $sy;
    my $l1;
    my $r1;  
    my @tData = grep ((($_->[5] == 1) && (($_->[6] == 0) || ($_->[7] == 0))),@Data);

    @tData = reverse sort by_rs2 @tData;
    @arr = @{$tData[0]};
    undef(%BadObjs);
#    if($arr[11] < $arr[12]){$xmax = $arr[12];}else{$xmax = $arr[11];}
 
#    @tData = sort by_ls1 @tData;
    @tData = sort by_pt @tData;
    &Qplot::Set_xxt_yxt($xxt,$yxt);
    
    
    foreach $line (@tData)
    {
#     if(($$line[5] == 1) && (($$line[6] == 0) || ($$line[7] == 0)))      # in limit
#      {
     
       $left = &Qplot::dmapx($$line[9],$xmax,0);
       $l1 = &Qplot::dmapx($$line[10],$xmax,0);
       $right = &Qplot::dmapx($$line[11],$xmax,0);
       $r1 = &Qplot::dmapx($$line[12],$xmax,0);
 
   
       if (($slot = &Slot_fit($left,@rows)) != -1)
          {
	   $rows[$slot] = $right;
	   $sy = $y + ($slot * 10);
          }else {
	         push(@rows,$right);
	         $sy = $y + (($#rows) * 10);
                 }
      my $fwd =0;
      my $id;
      my @Linfo;
      my @Rinfo;

       if($$line[15] eq "forward"){$fwd = 1;}
    #   $BadObjs{$c->create('line', $left, $sy, $right,$sy, -width => 2,-fill => 'red')} = $$line[0];
       my $l =  $c->create('line', $left, $sy, $right,$sy, -width => 2,-fill => 'red');
       $c->addtag($$line[0],withtag => $l);
       $id = &FRead_box($left,$sy-3,$l1,$sy+3,$fwd,$c,$$line[5],$$line[8]); # 
       push (@Linfo,$$line[0],$$line[1],$$line[2],$$line[3],$$line[4],
                   $left,$l1,$fwd,$$line[5],$$line[8],$$line[13],$$line[15],int(abs($$line[9]-$$line[10]))); 
       $c->bind($id,'<Any-Enter>'=>[\&read_info,\@Linfo]);

       if($$line[16] eq "forward"){$fwd = 1;}else{$fwd =0;}
       $id = &FRead_box($right,$sy-3,$r1,$sy+3,$fwd,$c,$$line[5],$$line[8]);
       push (@Rinfo,$$line[0],$$line[1],$$line[2],$$line[3],$$line[4],
	     $right,$r1,$fwd,$$line[5],$$line[8],$$line[14],$$line[16],int(abs($$line[9]-$$line[10]))); 
       $c->bind($id,'<Any-Enter>'=>[\&read_info,\@Rinfo]);
       if(defined($sy)){if ($maxy < $sy){$maxy = $sy;}}
       $c->update;
#      }
    }    
   
 $maxy;
}
######################################################################################################
sub Draw_ugly {
    my ($y, $xxt, $yxt,$xmax) =@_;
    my $line;
    my @rows;
    my $left;
    my $right;
    my $lastright = 50;
#    my $xmax;
    my $arr;
    my $slot;
    my $maxy;
    my $sy;
    my $r;
    my $r1;  
    my $l1;
    $maxy =0;
    my @inlist;
    my @tData = grep ((($_->[5] == 0) && (($_->[6] == 0) || ($_->[7] ==0) || ($_->[8] ==0))),@Data);

    @tData = reverse sort by_rs2 @tData;
    $arr = $tData[0];
    undef (%UglyObjs);
#    if($$arr[11] < $$arr[12]){$xmax = $$arr[12];}else{$xmax = $$arr[11];}
 
    @tData = sort by_ls1 @tData;
    &Qplot::Set_xxt_yxt($xxt,$yxt);
    
    
    foreach $line (@tData)
    {
  #   if(($$line[5] == 0) && (($$line[6] == 0) || ($$line[7] ==0) || ($$line[8] ==0)))      # in limit
  #    {
     
       $left = &Qplot::dmapx($$line[9],$xmax,0);
       $l1 = &Qplot::dmapx($$line[10],$xmax,0);
       $right = &Qplot::dmapx($$line[11],$xmax,0);
       $r1 = &Qplot::dmapx($$line[12],$xmax,0);

       if (($slot = &Slot_fit($left,@rows)) != -1)
          {
      	      $rows[$slot] = $right;
	      $sy= $y + ($slot * 10);
          }else {		# 
	      push(@rows,$right);
	      $sy = $y + (($#rows) * 10);
	  }
       my $fwd =0;
       my $id;
       my @Sinfo;

       # $c->create('line', $left, $sy, $right,$sy, -width => 2,-fill => 'red');
     

       $fwd = 1 if($$line[15] eq "forward");
       $id = &FRead_box($left,$sy-3,$l1,$sy+3,$fwd,$c,$$line[5],$$line[8]);
       $c->addtag($$line[0],withtag => $id);
       push (@Sinfo,$$line[0],$$line[1],$$line[2],$$line[3],$$line[4],
	     $right,$r1,$fwd,$$line[5],$$line[8],$$line[14],$$line[16],int(abs($$line[9]-$$line[10])));
       $c->bind($id,'<Any-Enter>'=>[\&read_info,\@Sinfo]);
       if ($maxy < $sy){$maxy = $sy;}
       $c->update;
#      }
    }    
    $lasty = $maxy;
  
   
 $maxy;
}
#####################################################################################################
sub FRead_box {

my ($x1,$y1,$x2,$y2,$fwd,$canv,$hfr,$bad) = @_; 

my $col;
my $h = int((abs($x1-$x2))/2);
my $circ;
my $bid;

if ($x1 < $x2){$h += $x1;$circ = $x1-5;}else{$h += $x2;$circ = $x1+5;}
if(!$fwd){$col = 'yellow';}else{$col = 'blue';}

$bid= $canv->create('polygon',$x1,$y1,$h,$y1,$x2,$y1+3,$h,$y2,$x1,$y2,$x1,$y1,
                  -width=>1,-outline=>'black',-fill=> $col);
    

     # internally inconsistent....
 
$BadObjs{$canv->create('oval',$x1,$y1-1,$circ,$y2+1,-width=> 1,-outline => 'black',-fill=> 'black')} = 'Blob' if($bad); 
  
$bid;
}
#####################################################################################################
sub Slot_fit{
    my ($l,@slots) = @_;
    my $fits = -1;
    my $slot =0;
    my $r;

   foreach $r (@slots)
   {
       if($l > ($r + 20)){$fits = $slot;last;}
       $slot++;       
   }
    $fits;
}
###################################################################################################
sub FindTemplate{
 
    my $picked;
    my @flash;
    my @list = sort &ByTempl::Get_Templ_Array;
    $picked = &ListPick::ListPick($MW,'Pick the template you wish to highlight ',@list);
    my @bbox = (0,0,0,0);
   
    if($picked eq "") {return @bbox;}

    @bbox = $c->bbox($picked); 

    @bbox;
}
##########################################################################################################
sub DArrow{
    my($c , $x , $y) = @_;
   
    $c->create('line', $x, $y - 15, $x,$y, -width => 2,-arrow => 'last' ,-fill => 'red');

}
##########################################################################################################
sub Draw_keylabel{

 my ($y1,$y2,$text,$col) = @_;
 my $len = length($text);
 my @ids;

 if(defined($y1) && defined($y2))
 {
     my $start = int(abs($y2 - $y1)/10)+$y1;
     my $i;
     my $scalfact = ($start+($len *8));
   
 
 
     $ids[0] = $c->create('line', 30, $y1, 30,$y2, -width => 2,-fill => $col);
     $ids[1] = $c->create('line', 30, $y1, 35,$y1, -width => 2,-fill => $col);
     $ids[2] = $c->create('line', 30, $y2, 35,$y2, -width => 2,-fill => $col);
 
 if($scalfact <= $y2)
 {
    for($i=0; $i<$len; $i++)
    {
     $ids[3+$i] = $c->create('text',10,$start + (8 * $i), -text => substr($text,$i,1), -font => $FONTs, -fill => 'blue');
     }
  }
 }
 @ids;

}
######################################################################################################
sub ReScale{

    my ($c,$scfactx,$scfacty)= @_;
  
    foreach(keys(%GoodObjs))
    {
	$c->scale($_,0,0,$scfactx,$scfacty);

    } 
    foreach(keys(%BadObjs))
    {
	$c->scale($_,0,0,$scfactx,$scfacty);

    }
    foreach(keys(%UglyObjs))
    {
	$c->scale($_,0,0,$scfactx,$scfacty);

    } 

}
######################################################################################################
sub by_ls1{

    $$a[9]<=>$$b[9];
}
sub by_rs2 {

    $$a[12]<=>$$b[12];
}
sub by_pt{
    abs($$a[9] - $$a[11])* $$a[8] * $$a[9]<=>abs($$b[9] - $$b[11]) * $$b[8] * $$b[9];
}
#####################################################################################################
1;
