#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::Price;
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     { 'invalid_price' }

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::ProductMonitorAlertPrice->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->{CatalogPrice} = Common::FormObject::Scalar::Money->new( value => $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 );
    return unless ( $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");

    my $product      = new BookPub::Catalog::Product::Book::WithPricing( productID => $productID );
    my $catalogPrice = $self->_getCatalogPrice($product);
    my $catPrice     = $catalogPrice ? $catalogPrice->Price : 0;

    if ( $catalogPrice->PriceTypeID != 41 && $catalogPrice->PriceTypeID != 42 ) {
        Log->notice("  ** Not an agency price.  Ignoring alerts");
        return;
    }

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

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 catalog price: $catalogPrice");

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

    if ( $price != $catalogPrice ) {
        Log->info( " ALERT: $price != " . $catalogPrice );
        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
    );

}

sub _getCatalogPrice {
    my $self = shift;
    my $product = shift || return;

    Log->info( " -- Getting Catalog Price for product id: " . $product->ProductID );
    my $price = $product->getPriceByCurrency('USD') || return;

    Log->info( "    -- Product market price id: " . $price->ProductMarketPriceID );

    return $price;
}

###
1;    #
###
