#!/usr/bin/perl -w

use warnings;
use strict;

use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
use POSIX qw/strftime/;
use CGI::Pretty qw[:standard];

use czartool::CzarDb;
use czartool::Gpc1Db;
use czartool::Plotter;
use czartool::StageMetrics;

package czartool::Metrics;

###########################################################################
#
# Constructor
#
###########################################################################
sub new {

    my $class = shift;
    my $self = {
        config => shift,
        verbose => shift,
        save_temps => shift,
    };

    $self->{gpc1Db} = $self->{config}->getGpc1Instance();
    $self->{czarDb} = $self->{config}->getCzarDbInstance();
    $self->{baseDir} = $self->{config}->getMetricsSaveLocation();

    # create path, dir
    mkdir($self->{baseDir}, 0777);

    # instantiate a plotter object
    $self->{plotter} = czartool::Plotter->new_file(
            $self->{config},
            ".",
            $self->{save_temps});

    bless $self, $class;
    return $self;
}

###########################################################################
#
# Writes HTML intro
#
###########################################################################
sub createHtml {
    my ($self, $title) = @_; 

    open (FILE, ">$self->{path}/index.html");
    $self->{htmlFile} = *FILE;

    my $htmlFile = $self->{htmlFile};
    print $htmlFile "<html>\n"; 
    print $htmlFile "<head>\n";
    print $htmlFile "<title>$title</title>\n";
    print $htmlFile "<a name=\"top\"></a>\n";
    print $htmlFile "<h1 align=\"middle\">$title</h1>\n";
}

###########################################################################
#
# Creates summary plots for period 
#
###########################################################################
sub createSummaryPlots {
    my ($self) = @_;

    my $htmlFile = $self->{htmlFile};

    print $htmlFile "<a name=\"summaryplot\"></a>\n";
    print $htmlFile "<h2  align=\"middle\">Summary plots <a href=\"#top\">(top)</a></h1>\n";
    $self->{plotter}->createTimeSeries("all_stdscience_labels", undef, $self->{begin}, $self->{end}, 0, 0, 0);
    print $htmlFile "<img src=\"czarplot_linear_all_stdscience_labels_all_stages_t.png\" alt=\"timeseries\" />\n";
    $self->{plotter}->createHistogram("all_stdscience_labels", $self->{begin}, $self->{end}, 0, 0, 0);
    print $htmlFile "<img src=\"czarplot_linear_all_stdscience_labels_all_stages_h.png\" alt=\"histogram\" />\n";
    $self->{plotter}->createRateTimeSeries("all_stdscience_labels", undef, $self->{begin}, $self->{end}, undef, 0);
    print $htmlFile "<img src=\"czarplot_linear_all_stdscience_labels_all_stages_r.png\" alt=\"rate\" />\n";
    $self->{plotter}->plotStorageTimeSeries($self->{begin}, $self->{end});
    print $htmlFile "<img src=\"czarplot_cluster.png\" alt=\"storage plot not available\" />\n";
}

###########################################################################
#
# Creates magic mask plots for provided period 
#
###########################################################################
sub createMaskPlots {
    my ($self, $begin, $end) = @_;

    my $htmlFile = $self->{htmlFile};

    print $htmlFile "<a name=\"mask\"></a>\n";
    print $htmlFile "<h2  align=\"middle\">Magic mask fraction <a href=\"#top\">(top)</a></h1>\n";

    $self->{plotter}->plotMagicMaskFraction($begin, $end);
    print $htmlFile "<img src=\"czarplot_magic_mask_fraction_h.png\" alt=\"\" />\n";
    print $htmlFile "<img src=\"czarplot_magic_mask_fraction_d.png\" alt=\"\" />\n";
}

###########################################################################
#
# Writes HTML intro
#
###########################################################################
sub finishHtml {
    my ($self) = @_;

    my $htmlFile = $self->{htmlFile};

    print $htmlFile "<br>\n";
    print $htmlFile "</body>\n";
    print $htmlFile "</html>\n";
    close($htmlFile);
}
1;
