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

use lib '/app/tools/bookpub/lib';
use lib '/app/tools/common/lib';
use Common::Preference;
use Common::Assert;

use BookPub::DB::Item::BookProduct;
use BookPub::DB::Item::WatchListItem;
use BookPub::Market::WatchListItem;
use BookPub::Market::WatchListList;
use BookPub::DB::Item::BookProduct;
use BookPub::DB::Item::BookFormat;
use BookPub::DB::Item::BookFormatType;
use BookPub::DB::Item::OnixCode;

use RSApache::Response::XSLT;
use RSApache::Response::JSON;
use RSApache::Command::XMLCommand;

use base 'RSApache::Command::XMLCommand';

sub cmd      { return 'add_item'; }
sub area     { return 'market'; }
sub pageName { return 'Add to List'; }

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

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

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

    # We'll need to be able to accept book IDs too ...
    #
    my $productID = $self->getParam("ProductID");
    my $bookID    = $self->getParam("BookID");
    assert( $productID || $bookID );

    my $watchListID = $self->getParam("WatchListID");

    if ( $watchListID && $productID ) {

        # If we got a watch list id, that means we are trying to add this product to it.
        # First, let's make sure it's not already there.
        my $itemExists = BookPub::DB::Item::WatchListItem->Lookup( watch_list_id => $watchListID, product_id => $productID );
        if ($itemExists) {

            # It may be in the database, but in a deleted state.
            # In that case, we'll just reactivate it.
            #
            if ( $itemExists->deleted == 0 ) {
                $self->addMessageXML( type => "error", code => Common::FormObject::kErrDuplicate );
            } else {
                $itemExists->deleted(0);
                $itemExists->save();
                $self->addMessageXML( type => "success", code => Common::FormObject::kSuccessFormSubmit );
            }
        } else {

            my $watchListItem = BookPub::Market::WatchListItem->save( watchListID => $watchListID, productID => $productID );

            if ($watchListItem) {
                $self->addMessageXML( type => "success", code => Common::FormObject::kSuccessFormSubmit );
            }

        }
    } elsif ( $watchListID && $bookID ) {
        my $products = BookPub::DB::Item::BookProduct->GetAllByBookID($bookID);
        my $addedProductsFlag;
        while ( $products->hasNext ) {
            my $product        = $products->next();
            my $bookProductID  = $product->product_id;
            my $bookFormatID   = $product->book_format_id;
            my $bookFormat     = BookPub::DB::Item::BookFormat->Lookup( book_format_id => $bookFormatID );
            my $bookFormatType = BookPub::DB::Item::BookFormatType->Lookup( book_format_type_id => $bookFormat->book_format_type_id );
            my $codeItem       = BookPub::DB::Item::OnixCode->Lookup( onix_code_id => $bookFormatType->onix_code_id );
            my $description    = $codeItem->description();

            # We only want to add the monitored product types to the watch list.
            #
            if ( $description eq 'eBook' || $description eq 'Hardback' || $description eq 'Paperback / softback' ) {

                # If we got a watch list id, that means we are trying to add this product to it.
                # First, let's make sure it's not already there.
                my $itemExists = BookPub::DB::Item::WatchListItem->Lookup( watch_list_id => $watchListID, product_id => $bookProductID );
                if ($itemExists) {

                    # It may be in the database, but in a deleted state.
                    # In that case, we'll just reactivate it.
                    #
                    if ( $itemExists->deleted == 1 ) {
                        $itemExists->deleted(0);
                        $itemExists->save();
                        $self->addMessageXML( type => "success", code => Common::FormObject::kSuccessFormSubmit );
                        $addedProductsFlag = 1;
                    }
                } else {

                    my $watchListItem = BookPub::Market::WatchListItem->save( watchListID => $watchListID, productID => $bookProductID );

                    if ($watchListItem) {
                        $self->addMessageXML( type => "success", code => Common::FormObject::kSuccessFormSubmit );
                        $addedProductsFlag = 1;
                    }

                }
            }
        }

        if ( !$addedProductsFlag ) {
            $self->addMessageXML( type => "error", code => Common::FormObject::kErrFieldBlank );
        }
    }

    $self->{xml}{WatchListList} = BookPub::Market::WatchListList->new( type => 'simple' );

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

1;
