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

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

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 $format_type = $args{formatType};

    my $format_names = $stats_obj->FormatMap();
    $self->{ProductInfo}->{FormatName} = $format_names->{$format_type};
    $self->{ProductInfo}->{FormatType} = $format_type;

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

    # --------------------------
    # grand total data
    # --------------------------
    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 );

    # ---------------------------------
    # pie chart - revenue by service
    # ---------------------------------
    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 @{ $pie_data->{labels} }, $service_names->{ $service_ref->ID };
        push @{ $pie_data->{ids} },    $service_ref->ID;
        push @{ $pie_data->{data} },   $service_ref->Revenue;
    }

    if ( $pie_data && defined $pie_data->{data} ) {
        my $image_link = $args{commandObj}->getParamsAsString( c => 1, service => 1 );
        $image_link .= '&' if ( length $image_link );
        $image_link .= 'c=sf&service=';

        my $pie_href = GetPieChart(
            labels         => $pie_data->{labels},
            data           => $pie_data->{data},
            image_map_link => $image_link,
            label_ids      => $pie_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++;
    }

    # $top_services is really ALL services at this point so narrow it down to top 5
    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;
}

# Overridden from base report class
#
sub _initStatsParams {
    my ( $self, %args ) = @_;

    # This hash of parameters gets re-used several times.
    # !!! Why, I don't really understand.  That needs to be fixed.
    #
    my %params = (
        period_id    => $self->{_period}->PeriodID,
        period_type  => $self->{_period}->PeriodType,
        end_period_id => $args{endPeriodParam},
        client_id    => Common::RSApp::GetClientID(),
        imprint_id   => $args{imprintID},
        publisher_id => $args{publisherID},
        format_type  => $args{formatType},
        country_code => $args{countryCode},
        user_id      => $args{commandObj}->_getUser()->UserID(),
    );

    return \%params;
}

1;
