#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::PhysicalPrice;
use strict;
use warnings;

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

use base 'BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert';

sub _getDBItemClass { 'BookPub::DB::Item::ProductMonitorAlertPrice' }
sub _objectType     { 'price_mismatch' }

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

    my $parent = BookPub::DB::Item::ProductMonitorAlert->Lookup( product_monitor_alert_id => $id );
    $self->SUPER::_propertiesFromDBItem( $hrProperties, $parent );

    my $dbItem = BookPub::DB::Item::ProductMonitorAlertPrice->Lookup( product_monitor_alert_id => $id );
    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

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

    unless ( ref($dbItem) eq 'BookPub::DB::Item::ProductMonitorAlertPrice' ) {
        $dbItem =
          BookPub::DB::Item::ProductMonitorAlertPhysicalPrice->Lookup( product_monitor_alert_id => $dbItem->product_monitor_alert_id );
    }

    $hrProperties->{catalog_price} = $dbItem->catalog_price;
}

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

    $self->SUPER::_initProperties($props);

    $self->{PhysicalPrice} = $props->{catalog_price};

    return $self;
}

sub create {
    my $self      = shift;
    my $data      = shift || die "data required";
    my $serviceID = $data->{service_id} || die "Service ID required";
    my $productID = $data->{product_id} || die "Product ID required";
    my %args      = @_;

    Log->info(" -- Create an alert");

    my $productMonitorItem =
      new BookPub::RDAS::Form::ProductMonitor::ProductMonitorItem::Price( productID => $productID, serviceID => $serviceID );
    assert( $productMonitorItem->ProductMonitorItemID );

    my $id = $self->SUPER::create( $data, %args, productMonitorItemID => $productMonitorItem->ProductMonitorItemID ) || return;

    my $dbItem = BookPub::DB::Item::ProductMonitorAlertPrice->Create(
        product_monitor_alert_id => $id,
        catalog_price            => $data->{catalog_price},
    );

    $dbItem->save();

    return $dbItem->product_monitor_alert_id;
}

sub set {
    my $self      = shift;
    my %args      = @_;
    my $serviceID = $args{serviceID} || die "Service ID required";
    my $productID = $args{productID} || die "Product ID required";
    my $price     = $args{price} || 0;

    Log->notice(" + Begin Alert Process for digital / physical price mismatch");

    my $product = new BookPub::Catalog::Product::Book( productID => $productID );

    unless ( $product->isEBook ) {
        Log->notice("  ** Not an ebook product, ignoring physical price check");
        return;
    }

    my $book = new BookPub::Catalog::Book( bookID => $product->BookID() );
    my $maxPhysicalPrice = $book->maxPhysicalCurrentPrice( currency => 'USD', serviceID => $serviceID );

    unless ( defined($maxPhysicalPrice) ) {
        Log->info("No physical price found for this book.  Ignoring alert");
        $self->_resolveAlerts( type => $self->_objectType(), serviceID => $serviceID, productID => $productID );
        return;
    }

    if ( $self->_isAlert( @_, catalogPrice => $maxPhysicalPrice ) ) {
        $self->_generateAlert( @_, catalogPrice => $maxPhysicalPrice );
    } else {
        Log->info(" No alert generated.  Resolving any open alerts");
        $self->_resolveAlerts( type => $self->_objectType(), serviceID => $serviceID, productID => $productID );
    }
}

sub _isAlert {
    my $self         = shift;
    my %args         = @_;
    my $serviceID    = $args{serviceID} || die "Service ID required";
    my $productID    = $args{productID} || die "Product ID required";
    my $catalogPrice = $args{catalogPrice};
    my $price        = $args{price} || 0;

    Log->info(" -- Determine if alert is required, advertised price: $price, phsycal catalog price: $catalogPrice");

    unless ( defined($catalogPrice) ) {
        Log->notice("*** No catalog price found, ignoring alert ***");
        return;
    }

    if ( $price > $catalogPrice ) {
        Log->info( " ALERT: $price (digital) > " . $catalogPrice . " (physical)" );
        return 1;
    }

    return;
}

sub _generateAlert {
    my $self         = shift;
    my %args         = @_;
    my $serviceID    = $args{serviceID} || die "Service ID required";
    my $productID    = $args{productID} || die "Product ID required";
    my $catalogPrice = $args{catalogPrice} || die "Catalog price required";

    Log->info(" --Start the alert generation process");

    if ( $self->_requireAlertUpdate( @_, catalogPrice => $catalogPrice ) ) {
        Log->info(" -- An alert update is required.");
        $self->_resolveAlerts( serviceID => $serviceID, productID => $productID, type => $self->_objectType );
        $self->create( { service_id => $serviceID, product_id => $productID, catalog_price => $catalogPrice } );
    } else {
        Log->info(" -- No alert update required");
    }

}

sub _requireAlertUpdate {
    my $self = shift;
    my %args = @_;

    my $serviceID    = $args{serviceID}    || die "Service ID required";
    my $productID    = $args{productID}    || die "Product ID required";
    my $catalogPrice = $args{catalogPrice} || return;

    return $self->_getDBItemClass()->requireUpdate(
        service_id    => $serviceID,
        product_id    => $productID,
        catalog_price => $catalogPrice
    );

}

###
1;    #
###
