#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package BookPub::Market::Command::Report::Product;
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::Market::Command::Report';

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

sub cmd { return 'report'; }

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

sub _getQueryParams {
    my ( $self, $hashRef ) = @_;
    $self->{xml}{Query}{productID} = $self->getParam('productID');
    die "ERROR - missing productID parameter" unless $self->{xml}{Query}{productID};

    # No cookies for you!
    #
    $self->{xml}{Query}{pageSize}  = Common::Util::returnFirstDefined( $self->getParam('pageSize'),  kDefaultPageSize );
    $self->{xml}{Query}{view}      = Common::Util::returnFirstDefined( $self->getParam('view'),      kDefaultView );
    $self->{xml}{Query}{issue}     = Common::Util::returnFirstDefined( $self->getParam('issue'),     kDefaultIssue );
    $self->{xml}{Query}{serviceID} = Common::Util::returnFirstDefined( $self->getParam('serviceID'), undef );
    $self->{xml}{Query}{page}      = Common::Util::returnFirstDefined( $self->getParam('page'),      1 );
}

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

    $self->SUPER::_getXMLItems();

    $self->{xml}{BookProduct} = BookPub::Catalog::Product::Book::WithFormat->new( productID => $self->{xml}{Query}{productID} );
    $self->{xml}{Book} = BookPub::Catalog::Book::WithAuthor->new( bookID => $self->{xml}{BookProduct}->BookID() );

    # A bit of a hack, but I'm going to 'overload' the pageName XML here in execute.
    # !!! We can't do this in the regular pageName method, since that is called before execute() is called..
    #
    my $pageName = 'Alert Report';
    my $title    = $self->{xml}{Book}->Title();
    $title .= " : " . $self->{xml}{Book}->Subtitle() if $self->{xml}{Book}->Subtitle();

    my $format;
    if ( $self->{xml}{BookProduct}{ProductFormat}{BookFormatSubtype} ) {
        $format = $self->{xml}{BookProduct}{ProductFormat}{BookFormatSubtype}{Description};
    } else {
        $format = $self->{xml}{BookProduct}{ProductFormat}{BookFormatType}{Description};
    }

    $pageName .= "  -  $title ($format)";
    $self->{xml}{PageName} = $pageName;
}

1;
