#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Admin::Mcps::Command::Show;

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

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

use lib '/app/tools/admin/lib';
use Admin::Mcps::DefaultRoyaltyRateList;
use Admin::Mcps::ExportAllowanceList;
use Admin::Mcps::ExportUpliftHash;
use Admin::Mcps::ExportUpliftCountryList;
use Admin::Mcps::RetentionRateList;
use Admin::Mcps::SalesPricingMode;
use Admin::Command;
use base 'Admin::Command';

use constant kTemplate => '/app/tools/admin/templates/mcps/show.xsl';

sub cmd  { return "show"; }
sub area { return "mcps"; }

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

    my $clientID = Common::RSApp::GetClientID();

    # Give any inherited methods the first crack at this.
    # (This checks for login and basic access permissions).
    #
    my $response = $self->SUPER::execute();
    return $response if $response;

    # First check for the country_code param.
    #
    my $countryCode = $self->getParam('country_code');

    # If one wasn't passed, then check for a stored preference.
    #
    if ( !$countryCode ) {
        $countryCode = Common::Preference::Get("mcps_admin_country_code");
        if ($countryCode) {
            $countryCode = $countryCode->value();
        }
    }

    # Let's make sure that there is at least one entry for this country.
    # If not, we'll just show the entries for all countries.
    if ($countryCode) {
        my $countryTest = RPS::DB::Item::McpsExportUplift->Lookup( country_code => $countryCode );

        if ( !$countryTest ) {
            $countryCode = '';
        }
    }

    $self->{xml}{McpsSettings}{DefaultRoyaltyRateList}  = Admin::Mcps::DefaultRoyaltyRateList->new();
    $self->{xml}{McpsSettings}{RetentionRateList}       = Admin::Mcps::RetentionRateList->new();
    $self->{xml}{McpsSettings}{ExportAllowanceList}     = Admin::Mcps::ExportAllowanceList->new();
    $self->{xml}{McpsSettings}{ExportUpliftList}        = Admin::Mcps::ExportUpliftHash->new( countryCode => $countryCode );
    $self->{xml}{McpsSettings}{ExportUpliftCountryList} = Admin::Mcps::ExportUpliftCountryList->new();
    $self->{xml}{McpsSettings}{SalesPricingMode}        = Admin::Mcps::SalesPricingMode->new( clientID => $clientID );
    $self->{xml}{McpsSettings}{CountryCode}             = $countryCode;

    $self->{xml}{TypeList}    = RPS::Product::TypeList->new();
    $self->{xml}{CountryList} = RPS::CountryList->new();

    # Save last args as preferences, if they were specified
    #
    Common::Preference::SetPersistentCookie( 'mcps_admin_country_code' => $countryCode );
    Common::Preference::StoreCookies();

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

1;

