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

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

use base 'Common::FormObject';

sub GetAlertClass {
    my $type = shift;

    assert($type);

    return
        $type eq 'not_available'      ? 'BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::Unavailable'
      : $type eq 'available_too_soon' ? 'BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::Available'
      : $type eq 'coverart_missing'   ? 'BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::CoverArt'
      : $type eq 'price_mismatch'     ? 'BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::PhysicalPrice'
      : $type eq 'invalid_price'      ? 'BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::Price'
      :                                 undef;
}

sub GetAlertText {
    my $type = shift || return;

    return
        $type eq 'not_available'      ? 'Product Unavailable'
      : $type eq 'available_too_soon' ? 'Available Too Soon'
      : $type eq 'coverart_missing'   ? 'Cover Art Unavailable'
      : $type eq 'price_mismatch'     ? 'Print/Digital Price'
      : $type eq 'invalid_price'      ? 'Invalid Price'
      :                                 undef;
}

sub GetAlertTypes {
    return qw( not_available
      available_too_soon
      coverart_missing
      price_mismatch
      invalid_price );
}

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

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

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

    $self->_initProperties( \%properties );

    return $self;
}

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

    my $dbItem = BookPub::DB::Item::ProductMonitorAlert->Lookup( product_monitor_alert_id => $id );

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

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

    $hrProperties->{product_monitor_alert_id} = $dbItem->product_monitor_alert_id;
    $hrProperties->{product_monitor_item_id}  = $dbItem->product_monitor_item_id;
    $hrProperties->{type}                     = $dbItem->type;
    $hrProperties->{date_created}             = $dbItem->date_created;
    $hrProperties->{date_resolved}            = $dbItem->date_resolved;
}

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

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

    $self->{ProductMonitorAlertID} = $props->{product_monitor_alert_id};
    $self->{ProductMonitorItemID}  = $props->{product_monitor_item_id};
    $self->{Type}                  = $props->{type};
    $self->{Text}                  = GetAlertText( $props->{type} );

    $self->{DateCreate}   = Common::FormObject::Scalar::DateTime->new( value => $props->{date_created},  readOnly => 1 );
    $self->{DateResolved} = Common::FormObject::Scalar::DateTime->new( value => $props->{date_resolved}, readOnly => 1 );
}

sub _getDBItemClass { die "Must be overloaded" }

sub create {
    my $self = shift;
    my $data = shift || die "data required";
    my %args = @_;

    my $productMonitorItemID = $args{productMonitorItemID} || $self->{_productMonitorItemID};

    assert($productMonitorItemID);

    my $pmAlert = BookPub::DB::Item::ProductMonitorAlert->Create(
        type                    => $self->_objectType(),
        product_monitor_item_id => $productMonitorItemID
    );
    $pmAlert->save();

    return $pmAlert->product_monitor_alert_id;
}

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

    my $excludeProductID = $args{excludeProductID};
    my $productID = $args{productID} || $args{excludeProductID};

    my $serviceID = $args{serviceID} || die "Service ID required";
    my $type      = $args{type}      || die "Type required";

    assert($productID);

    Log->info("Resolving unresolved alerts");
    my $product = new BookPub::Catalog::Product::Book( productID => $productID );

    Log->debug( "DBITEM: " . $self->_getDBItemClass() );
    if ( $product->isEBook() ) {
        $self->_getDBItemClass()
          ->ResolveAlert( type => $type, service_id => $serviceID, book_id => $product->BookID, exclude_product_id => $excludeProductID );
    } else {
        $self->_getDBItemClass()->ResolveAlert( type => $type, service_id => $serviceID, product_id => $productID );
    }
}

###
1;    #
###
