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

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

use lib '/app/tools/bookpub/lib';
use BookPub::Stats::Sales;
use BookPub::Analytics::ReportLib;    # for pie charts
use BookPub::DB::Item::Service;
use BookPub::DB::Item::Imprint;
use BookPub::Analytics::BaseReport;
use base 'BookPub::Analytics::BaseReport';

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

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

    my $offset = $self->_getPageOffset( $args{pageNum} );

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

    my @tables        = ();
    my $top_services  = $stats_obj->GetTopRanking( category => 'service' );
    my $service_names = $self->_getServiceMap();

    # -------------------------
    # table data - top artists
    # -------------------------

    # $top_services is really ALL services at this point so narrow it down to top 5
    splice( @$top_services, 5 );

    my $limit;
    if ( $args{longPage} == 1 ) {
        $limit = MAX_RECORDS;
    } else {
        $limit = RECORDS_PER_PAGE;
    }
    my $top_count = $self->_getTopAuthors(
        tables      => \@tables,
        stats_obj   => $stats_obj,
        rank_start  => $offset,
        metric_name => 'service',
        metric_list => $top_services,
        metric_hash => $service_names,
        limit       => $limit,
    );

    my $page_count = BookPub::Analytics::BaseReport::_getPageCount( $top_count, $offset );

    if ( $args{longPage} == 1 ) {
        $self->{Records}{Limit}      = MAX_RECORDS;
        $self->{Records}{MaxPageNum} = 1;
    } else {
        $self->{Records}{Limit}      = RECORDS_PER_PAGE;
        $self->{Records}{MaxPageNum} = $page_count;
    }
    $self->{Tables}->{Table} = \@tables;
    return $self;
}

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

    my $offset = $self->_getPageOffset( $args{pageNum} );

    # 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},
        country_code => $args{countryCode},
        offset       => $offset,
        user_id      => $args{commandObj}->_getUser()->UserID(),
    );

    return \%params;
}

1;
