#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package BookPub::Analytics::ServiceFormatDetailReport;
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::Client;

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

    my $service_names = $self->_getServiceMap();
    $self->{ProductInfo}->{ServiceName} = $service_names->{$service_id};
    $self->{ProductInfo}->{ServiceID}   = $service_id;

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

    my $data   = {};
    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 );

    # ---------------------------
    # table data - top 10 books
    # ---------------------------
    $self->_getTopProducts(
        tables    => \@tables,
        stats_obj => $stats_obj,
        prod_type => PROD_BOOK,
        limit     => 25
    );

    # ---------------------------
    # table data - top 10 chapters
    # ---------------------------
    $self->_getTopProducts(
        tables    => \@tables,
        stats_obj => $stats_obj,
        prod_type => PROD_CHAP,
        limit     => 25
    );

    # -------------------------
    # table data - top 10 authors
    # -------------------------
    $self->_getTopAuthors(
        tables    => \@tables,
        stats_obj => $stats_obj,
        limit     => 25,
    );

    $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},
        service_id   => $args{serviceID},
        format_type  => $args{formatType},
        country_code => $args{countryCode},
        user_id      => $args{commandObj}->_getUser()->UserID(),
    );

    return \%params;
}

1;
