#!/usr/bin/perl
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use File::Basename;


# ------------------------
# main
main : {
  # options hash table
  my %h_options;

  Getopt::Long::Configure( "no_ignorecase" );
  Getopt::Long::Configure( "bundling" );

  # retrieve all options
  GetOptions (\%h_options,
	      'h|help'
	     ) or  pod2usage( -verbose => 1 , noperldoc => 1) ;
	
  # help
  pod2usage( -verbose => 1 , noperldoc => 1) if (exists $h_options{'h'});
	
  # test ARGV parameters
  pod2usage( -verbose => 2 , noperldoc => 1) if (@ARGV != 1);

  my %h_data;
  my $what = "";
  open (TXT, $ARGV[0]) or die "Can't open file $ARGV[0] : $!";
  while(my $line = <TXT>) {
    chomp $line;
    if($line =~ /^##/) {
      $line =~ s/^##//;
      $what = $line;
      $what =~ s/\s+.*//;
      $h_data{$what}[0] = $line;
    }
    elsif($line =~ /^#/) {
      my @a_line = split('\t', $line);
      shift @a_line;
      for (my $i=0; $i<= $#a_line; $i++) {
	$a_line[$i] =~ s/(.+)/'$1'/;
      }
      $h_data{$what}[1] = join(",",@a_line);
    }
    else {
      my @a_line = split('\t', $line);
      $a_line[0] =~ s/^(.+)/\{ name: '$1', data: \[/;
      my $name = shift @a_line;
      #for (my $i=0; $i<= $#a_line; $i++) {
      #	$a_line[$i] =~ s/(.+)/'$1'/;
      #}
      push(@{$h_data{$what}}, "$name".join(",",@a_line)."] }");
    }
  }
  close TXT;
  
  print <<"EOF";
<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Sigenae/Genotoul miRNA pipeline</title>
    <!--  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> -->
    <script type="text/javascript" src="http://snp.toulouse.inra.fr/~sigenae/LIB/js/jquery.js"></script>
    <script type="text/javascript" src="http://snp.toulouse.inra.fr/~sigenae/LIB/js/highcharts.js"></script>
    <script type="text/javascript" src="http://snp.toulouse.inra.fr/~sigenae/LIB/js/exporting.js"></script>
    <script type="text/javascript">
	var chart;
        \$(document).ready(function() {
	  chart = new Highcharts.Chart({
		  chart: {
			  renderTo: 'container',
			  			  defaultSeriesType: 'column',
                          borderWidth: 1,
                          backgroundColor: {
                                linearGradient: [0, 0, 500, 500],
                                stops: [
                                        [0, 'rgb(255, 255, 255)'],
                                        [1, 'rgb(240, 240, 255)']
                                       ]
			  }
			 },
		  title: {text:
EOF
  
  print "'".$h_data{"Global"}[0]."'";
  
  print <<"EOF";
                         },
		  subtitle: {text: 'Source: Sigenae/Genotoul miRNA pipeline'},
                  credits: {enabled:false},
		  xAxis: {
			  categories: [
EOF
  
  print $h_data{"Global"}[1];
  
  print <<"EOF";
				      ]},
		  yAxis: {
			  min: 0,
			  title: {text: 'Sequence number'
                         }},
		  legend: {
		   	   layout: 'vertical',
			   backgroundColor: '#FFFFFF',
			   align: 'right',
			   verticalAlign: 'top',
			   x: -10,
			   y: 70,
			   floating: true,
			   shadow: true
			  },
		  tooltip: {
			    formatter: function() {
				return ''+ this.y;
			    }
			   },
		  plotOptions: {
				column: {
				pointPadding: 0.2,
				borderWidth: 0
			        }
			       },
		  series: [
EOF
  
  for (my $i=2; $i<=$#{$h_data{"Global"}}; $i++) {
    print $h_data{"Global"}[$i];
    print ",\n" if($i!=$#{$h_data{"Global"}});
  }


  print <<"EOF";
			  ]});
	  chart2 = new Highcharts.Chart({
		  chart: {
			  renderTo: 'container2',
			  defaultSeriesType: 'column',
                          borderWidth: 1,
                          backgroundColor: {
                                linearGradient: [0, 0, 500, 500],
                                stops: [
                                        [0, 'rgb(255, 255, 255)'],
                                        [1, 'rgb(240, 240, 255)']
                                       ]
			  }
			 },
		  title: {text:
EOF

  print "'".$h_data{"NM"}[0]."'";

  print <<"EOF";
                         },
		  subtitle: {text: 'Source: Sigenae/Genotoul miRNA pipeline'},
                  credits: {enabled:false},
		  xAxis: {
			  categories: [
EOF

  print $h_data{"NM"}[1];

  print <<"EOF";
				      ]},
		  yAxis: {
			  min: 0,
			  title: {text: 'Sequence number'
                         }},
		  legend: {
			   layout: 'vertical',
			   backgroundColor: '#FFFFFF',
			   align: 'right',
			   verticalAlign: 'top',
			   x: -10,
			   y: 70,
			   floating: true,
			   shadow: true
			  },
		  tooltip: {
			    formatter: function() {
				return ''+
				this.x +' : '+ this.y;
			    }
			   },
		  plotOptions: {
				column: {
				pointPadding: 0.2,
				borderWidth: 0
			        }
			       },
		  series: [
EOF
  
  for (my $i=2; $i<=$#{$h_data{"NM"}}; $i++) {
    print $h_data{"NM"}[$i];
    print ",\n" if($i!=$#{$h_data{"NM"}});
  }

  print <<"EOF";
			  ]});});
    </script>
  </head>
  <body>
    <div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>
    </br>
    <div id="container2" style="width: 800px; height: 400px; margin: 0 auto"></div>
  </body>
</html>

EOF
}

=pod

=head1 NAME

        plot3.pl - Build html report from bwasam2stat.pl output

=head1 SYNOPSIS

        plot3.pl statOUTPUT.txt > OUTPUT.html

=head1 OPTIONS

        -h,--help
	
=cut
