package Blast::Client_View;
use strict;
use IO::String;
use Bio::SearchIO;
use CGI;
use IO::File;

sub new {
   my $self = {};
   bless $self, shift;
 
   my $query = new CGI;
   $self->query( $query );
    
   return $self;
}

#
# gets sets the query object
#
sub query {
   my ($self, $obj) = @_;
   if( $obj ) {
      $self->{'_query'} = $obj;
   }
   return $self->{'_query'};
}

#
# gets sets the config object
#
sub config {
   my ($self, $obj) = @_;
   if( $obj ) {
      $self->{'_config'} = $obj;
   }
   return $self->{'_config'};
}

sub program_selector {
   my $self = shift;
   #
   # dropdown with the name parameter of each item in the programs array
   #

   my @values = map { $_->{name}             }                    @{$self->config->programs};
   my %labels = map { $_->{name}, $_->{name} ." - ". $_->{desc} } @{$self->config->programs};

   unshift @values, 'unselected';
   $labels{'unselected'} = 'Please Select a Program';

   return $self->query->popup_menu( -name    => 'program',
                              -values  => \@values,
                              -labels  => \%labels,
                              -default => $self->config->options->{p}
                            );
}


sub matrix_selector {
   my $self = shift;
   #
   # dropdown with the name parameter of each item in the matrices array
   #

   my @values = map { $_->{name} } @{$self->config->matrices};

   return $self->query->popup_menu( -name    => 'matrix',
                              -class   => 'blastform_dropdown',
                              -values  => \@values,
                              -default => $self->config->options->{M}
                            );
}


sub database_selector {
   my $self = shift;
   #
   # simply display the name parameter of each item in the progras array
   #

   my @values = map { $_->{name}             } @{$self->config->databases};
   my %labels = map { $_->{name}, $_->{desc} . " - " . uc($_->{type}) } @{$self->config->databases};

   unshift @values, 'unselected';
   $labels{'unselected'} = 'Please Select a Database';

   return $self->query->popup_menu( -name    => 'database',
                              -values  => \@values,
                              -labels  => \%labels,
                              -default => $self->config->options->{d}
                            );
#   return $self->query->checkbox_group( -name    => 'database',
#                                  -values  => \@values,
#                                  -labels  => \%labels,
#                                  -default => $self->config->options->{d},
#                                  -columns=>3
#                            );
}

sub e_value_selector {
   my $self = shift;
   return $self->query->popup_menu( -name    => 'e_cutoff',
                              -class   => 'blastform_dropdown',
                              -values  => \@{$self->config->e_values},
                              -default => $self->config->options->{e}
                            );
}

sub filter_selector {
   my $self = shift;

   return $self->query->radio_group( -name    => 'filter',
                               -values  => ['T','F'],
                               -default => $self->config->options->{F},
                               -labels  => { 'T'=>'On', 'F'=>'Off' }
                            );


}

sub gapped_selector {
   my $self = shift;

   return $self->query->radio_group( -name    => 'gapped',
                               -values  => ['T','F'],
                               -default => $self->config->options->{g},
                               -labels  => { 'T'=>'True', 'F'=>'False' }
                            );


}

sub numalign_selector {
   my $self = shift;
   my %labels;

   return $self->query->popup_menu( -name    => 'num_align',
                              -class   => 'blastform_dropdown',
                              -values  => \@{$self->config->num_alignments},
                              -default => $self->config->options->{b},
                              -labels  => \%labels
                            );
}

sub wordsize_selector {
   my $self = shift;
   my %labels;

   $labels{0} = '-DEFAULT-';

   return $self->query->popup_menu( -name    => 'word_size',
                              -class   => 'blastform_dropdown',
                              -values  => \@{$self->config->word_sizes},
                              -default => $self->config->options->{W},
                              -labels  => \%labels
                            );



}

sub print_report {
   my ($self, $report) = @_;

   my ( $report_text ) = $report->result();
  #
  # print report (use bioperl searchio object)
  #
   my $stringio = IO::String->new( $report_text );
   my $parser   = Bio::SearchIO->new('-fh'     => $stringio,
                                     '-format'  => 'blast'
   ); 
   my $out      = new Bio::SearchIO( -writer => $self->config->report_writer );

   $self->config->page_header();
   $out->write_result($parser->next_result);
   $self->config->page_footer();
}

sub print_form {
   my $self = shift;

   print qq {

   <form method="post" name="blast_form">
   <table width="50%">
      <tr>
         <td>Program</td>
         <td colspan="2"> }. $self->program_selector(). qq { </td>
      </tr>
      <tr>
         <td colspan="3">
           Type or Paste a Query Sequence in Fasta Format<br>
           <textarea name="query" rows="5" cols="80">} . $self->config->options->{i} . qq{</textarea> 
         </td>
      </tr>
      <tr>
         <td>Database</td>
         <td colspan="2"> }. $self->database_selector(). qq { </td>
      </tr>
      <tr>
         <td colspan="3"><input type="button" value="Run BLAST" onClick="validate_blast_form()"> or <input type="reset"> or <input type="button" value="BLAST at NCBI" onClick="blast_at_ncbi()"></td>
                         <input name="run_blast" type="hidden" value="0">
      </tr>
      <tr>
         <th colspan="3">
Options
         </th>
      </tr>
      <tr>
         <td>E Value</td>
         <td colspan="2"> }. $self->e_value_selector(). qq { </td>
      </tr>
      <tr>
         <td>Number of alignments to show</td>
         <td colspan="2"> }.  $self->numalign_selector(). qq { </td>
      </tr>
      <tr>
         <td width="40%">Word size</td>
         <td> }.  $self->wordsize_selector(). qq { </td><td align="left" width="50%">default = 11 for BLASTN, 3 for all others</td>
      </tr>
      <tr>
         <td>Matrix</td>
         <td colspan="2"> }.  $self->matrix_selector(). qq { </td>
      </tr>
      <tr>
         <td>Gapped alignment</td>
         <td colspan="2"> }.  $self->gapped_selector(). qq { </td>
      </tr>
      <tr valign="top">
         <td>Filtering</td>
         <td> }.  $self->filter_selector(). qq { </td><td align="left" width="50%">DUST filter for BLASTN, SEG filter for all others<br>
                                                                                   Because of resource limits, when BLASTing against the Chromosomal database with filtering off,
                                                                                   the filtering is actually set to 'm D' which turns off filtering
                                                                                   for extending hits, but leaves filtering on when building the initial words.</td>
      </tr>

   </table>

   </form>
   <form method="post" name="ncbi_blast_form" action="}. $self->config->ncbi_link(). qq {">
      <input name="program" type="hidden">
      <input name="query"   type="hidden">
   </form>
   };
}
1;