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

use lib '/app/tools/bookpub/lib';
use BookPub::Market::WatchList;
use BookPub::Market::WatchListList;
use BookPub::Market::WatchListItemList;
use BookPub::Catalog::ImprintList;
use BookPub::DB::Item::ProductMonitorAlert;
use BookPub::DB::Item::WatchList;

use base 'BookPub::Command';

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

sub cmd      { return 'main'; }
sub area     { return 'market'; }
sub pageName { return 'Market Home'; }

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

use constant kDefaultPageSize   => 25;
use constant kCookiePageSize    => 'bookpub_market_main_page_size';
use constant kCookieWatchListID => 'bookpub_market_main_watch_list_id';

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

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

    my $pageSize =
      Common::Util::returnFirstDefined( $self->getParam('pageSize'), Common::Preference::GetValue(kCookiePageSize), kDefaultPageSize );
    if ( 'all' eq $pageSize ) {

        # 'all' is no longer a valid option.
        #
        $pageSize = kDefaultPageSize;
    }

    my $page = $self->getParam('page');
    $page = 1 unless $page;

    my $watchListID = Common::Util::returnFirstDefined( $self->getParam('WatchListID'), Common::Preference::GetValue(kCookieWatchListID) );

    my $watchListDB;

    if ($watchListID) {
        $watchListDB = BookPub::DB::Item::WatchList->Lookup( watch_list_id => $watchListID );
    }

    # Let's make sure that watch list is still around.
    #
    if ($watchListDB) {
        $self->{xml}{WatchList} = BookPub::Market::WatchList->new( watchListID => $watchListID );
    } else {

        # Clear out the bad ID.
        undef $watchListID;

        # We'll show the first one this user has access to by default (if there is one).
        my $userID = Common::RSApp::GetActiveUserID();
        my $dbItem = BookPub::DB::Item::WatchList->GetFirstWithAccess($userID);
        while ( $dbItem->hasNext() ) {
            my $listDBItem = $dbItem->next();
            $watchListID = $listDBItem->watch_list_id;
            $self->{xml}{WatchList} = BookPub::Market::WatchList->new( dbItem => $listDBItem );
        }
    }

    if ($watchListID) {
        my $listPage     = $page;
        my $listPageSize = $pageSize;

        #die $pageSize;

        $self->{xml}{WatchList}{WatchListItemList} = BookPub::Market::WatchListItemList->new(
            watchListID => $watchListID,
            currentPage => $listPage,
            pageSize    => $listPageSize,
        );
    }

    $self->{xml}{PageSize} = $pageSize;

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

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

    $self->{xml}{ActiveAlertCount} = BookPub::DB::Item::ProductMonitorAlert->CountActiveAlerts();

    Common::Preference::SetPersistentCookie( kCookiePageSize,    $pageSize );
    Common::Preference::SetPersistentCookie( kCookieWatchListID, $watchListID );
    Common::Preference::StoreCookies();

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

1;
