#!/usr/local/bin/perl -wT
#########
# The Amazing Configuraliser
# rmp Jan 2001
#
use strict;
use lib "../modules";

use EnsWeb;
use ColourMap;
use WebUserConfig;
use CGI qw(:cgi);
use Apache;
use GramenePage;

&main();
1;


sub main {
    my $cgi        = new CGI;
    my $Config     = new WebUserConfig($cgi);
    my $ColourMap  = new ColourMap;

    my $script_to_configure = $cgi->param('script');
    my $SCRIPTNAME          = $ENV{'SCRIPT_NAME'};
    my %script_real_names   = (
			       'contigviewtop' => "Contigview Overview Window",
			       'contigviewbottom' => "Contigview Detailed Window",
			       );
    my $r           = Apache->request();
    my $grpg=GramenePage->new($r);

    if(!defined $script_to_configure) {
	print &config_header($grpg);
	print qq(
<h1>Select a view to customise</h1>
);

#	for my $section ($Config->sections()) { #}
	for my $section (qw(contigviewtop contigviewbottom)) {
	    print qq(<a href="$SCRIPTNAME?script=$section">$script_real_names{$section}</a><br />\n) if(scalar $Config->subsections($section) != 0);
	}

	print &config_footer($grpg);

    } else {
	my $action = $cgi->param('action') || "default";
	
	my $cookie;

	if($action eq "reset2defaults") {
	    #########
	    # reset will immediately expire the cookie we give it
	    #
	    $cookie = $Config->reset($script_to_configure);
	    
	} else {
	    #########
	    # pull our values out of the CGI block
	    #
	    
	    #########
	    # loop through 'artefacts'
	    #
	    for my $subsection ($Config->subsections($script_to_configure)) {
		#########
		# loop through settings for artefacts
		#
		for my $key (keys %{$Config->values($script_to_configure, $subsection)}) {
		    
		    my $parameter_name = qq($script_to_configure.$subsection.$key);
		    
		    my $val = $cgi->param($parameter_name);
		    
		    $Config->set($script_to_configure, $subsection, $key, $val) if(defined $val && $val ne "");
		}
	    }
	    $cookie = $Config->save();
	}
	
	print &config_header($grpg,$cookie);

	my $length = length($cookie);
	print qq(
    <h2>Customising '$script_real_names{$script_to_configure}'</h2>
    <form action="$SCRIPTNAME" method="POST">
);

	my @available_col = $ColourMap->names();
	my @available_on  = qw(off on);
	my @available_str = qw(f r b);
	
	my @available_settings = qw(on col dep);
	my $colspan = scalar @available_settings + 1;
	
	my $i = 1;
	my %colours = (
		       $i  => qq(bgcolor="#ffffe7"),
		       !$i => qq(bgcolor="#ffffc8"),
		       );
	
	print qq(
<table bgcolor="#aaaaaa" align="center" border="0" cellspacing="0" cellpadding="0">
  <tr bgcolor="#aaaaaa">
    <td colspan="3" align="center"><img src="/gfx/blank.gif" width="1" height="1" alt=""></td>
  </tr>
  <tr bgcolor="#aaaaaa">
    <td align="center"><img src="/gfx/blank.gif" width="1" height="1" alt=""></td>
    <td>
<table border="0" align="center" cellspacing="0" cellpadding="0">
  <tr class="yellow2">
    <th colspan="2">Feature&nbsp;</th>
);

	for my $key (@available_settings) {
	    next if($key eq "on");
	    my $keyname = ucfirst($Config->get($script_to_configure, '_names', $key));
	    print qq(<th>&nbsp;$keyname&nbsp;</th>);
	}

	print qq(
  </tr>
  <tr><td colspan="$colspan"><img src="/gfx/blank.gif" width="450" height="1" alt=""></td></tr>
  <tr $colours{$i}><td colspan="$colspan"><img src="/gfx/blank.gif" width="2" height="2" alt=""></td></tr>
);

	$i = 1;
	my @subsections = $Config->subsections($script_to_configure);
	
	my @order = sort { $Config->get($script_to_configure, $a, 'pos') <=> $Config->get($script_to_configure, $b, 'pos') } @subsections;
	print STDERR join('+',@subsections),"\n->",join('+',@order),"\n";
	for my $subsection (@order) {

	    #########
	    # loop through settings for artefacts
	    #
	    print qq(<tr $colours{$i}>);
	    $i = !$i;

	    #########
	    # scary bit!!! I'mma gonna build mahself a label missy!
	    #
	    my $glyphset_name = qq(Bio::EnsEMBL::GlyphSet::).($Config->get($script_to_configure,$subsection,'glyph') || $subsection);
	    
	    eval "require $glyphset_name";
	    next if($@);
	    
	    $glyphset_name->import();
	    my $glyphset = new $glyphset_name;
	    # 'text' is for the tracks that use a generic glyphset, like est.pm
	    my $subsection_label = $Config->get($script_to_configure,$subsection,'text') || $subsection;
	    $subsection_label = $glyphset->label->text() if(defined $glyphset->label());
	    
	    print qq(  <td align="left">&nbsp;$subsection_label&nbsp;</td>);

	    for my $key (@available_settings) {
		print qq(  <td align="center">&nbsp;);
		
		my $content = "";
		
		if($Config->canset($script_to_configure, $subsection, $key)) {
		    
		    my $parameter_name = qq($script_to_configure.$subsection.$key);
		    
		    my $val = $Config->get($script_to_configure, $subsection, $key);
		    $val = "" unless(defined $val);
		
		    if($key eq "on") {
			$content .= qq(<select name="$parameter_name">\n);
			for my $opt (@available_on) {
			    my $selected = "";
			    $selected = "selected" if($opt eq $val);
			    $content .= qq(  <option value="$opt" $selected>$opt</option>\n);
			}
			$content .= qq(</select>\n);
			
		    } elsif($key eq "col" || $key eq "hi" || $key eq "low" || $key eq "known" || $key eq "unknown" || $key eq "ext") {
			$content .= qq(<select name="$parameter_name">\n);
			for my $opt (@available_col) {
			    my $id = $ColourMap->id_by_name($opt);
			    my $selected = "";
			    $selected = "selected" if($id eq $val);
			    
			    $content .= qq(  <option value="$id" $selected>$opt</option>\n);
			}
			$content .= qq(</select>\n);
			
		    } elsif($key eq "str") {
			$content .= qq(<select name="$parameter_name">\n);
			for my $opt (@available_str) {
			    my $selected = "";
			    $selected = "selected" if($opt eq $val);
			    my $opt_english = "both";
			    $opt_english = "forward" if($opt eq "f");
			    $opt_english = "reverse" if($opt eq "r");
			    
			    $content .= qq(  <option value="$opt" $selected>$opt_english</option>\n);
			}
			$content .= qq(</select>\n);
			
		    } elsif($key eq "dep") {
			$content .= qq(<select name="$parameter_name">\n);
			my $expanded=$Config->default($script_to_configure,$subsection,$key);
			$val=$expanded || 6 if $val;
			my %selected = (
					'0' => "",
					$expanded => "",
					);
			$selected{$val} = "selected";

			$content .= qq(  <option value="0" $selected{'0'}>compact</option>\n);
			$content .= qq(  <option value="$expanded" $selected{$expanded}>expanded</option>\n);
			$content .= qq(</select>\n);

		    } else {
			$content .= qq(<input type="text" name="$parameter_name" value="$val" size="3">\n);
		    }
		}
		$content = "&nbsp;" if($content eq "");
		print qq($content&nbsp;</td>);
	    }
	    print qq(</tr>);
	}

	my $colspan2 = $colspan-1;
	print qq(
      <tr $colours{$i}>
	<td colspan="$colspan2" align="right">
	  <input type="hidden" name="script" value="$script_to_configure">
	  <input type="image" border="0" src="/gfx/buttons/save.gif" hspace="4" vspace="4" alt="Save">
	</form>
	</td>
	<td align="center">
	  <form action="$ENV{'SCRIPT_NAME'}" method="POST">
	    <input type="hidden" name="script" value="$script_to_configure">
	    <input type="hidden" name="action" value="reset2defaults">
	    <input type="image" border="0" src="/gfx/buttons/reset.gif" hspace="4" vspace="4" alt="Reset to defaults">
	  </form>
	</td>
      </tr>
    </table>
    </td>
    <td align="center"><img src="/gfx/blank.gif" width="1" height="1" alt=""></td>
  </tr>
  <tr bgcolor="#aaaaaa">
    <td colspan="3" align="center"><img src="/gfx/blank.gif" width="1" height="1" alt=""></td>
  </tr>
</table>
);
	print &config_footer($grpg);
    }
}


sub config_header {
    my ($grpg,$cookie) = @_;
    my $content = "";

    if(defined $cookie) {
	$content .= &CGI::header('-cookie' => $cookie);
    } else {
	$content .= &CGI::header();
    }
    #$content .= &EnsWeb::make_cgi_header();
    my ($head,$onload,$js_divs)=&EnsWeb::cgi_header_info();
    $content .= $head.$grpg->start_body(  -ensembl=>1, -bodyattr=>$onload, -bodyfirst=>$js_divs  );
    $content .= &EnsWeb::print_form();

    return $content;
}

sub config_footer {
    my ($grpg)=@_;
    my $content = "<br />";
    #$content .= &EnsWeb::make_cgi_footer();
    $content .= $grpg->end_body;
    return $content;
}
