#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package BookPub::Market::Command::Report;
use strict;
use warnings;

use Data::Dumper;

use lib '/app/tools/bookpub/lib';
use BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlertList;
use BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlertTypeList;
use BookPub::RDAS::Form::ProductMonitor::ProductMonitorItem;
use BookPub::Catalog::ServiceList::AlertsOnly;
use BookPub::Catalog::Book::WithAuthor;
use BookPub::Catalog::Product::Book::WithFormat;

use base 'BookPub::Command';

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

sub area     { return 'market'; }
sub pageName { 'Alert Report'; }

use constant kDefaultPageSize => 25;
use constant kDefaultView     => 'current';
use constant kDefaultIssue    => 'all';

use constant kCookiePageSize  => 'bookpub_market_report_page_size';
use constant kCookieServiceID => 'bookpub_market_report_page_service_id';
use constant kCookieView      => 'bookpub_market_report_view';
use constant kCookieIssue     => 'bookpub_market_report_issue';

use constant kTemplate => "/app/tools/bookpub/templates/market/report.xsl";

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

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

    # Call a virtual method to fill in the {xml}{Query} hash.
    #
    $self->_getQueryParams();

    $self->_getXMLItems();

    $self->_setCookies();

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

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

    $self->{xml}{LastUpdate} =
      BookPub::RDAS::Form::ProductMonitor::ProductMonitorItem->LastUpdated( serviceID => $self->{xml}{Query}{serviceID} );

    $self->{xml}{ProductMonitorAlertTypes} = new BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlertTypeList(
        issue     => $self->{xml}{Query}{issue},
        view      => $self->{xml}{Query}{view},
        serviceID => $self->{xml}{Query}{serviceID},
        productID => $self->{xml}{Query}{productID},
    );

    $self->{xml}{ProductMonitorAlerts} = new BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlertList(
        currentPage => $self->{xml}{Query}{page},
        issue       => $self->{xml}{Query}{issue},
        pageSize    => $self->{xml}{Query}{pageSize},
        view        => $self->{xml}{Query}{view},
        serviceID   => $self->{xml}{Query}{serviceID},
        productID   => $self->{xml}{Query}{productID},
    );

    $self->{xml}{ServiceList} = new BookPub::Catalog::ServiceList::AlertsOnly();
    $self->{xml}{PageSize}    = $self->{xml}{Query}{pageSize};
}

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

    # Default - do nothing.
}

1;
