#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Catalog::Command::Search;
use strict;
use warnings;

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

use BookPub::Catalog::ImprintList;
use BookPub::SearchCommand;
use BookPub::Catalog::Command::ShowBook;
use BookPub::Catalog::Command::ShowBookProduct;

use base 'BookPub::SearchCommand';

sub cmd      { return 'search'; }
sub area     { return 'catalog'; }
sub pageName { return 'Search'; }

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

sub pageSizePrefName { 'catalog_search_pagesize' }

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

    my $pagesize = $self->setPagesize();

    my $response = $self->SUPER::execute(
        dbClass  => 'BookPub::DB::Item::Book',
        xmlClass => 'BookPub::Catalog::Book::WithAuthor::WithProductCount',
        xmlTag   => 'Book',
        pageSize => $pagesize
    );
    return $response if $response;

    $self->{xml}{BookCount} = $self->totalCount();

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

    # If we only have a single result, attempt to redirect straight to the
    # book or product.
    # Basically, if the search term was an ISBN, redirect straight to the product.
    # Otherwise, assume it's a book search and redirect to that instead.
    #
    # I use the term 'redirect' loosely.  We're not going to return a REDIRECT to the browser, but
    # instead just instantiate and execute the corresponding Command internally.
    #
    if ( 1 == $self->totalCount() ) {
        my $query = $self->getParam('Query');

        my $bookList = $self->{xml}{Book};
        my $theBook  = $bookList->[0];
        my $bookID   = $theBook->BookID();

        if ( $query =~ /^\d\d\d\d\d\d\d\d\d\w$/ ) {
            my $products = BookPub::DB::Item::BookProduct->GetAllByISBN10($query);

            # If there's only one, redirect to it.
            # Otherwise, just go to the book page.
            if ( $products->size() == 1 ) {
                my $product    = $products->next();
                my $productID  = $product->product_id();
                my $newCommand = BookPub::Catalog::Command::ShowBookProduct->new( ProductID => $productID );
                return $newCommand->execute();
            } else {
                my $newCommand = BookPub::Catalog::Command::ShowBook->new( BookID => $bookID );
                return $newCommand->execute();
            }
        } elsif ( $query =~ /^\d\d\d\d\d\d\d\d\d\d\d\d\d$/ ) {
            my $products = BookPub::DB::Item::BookProduct->GetAllByISBN13($query);

            # If there's only one, redirect to it.
            # Otherwise, just go to the book page.
            if ( $products->size() == 1 ) {
                my $product    = $products->next();
                my $productID  = $product->product_id();
                my $newCommand = BookPub::Catalog::Command::ShowBookProduct->new( ProductID => $productID );
                return $newCommand->execute();
            } else {
                my $newCommand = BookPub::Catalog::Command::ShowBook->new( BookID => $bookID );
                return $newCommand->execute();
            }
        } else {
            my $newCommand = BookPub::Catalog::Command::ShowBook->new( BookID => $bookID );
            return $newCommand->execute();
        }
    } else {
        $response = RSApache::Response::XSLT->new( $self->{xml}, kTemplate );
    }

    return $response;
}

1;
