#####################################################################
#!/usr/bin/perl -w
# Mode: perl
# Programmer: Kuan Chang
# Date: 12/05/2001
#####################################################################

use strict;
use lib '/export/share_more/ensembl/gramene_ensembl/perl/ontology/';

use DBI;
use CGI qw(:standard *table *TR *th *td *ul *ol);
use MuDB;
use GL qw(:DEFAULT);
#?# use GramenePage;

#####################################################################
# constants
my $GO_PREFIX = "GO:";
my $TO_PREFIX = "TO:";
my $GR_PREFIX = "GR:";
my $HTML_HOME = "/plant_ontology";
my $EXE_HOME ="/perl/ontology/";
my $EXE_NAME ="search_mutant";
my $SEARCH_PROTEIN_EXE_NAME ="protein_search";

#####################################################################
# Connect to the database
my $db = MuDB->new();
#?# my $page = GramenePage->new(Apache->request);

my $input  = param('id');
my ($id, $is_GR);
undef $id;
($id, $is_GR) = extract_accessionID($input);

# create CGI object
my $cgi = new CGI;
print $cgi->header;

if(defined $id){
my $name =  $db->get_mutant_name($id);
my $phenotypic_description = $db->get_phenotypic_description($id);
my $supplementray_description =  $db->get_supplementray_description($id) ? $db->get_supplementray_description($id) : undef;
# null array ?
my @reference_ids = $db->get_reference_ids('mutant', $id) ? $db->get_reference_ids('mutant', $id) : undef;

if($is_GR) {
  print $cgi->start_html(-title=>"Summary for Rice Mutant: $name ($input)");
}

# show GramenePage header
print header();
#?# print $page->start_body;
# show Gramene search engine
print_search_area();

# show mutant page
if($is_GR) {
  print h1("Summary for Rice Mutant: <i>$name</i> ($input)");

  print start_table({-width=>'98%', -align=>'center', -cellpadding=>4, -cellspacing=>1});
  print TR(th({-align=>'left', -class=>'datatitle', -width=>'12%'}, 'Mutant Name'), td({-class=>'databody'}, $name));
  print TR(th({-align=>'left', -class=>'datatitle', -width=>'20%'}, 'Phenotypic Description'), td({-class=>'databody'}, $phenotypic_description));

  if(defined $supplementray_description){
    print TR(th({-align=>'left', -class=>'datatitle'}, 'Supplementray Description'));
    print TR(td({-align=>'left', -class=>'databody'}, $supplementray_description ||'No other informationavailable'));
  }
  if(defined @reference_ids){
    print TR(th({-align=>'left', -class=>'datatitle'}, 'References'));
    print TR(td({-align=>'left', -class=>'databody'}, 'Update Soon'));
  }
print end_table;
}
else{
  print_top("Cannot find any information about $input");
  print_search_area();	
  print h1("Sorry! Cannot find any information about <i>$input</i>");
}
#?# print $page->end_body;

sub print_tree {
  my ($id) = shift;
  my $self = url(-relative=>1);
  my $name  =  $db->get_name($id);	
  my $color_name = '<font color="red">'.$name.'</font>';	

  print start_table({-width=>'98%',-align=>'center',-cellpadding=>3, -cellspacing=>0});
  print TR({-class=>'datatitle', -align=>'left'},th('Derivation'));
  print start_TR({-class=>'databody'}), start_td;

  my @paths = get_paths($id);
  my %seen_it;
  my $previous = 0;
  for my $path (@paths) {
    my $skip;
    for my $component (@$path) {
      print start_ul();
      if($seen_it{$component} && !$skip){
	$previous = $component;
      }
      next if $seen_it{$component}++ && !$skip;
      $skip++;
      my $magic = $db->get_accession($component);
	print li($component eq $id ? ($db->get_relationship_symbol($previous, $component), b($color_name. " (".$magic.")" )) 
	       : ($db->get_relationship_symbol($previous, $component), object_link($component,b($db->get_name($component) ." (".$magic.")")))),"\n";
		
      # print li($component eq $id ? ($db->get_relationship_symbol($previous, $component), b($color_name. " (".$db->get_accession($component).")" )) 
	#       : ($db->get_relationship_symbol($previous, $component), object_link($component,b($db->get_name($component) ." (".$db->get_accession($component).")")))),"\n";	       
      #         : ObjectLink($component,b(GetName($component) ." (".GetGOAccession($component).")"))),"\n";
       $previous = $component;
    }
    if (my @children = $db->get_children($id)) {
      # print ul(li([map { a({-href=>"$self?id=$id"},$_->GetName . " ($_)") } @children]));
    #  print ul(li([map { $db->get_relationship_symbol($id, $_).a({-href=>"$self?id=".$db->get_accession($_)}, $db->get_name($_) . " (".$db->get_accession($_).")") } @children]));
	foreach my $child (@children) {
	    print ul(li($db->get_relationship_symbol($id, $child).a({-href=>"$self?id=".$db->get_accession($child)}, $db->get_name($child) . " (".$db->get_accession($child).")") ));
	}
    }
    print scalar(end_ul()."\n") x @$path;
  }
  print end_td,end_TR,end_table;
}

sub print_search_area {
  my $self = url(-relative=>1);
  print start_form(-method=>'GET', -action=>"search");
  print table({-width=>'100%', -align=>'center'}, 
	  # TR({-align=>'center', -class=>'searchbody'}, td(font({-color=>'red'},"Type in ID or keyword to search for&nbsp;&nbsp;"), textfield(-framewidth=>4, -name=>'query', -size=>55, -maxlength=>256),font("&nbsp;&nbsp;"),submit(-name=>'btn', -value=>'Ontology Search'),font("&nbsp;"), reset(-name=>'Clear'))) 
	   #   TR({-align=>'center', -class=>'searchbody'}, td(submit(-name=>'btn', -value=>'Ontology Search'),reset(-name=>'Clear'))) 
	);
  
#<td align=center><input type=text value="" framewidth=4 name=query size=55 maxlength=256><br><input name=btn type=submit value="GO Search"><input name=btn type=submit value="TO Search"></td
  print end_form();
  print "<hr>"; 	
}

sub object_link {
  my($term_id,$title) = @_;
  my $accession = $db->get_accession($term_id);
  my $href = "$EXE_HOME$EXE_NAME?id=$accession";
  return a({-href=>$href},$title);
}

# utility functions
sub extract_ID{
  my $id = shift;
  my $is_GR = 0;
  if($id =~ /$GR_PREFIX/) {
    $id =~ s/$GR_PREFIX//;
    $is_GR = 1;
  } 
  return ($id, $is_GR);
}
