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

use lib '/app/tools/bookpub/lib';
use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Client;
use RSApache::Response::XSLT;
use BookPub::Command;
use BookPub::Catalog::Region::RegionWithCountriesList;
use BookPub::Catalog::Product::Book::WithPricing;
use BookPub::Catalog::Product::Book::WithProductMonitoring;
use BookPub::Catalog::Book::FullDisplay;
use BookPub::Catalog::ImprintList;
use BookPub::Catalog::PublisherList;
use BookPub::Catalog::ServiceList;
use BookPub::Catalog::ContributorRoleList;
use BookPub::Price::PriceTypeList;

use base 'BookPub::Command';

sub cmd      { return 'show_book_product'; }
sub area     { return 'catalog'; }
sub pageName { return 'View Book Product'; }

use constant kTemplate => "/app/tools/bookpub/templates/catalog/show_book_product.xsl";

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

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

    my $productID = $self->getParam('ProductID');
    assert($productID);

    # We'll need the book and the book product info.
    #
    my $product;

    my $client = Common::Client::Current();
    if ( $client->isClientType(Common::Client::kProductMonitoring) ) {
        $product = BookPub::Catalog::Product::Book::WithProductMonitoring->new( productID => $productID );
    } else {
        $product = BookPub::Catalog::Product::Book::WithPricing->new( productID => $productID );
    }

    # We have no business being here if there isn't a book product with that product id...
    #
    die("ERROR - No book_product found with productID $productID") unless $product;

    my $bookID = $product->BookID();
    my $book = BookPub::Catalog::Book::FullDisplay->new( bookID => $bookID, readOnly => 1 );

    $self->{xml}{PriceTypes} = BookPub::Price::PriceTypeList->new();
    $self->{xml}{Regions}    = BookPub::Catalog::Region::RegionWithCountriesList->new( productID => $productID );

    $self->{xml}{Product} = $product;
    $self->{xml}{Book}    = $book;

    $self->{xml}{ImprintList}         = BookPub::Catalog::ImprintList->new();
    $self->{xml}{PublisherList}       = BookPub::Catalog::PublisherList->new();
    $self->{xml}{ServiceList}         = BookPub::Catalog::ServiceList->new();
    $self->{xml}{ContributorRoleList} = BookPub::Catalog::ContributorRoleList->new();

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

1;
