#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Raptor::SaleReport::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 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 $stats_obj = $self->{_stats};
    return $self unless ($stats_obj);

    my $service_id  = $args{serviceID};
    my $format_type = $args{formatType};

    my $service_names = Client::Service->GetAllServiceNames();
    $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 albums
    # ---------------------------
    $self->_getTopProducts(
        tables    => \@tables,
        stats_obj => $stats_obj,
        prod_type => 'A',
        limit     => 25
    );

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

    # -------------------------
    # table data - top 10 artists
    # -------------------------
    $self->_getTopArtists(
        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,
        client_id    => Common::RSApp::GetClientID(),
        label_id     => $args{labelID},
        service_id   => $args{serviceID},
        format_type  => $args{formatType},
        country_code => $args{countryCode},
    );

    return \%params;
}

1;
