#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package RPS::Report::Command::BaseReport;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::FormObject;
use Common::Util;
use Common::Date;
use Common::Assert;
use Common::Preference;
use RSApache::Response::XSLT;
use RSApache::Response::XSLTDownload;
use RSApache::Response::ReportDownload;

use lib '/app/tools/rps/lib';
use RPS::Command;
use RPS::Product::TypeList;

use base 'RPS::Command';

use constant kTemplate => "/app/tools/rps/templates/report/index.xsl";
# Note: the following constant is not used for display of a download/text
# report, that's handled by the RPS::Report base class
use constant kTextTemplate => "/app/tools/rps/templates/report/text/index.xsl";


#
# These are 'pure virtual' methods that should be overridden by subclasses
#


# Override to return the correct report XML object.
#
sub _getReport
{
    my ($self, $page) = @_;
    assert(0, "ERROR - _getReport must be overridden");
}


# Returns the first part of the download file name (ie 'Foo_Bar_')
sub _getFilenamePrefix
{
    my ($self) = @_;
    assert(0, "ERROR - _getFilenamePrefix must be overridden");
}


sub _getFilename
{
    my ($self) = @_;
    my $date = Common::Date->now();
    return $self->_getFilenamePrefix() . '_' . $date->asString( "%Y%m%d" ) . '.tsv';
}


sub execute
{
	my ($self) = @_;

	my $response = $self->SUPER::execute();
	return $response if $response;

    # !!! Do I want a 'download' flag?
    # !!! Do I want to re-use the 'form' object for both?
    # !!! Can we just do it with a different template?

    Common::Log::Debug("In the BaseReport:execute");
    my $download = $self->getParam('download');
    my $page = $self->getParam('page');

    if ($download)
    {
        # construct the filename
        #
        my $filename = $self->_getFilename();
        my $reportIterator = $self->_getIterator();
		$response = RSApache::Response::ReportDownload->new($reportIterator, $filename);
    }
    else
    {
    	$self->_getXML($self->{xml}, $page);
    	$self->{xml}{TypeList} = RPS::Product::TypeList->new(includeTrackProducts => 1);

        $response = RSApache::Response::XSLT->new($self->{xml}, kTemplate);
    }

    # Save preference for this command if it's for a US or CA mechanical report
    #
    my @usMechCommands = ('tracks_no_timings', 'license_overage', 'tracks_no_licenses', 'inactive_licenses', 'license_accrual', 'publisher_mailing', 'mechanical_reserve_pipeline');
    for (@usMechCommands)
    {
        if ($_ eq $self->cmd())
        {
            Common::Preference::SetPersistentCookie( 'mech_report_pref' => 'us');
            Common::Preference::StoreCookies();
        }
        # Might as well check for the Canadian version while we're looping.
        if ('ca_' . $_ eq $self->cmd())
        {
            Common::Preference::SetPersistentCookie( 'mech_report_pref' => 'ca');
            Common::Preference::StoreCookies();
        }
    }

	return $response;
}


sub _getXML
{
    my ($self, $xml, $page) = @_;
    #my $page = $self->getParam('page');

    $xml->{Report} = $self->_getReport($page);
}

sub _getIterator
{
    my ($self, $xml, $page) = @_;

    $xml->{Report} = $self->_getReport("iterator");

}


1;
