#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::Market::WatchList::Static;
use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::RSApp;
use Common::Util;
use Common::Assert;
use Common::FormObject;
use BookPub::DB::Item::WatchList;

use base 'BookPub::Market::WatchList';

# Don't overload these
sub Public  { 1 }
sub Dynamic { 1 }

# Must be overloaded
sub Name        { die "Must be overloaded" }
sub Description { die "Must be overloaded" }
sub GlobalID    { die "Must be overloaded" }

# Criteria that can be overloaded
sub PublicationDate { }
sub Format          { }
sub Rank            { }
sub Market          { }
sub Author          { }
sub ImprintID       { }
sub PublisherID     { }

sub StaticWatchLists {
    qw(
      BookPub::Market::WatchList::Static::NewReleases
      BookPub::Market::WatchList::Static::NYTBestSellers
      BookPub::Market::WatchList::Static::Top20Ebooks
      BookPub::Market::WatchList::Static::Top20FreeEbooks
      BookPub::Market::WatchList::Static::Top20Physical
    );
}

sub CleanWatchLists {
    my $class = shift;

    $class->_requireLists();

    $class->_removeExtraLists();

    $class->_updateCurrentLists();
}

sub _requireLists {
    my $self  = shift;
    my @lists = $self->StaticWatchLists();

    foreach my $type (@lists) {
        eval "require $type";
        die $@ if ($@);
    }

}

sub _removeExtraLists {
    my $self  = shift;
    my @lists = $self->StaticWatchLists();
    my @globalID;

    foreach my $list (@lists) {
        push( @globalID, $list->GlobalID );
    }

    BookPub::DB::Item::WatchList->DeleteByGlobalID( global_id => \@globalID );
}

sub _updateCurrentLists {
    my $self  = shift;
    my @lists = $self->StaticWatchLists();

    foreach my $list (@lists) {
        $list->_updateList();
    }
}

sub _updateList {
    my $self = shift;

    my $dbitem = BookPub::DB::Item::WatchList->LookupByGlobalID( $self->GlobalID );

    unless ($dbitem) {
        $dbitem = BookPub::DB::Item::WatchList->Create();
    }

    $dbitem->name( $self->Name() );
    $dbitem->description( $self->Description() );
    $dbitem->global_id( $self->GlobalID() );
    $dbitem->public( $self->Public() );
    $dbitem->dynamic( $self->Dynamic() );

    $dbitem->publication_date( defined( $self->PublicationDate() ) ? $self->PublicationDate() : undef );
    $dbitem->format( defined( $self->Format )                      ? $self->Format            : undef );
    $dbitem->rank( defined( $self->Rank() )                        ? $self->Rank              : undef );
    $dbitem->market( defined( $self->Market() )                    ? $self->Market            : undef );
    $dbitem->author( defined( $self->Author() )                    ? $self->Author            : undef );
    $dbitem->imprint_id( defined( $self->ImprintID() )             ? $self->ImprintID         : undef );
    $dbitem->publisher_id( defined( $self->PublisherID() )         ? $self->PublisherID       : undef );
    $dbitem->price( defined( $self->Price() )                      ? $self->Price             : undef );

    $dbitem->save();
}

###
1;    #
###
