#------------------------------------------------------------
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package BookPub::Catalog::Chart::Price;

use strict;
use warnings;

use File::Temp;
use File::Basename;
use Data::Dumper;
use Date::Calc;

use Date::Manip;

use lib '/usr/local/chart_director/lib';
use perlchartdir;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Client;

use lib '/app/tools/bookpub/lib';
use BookPub::Analytics::ProductMonitor::Data::MonitoredPrices;
use BookPub::DB::Item::BookProduct;
use BookPub::Catalog::Chart::Base;

use base 'BookPub::Catalog::Chart::Base';

sub _generateChartImage {
    my $self = shift;

    my $currencySymbol = Common::Client::Current()->Locale()->currencyFormat()->symbol();
    my $thousands      = Common::Client::Current()->Locale()->numericFormat()->thousands();
    my $decimal        = Common::Client::Current()->Locale()->numericFormat()->decimal();

    my $c = new XYChart( 640, 250 );

    # Set the plotarea at (30, 20) and of size 400 x 200 pixels
    $c->setPlotArea( 85, 20, 490, 200 );

    my $layer = $c->addLineLayer2();
    my $chartData = $self->_getProductChartData( $layer, $c );

    $layer->setLineWidth(2);

    # Force the y-axis to start at zero
    $c->yAxis->setLinearScale( 0, $perlchartdir::NoValue );
    $c->setClipping();

    # Set the colors
    $c->setColor( $perlchartdir::TextColor, BookPub::Catalog::Chart::Base::kTextColor );

    # Set the labels on the x axis.
    $c->xAxis()->setLabels( $chartData->{labels} );

    # Set the label step (allows us to limit number of labels displayed).
    $c->xAxis()->setLabelStep( $self->{_labelStep} );

    # Set the label gap (spacing between the label and the chart).
    $c->xAxis()->setLabelGap(BookPub::Catalog::Chart::Base::kLabelGap);

    # Grey out the area without data.
    $c->xAxis()->addZone( 0, $chartData->{firstNonEmptyElement}, BookPub::Catalog::Chart::Base::kNoDataColor );

    # Set the y axis label formatting.
    $c->yAxis()->setLabelFormat( $currencySymbol . "{value|2" . $thousands . $decimal . "}" );

    # Set the label gap (spacing between the label and the chart).
    $c->yAxis()->setLabelGap(BookPub::Catalog::Chart::Base::kLabelGap);

    my $chartName = $self->_getChartFilename();

    $self->{ChartSource} = sprintf( "%s/%s", BookPub::Catalog::Chart::Base::kChartImgDir, basename($chartName) );

    # Output the chart
    my $result = $c->makeChart($chartName);

    # Create the image map
    $self->setImageMap( imageMap => $c->getHTMLImageMap( '', '', 'title="%%{value|2}%%"' ) );
}

sub _getProductChartData {
    my $self  = shift;
    my $layer = shift;
    my $c     = shift;

    my $productID = $self->{_productID};
    my $offset    = $self->{_offset};
    my $dateStart = $self->{_dateStart};
    my $dateEnd   = $self->{_dateEnd};
    my $serviceID = $self->{_serviceID};

    my $chartData = BookPub::Analytics::ProductMonitor::Data::MonitoredPrices->new(
        productID => $productID,
        serviceID => $serviceID,
        dateStart => $dateStart,
        dateEnd   => $dateEnd
    );

    # Grab the "Current Price" Data
    #
    my $expectedPrices = $chartData->getExpectedPricesForProductID($productID);

    my $firstNonEmptyElement = 0;

    foreach ( @{$expectedPrices} ) {
        if ( $_ ne '' ) {
            last;
        }
        $firstNonEmptyElement++;
    }

    $_ ||= $perlchartdir::NoValue for @{$expectedPrices};

    $layer->addDataSet( $expectedPrices, $c->dashLineColor( 0x185285, $perlchartdir::DashLine ), "Set Price" );

    # Now grab the data for the services
    #
    my @serviceIDArray;
    if ( 'ARRAY' eq ref($serviceID) ) {
        @serviceIDArray = @$serviceID;
    } else {
        push( @serviceIDArray, $serviceID );
    }

    foreach (@serviceIDArray) {
        my $monitoredPrices = $chartData->getServiceDataForProductID( $_, $productID );

        # Need to substitue $perlchartdir::NoValue for empty values.
        # Otherwise, chart director treats tham like zeroes.
        #
        $_ ||= $perlchartdir::NoValue for @{$monitoredPrices};

        my $serviceColor = $self->_getServiceColor($_);

        $layer->addDataSet( $monitoredPrices, $serviceColor, "Monitored Price" );
    }

    # Grab the dates to use as labels
    #
    my $labels = $chartData->getDates();

    # Let's add ALL of the service colors to the legend
    #
    my @legend;

    my %serviceColorMap = $self->_getServiceColorMap();

    foreach my $id ( keys %serviceColorMap ) {
        my $legendItem = {};

        $legendItem->{ServiceID} = $id;
        $legendItem->{Color} = sprintf( "#%x", $serviceColorMap{$id} );

        push( @legend, $legendItem );
    }

    $self->{Legend}{Item} = \@legend;

    return { labels => $labels, firstNonEmptyElement => $firstNonEmptyElement };
}

###
1;    #
###
