#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Raptor::SaleReport::AlbumsSummaryReport;
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 $pie_count      = 0;
    my $base_image_url = "";
    my $data           = {};
    my @pies           = ();
    my @tables         = ();

    # --------------------------
    # pie chart - top services
    # --------------------------
    my $top_services = $stats_obj->GetTopRanking( category => 'service', product_type => 'A' );
    my $service_names = Client::Service->GetAllServiceNames();

    foreach my $service_ref (@$top_services) {
        next if ( $service_ref->Revenue <= 0 );

        push @{ $data->{labels} }, $service_names->{ $service_ref->ID };
        push @{ $data->{ids} },    $service_ref->ID;
        push @{ $data->{data} },   $service_ref->Revenue;
    }

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

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

    # --------------------------
    # pie chart - top formats
    # --------------------------
    my $top_formats = $stats_obj->GetTopRanking( category => 'format' );
    my $format_names = $stats_obj->FormatMap();
    $data = {};

    foreach my $format_ref (@$top_formats) {
        next if ( $format_ref->Revenue <= 0 );

        push @{ $data->{labels} }, $format_names->{ $format_ref->ID };
        push @{ $data->{ids} },    $format_ref->ID;
        push @{ $data->{data} },   $format_ref->Revenue;
    }

    if ( $data && defined $data->{data} ) {
        my $image_link = $args{commandObj}->getParamsAsString( c => 1 );
        $image_link .= '&' if ( length $image_link );
        $image_link .= 'c=f&format=';
        my $pie_href = GetPieChart(
            labels         => $data->{labels},
            data           => $data->{data},
            image_map_link => $image_link,
            label_ids      => $data->{ids},
            image_src      => $base_image_url,
            chart_num      => $pie_count,
        );
        my $xml_href = {};
        $xml_href->{PieName}        = "top_formats";
        $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 dynamic images
        $base_image_url = $pie_href->{image_src};
        $pie_count++;
    }

    # -------------------------
    # table data - top albums
    # -------------------------

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

    my $top_count = $self->_getTopProducts(
        tables      => \@tables,
        stats_obj   => $stats_obj,
        prod_type   => 'A',
        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->{Pies}->{Pie}         = \@pies;
    $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},
        product_type => 'A',
        offset       => $offset,
    );

    return \%params;
}

1;
