#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::Market::WatchListItem;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::RSApp;
use Common::Util;
use Common::Assert;
use BookPub::DB::Item::BookProduct;
use BookPub::DB::Item::WatchListItem;
use BookPub::DB::Item::ProductMonitorAlert;
use BookPub::Catalog::Product::Book::WithPricing;
use BookPub::Catalog::Book::WithAuthor;

use base 'Common::FormObject';

sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    my %properties;
    if ( $args{dbItem} ) {
        $self->_propertiesFromDBItem( \%properties, $args{dbItem} );
    } elsif ( $args{watchListItemID} ) {
        $self->_propertiesFromDB( \%properties, $args{watchListItemID} );
    } else {
        $self->_propertiesFromDefaults( \%properties );
    }

    $self->_initProperties( \%properties );

    return $self;
}

sub _propertiesFromDB {
    my ( $self, $hrProperties, $watchListItemID ) = @_;

    my $dbItem = BookPub::DB::Item::WatchListItem->Lookup( watch_list_item_id => $watchListItemID );

    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

sub _propertiesFromDBItem {
    my ( $self, $hrProperties, $dbItem ) = @_;
    assert($dbItem);

    $hrProperties->{watch_list_item_id} = $dbItem->watch_list_item_id;
    $hrProperties->{watch_list_id}      = $dbItem->watch_list_id;
    $hrProperties->{product_id}         = $dbItem->product_id;
    $hrProperties->{deleted}            = $dbItem->deleted;
    $hrProperties->{average_rank}       = $dbItem->average_rank;
    $hrProperties->{service_rank}       = $dbItem->service_rank;
}

sub _initProperties {
    my ( $self, $props ) = @_;

    my $productID = $props->{product_id};

    $self->{WatchListItemID} = $props->{watch_list_item_id};
    $self->{WatchListID}     = Common::FormObject::Scalar->new( value => $props->{watch_list_id} );
    $self->{ProductID}       = Common::FormObject::Scalar->new( value => $productID );
    $self->{Deleted}         = Common::FormObject::Scalar::Boolean->new( value => $props->{deleted} );
    $self->{AverageRank}     = Common::FormObject::Scalar::Integer->new( value => $props->{average_rank} );
    $self->{ServiceRank}     = Common::FormObject::Scalar::Integer->new( value => $props->{service_rank} );

    my $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::WithAuthor->new( bookID => $bookID );

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

    my @productIDs = $productID;
    if ( BookPub::DB::Item::BookProduct->IsEBook( productID => $self->ProductID() ) ) {
        my %additionalArgs;
        if ( $product->{DefaultPrimary} && $product->{DefaultPrimary}->MarketID() ) {
            my $marketID     = $product->{DefaultPrimary}->MarketID();
            my $currencyCode = $product->{DefaultPrimary}{Currency}{CurrencyCode};
            my $price        = $product->getCurrentPrice( $marketID, $currencyCode );

            $additionalArgs{marketID}     = $marketID;
            $additionalArgs{currencyCode} = $currencyCode;
            $additionalArgs{price}        = $price;
        }
        @productIDs = BookPub::DB::Item::BookProduct->GetProductIDsOfSimilarProducts( $productID, %additionalArgs );
    }
    my $alertCount = BookPub::DB::Item::ProductMonitorAlert->CountActiveAlertsForProductIDs( \@productIDs );
    $self->{AlertCount} = $alertCount;

}

sub _propertiesFromDefaults {
    my ( $self, $hrProperties ) = @_;
}

sub save {
    my ( $self, %args ) = @_;

    my $watchListItemDBObj = BookPub::DB::Item::WatchListItem->Create();

    $watchListItemDBObj->watch_list_id( $args{watchListID} );
    $watchListItemDBObj->product_id( $args{productID} );

    $watchListItemDBObj->save();

    return $watchListItemDBObj;

}

sub delete {

    # !!! We only want to "really" delete this entry if we are deleting the whole watch list. !!!
    # !!! Otherwise, just set the deleted flag in the DB so that we can undo it if needed.    !!!

    my ($self) = @_;

    # delete this item
    #
    my $item = BookPub::DB::Item::WatchListItem->Lookup( watch_list_item_id => $self->WatchListItemID() );
    $item->delete();

    return 1;
}

###
1;    #
###
