#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package BookPub::Analytics::ServicesSummaryReport;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use Common::Assert;
use Common::XMLObject;
use Common::RSApp;
use Common::Util;

use lib '/app/tools/data_classes/lib';
use Period::QuarterPeriod;
use Period::MonthPeriod;
use Period::YearPeriod;

use lib '/app/tools/bookpub/lib';
use BookPub::Analytics::ReportLib;    # for pie charts
use BookPub::Stats::Sales;

use BookPub::Analytics::BaseReport;
use base 'BookPub::Analytics::BaseReport';

sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    my $stats_obj = $self->{_stats};
    return $self unless ($stats_obj);

    my $pie_count      = 0;
    my $base_image_url = "";
    my $data           = {};
    my @pies           = ();
    my @tables         = ();

    # --------------------------
    # grand total data
    # --------------------------

    # !!! Why do I have to pass the _same_ parameters to GetTotalMetric that I passed
    # !!! to it's constructor?
    #
    my $total_metric = {};
    $total_metric              = $stats_obj->GetTotalMetric( %{ $self->{_statParams} } );
    $self->{Totals}->{Revenue} = Common::Client::Current()->Locale()->formatMoney( $total_metric->Revenue );
    $self->{Totals}->{Units}   = Common::Client::Current()->Locale()->formatNumber( $total_metric->Units );

    # !!! Should transform the pie chart utils into an XMLObject.
    #
    # --------------------------
    # pie chart - top services
    # --------------------------
    my $top_services = $stats_obj->GetTopRanking( category => 'service' );
    my $service_names = $self->_getServiceMap();

    foreach my $service_ref (@$top_services) {
        next if ( $service_ref->Revenue <= 0 );

        push @{ $data->{labels} }, $service_names->{ $service_ref->ID };
        push @{ $data->{ids} },    $service_ref->ID;
        push @{ $data->{data} },   $service_ref->Revenue;
    }

    if ( $data && defined $data->{data} ) {

        # !!! In my opinion, this is a hack.
        # !!! We'll have to explore ways to avoid embedding urls in the pie chart.
        #
        my $image_link = $args{commandObj}->getParamsAsString( c => 1, service => 1 );
        $image_link .= '&' if ( length $image_link );
        $image_link .= 'c=s&service=';
        my $pie_href = GetPieChart(
            labels         => $data->{labels},
            data           => $data->{data},
            image_map_link => $image_link,
            label_ids      => $data->{ids},
        );
        my $xml_href = {};
        $xml_href->{PieName}        = "top_services";
        $xml_href->{ImageSrc}       = $pie_href->{image_src};
        $xml_href->{ImageMap}       = $pie_href->{image_map};
        $xml_href->{Legend}->{Item} = $pie_href->{legend_xml};

        push @pies, $xml_href;

        # we have to keep track of the image we created
        # and how many we've created
        $base_image_url = $pie_href->{image_src};
        $pie_count++;
    }

    # !!! Everything in the code above this point is identical in MainReport.pm...

    splice( @$top_services, 5 );

    # ---------------------------
    # table data - top 10 books
    # ---------------------------
    $self->_getTopProducts(
        tables      => \@tables,
        stats_obj   => $stats_obj,
        prod_type   => PROD_BOOK,
        limit       => 10,
        metric_name => 'service',
        metric_list => $top_services,
        metric_hash => $service_names,
    );

    # ---------------------------
    # table data - top 10 chapters
    # ---------------------------
    $self->_getTopProducts(
        tables      => \@tables,
        stats_obj   => $stats_obj,
        prod_type   => PROD_CHAP,
        limit       => 10,
        metric_name => 'service',
        metric_list => $top_services,
        metric_hash => $service_names,
    );

    # -------------------------
    # table data - top 10 authors
    # -------------------------
    $self->_getTopAuthors(
        tables      => \@tables,
        stats_obj   => $stats_obj,
        limit       => 10,
        metric_name => 'service',
        metric_list => $top_services,
        metric_hash => $service_names,
    );

    $self->{Pies}->{Pie}     = \@pies;
    $self->{Tables}->{Table} = \@tables;

    return $self;
}

1;
