#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Raptor::SaleReport::ArtistsSummaryReport;
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 Client::Service;
use Client::Label;
use Period::QuarterPeriod;
use Period::MonthPeriod;
use Period::YearPeriod;

use lib '/app/tools/raptor/lib';
use Raptor::SaleReport::BaseReport;
use SaleReport::ReportLib;    # for pie charts

use lib '/app/tools/stats/lib';
use Stats::Sales;

use base 'Raptor::SaleReport::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 = Client::Service->GetAllServiceNames();

    # -------------------------
    # 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 $top_count = $self->_getTopArtists(
        tables      => \@tables,
        stats_obj   => $stats_obj,
        limit       => Raptor::SaleReport::BaseReport::RECORDS_PER_PAGE,
        rank_start  => $offset,
        metric_name => 'service',
        metric_list => $top_services,
        metric_hash => $service_names,
    );

    my $page_count = Raptor::SaleReport::BaseReport::_getPageCount( $top_count, $offset );

    $self->{Records}{Limit}      = Raptor::SaleReport::BaseReport::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,
        client_id    => Common::RSApp::GetClientID(),
        label_id     => $args{labelID},
        country_code => $args{countryCode},
        offset       => $offset,
    );

    return \%params;
}

1;
