#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::Catalog::Product::Book::WithAlert;

use strict;
use warnings;

use Data::Dumper;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use lib '/app/tools/mediaserve/lib';

use Common::RSApp;
use Common::Log;
use Common::Assert;
use Common::Client;
use Common::FormObject;

use BookPub::Tracker::Service;
use BookPub::Catalog::Book::WithAuthor;

use BookPub::RDAS::Form::ProductMonitor::ProductMonitorItem::Price;
use BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::Price;
use BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::Available;
use BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::Unavailable;
use BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::CoverArt;
use BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::PhysicalPrice;

use BookPub::DB::Item::BookProduct;

use base 'BookPub::Catalog::Product::Book';

sub _propertiesFromDBItem {
    my $self   = shift;
    my $props  = shift;
    my $dbitem = shift;
    my $productMonitorItem;

    if ( ref($dbitem) =~ /ProductMonitorAlert(.*)/ ) {
        $productMonitorItem = BookPub::DB::Item::ProductMonitorItem->Lookup( product_monitor_item_id => $dbitem->product_monitor_item_id );

        my $alertClass = BookPub::RDAS::Form::ProductMonitor::ProductMonitorAlert::GetAlertClass( $dbitem->type() );
        Log->info( " DB Item Type: " . $dbitem->type );
        Log->info( " Alert Class: " . $alertClass );

        die "Unknown alert type: " . $dbitem->type() unless ($alertClass);

        $props->{alert} = $alertClass->new( productMonitorAlertID => $dbitem->product_monitor_alert_id );
        $props->{service_id} = $productMonitorItem->{service_id};

        $dbitem = BookPub::DB::Item::BookProduct->Lookup( product_id => $productMonitorItem->product_id );
    }

    return $self->SUPER::_propertiesFromDBItem( $props, $dbitem );
}

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

    # Invoke the inherited method first.
    #
    $self->SUPER::_initProperties($props);

    $self->{ServiceID} = $props->{service_id};
    $self->{Alert}     = $props->{alert};

    $self->{Service} = new BookPub::Tracker::Service( serviceID => $self->ServiceID );

    $self->{Book} = new BookPub::Catalog::Book::WithAuthor( bookID => $self->BookID );

    if ( $self->Alert ) {
        $self->{ProductMonitorItemPrice} =
          new BookPub::RDAS::Form::ProductMonitor::ProductMonitorItem::Price( productID => $self->ProductID,
            serviceID => $self->ServiceID );
    }
}

sub rowHeader {
    my $self = shift;
    my $delimeter = shift || "\t";

    return join( $delimeter,
        "Date Reported",
        "Service",
        "Issue",
        "Average Rank",
        "Title",
        "Author",
        "ISBN",
        "Pub. Date",
        "Catalog Price",
        "Advertised Price",
        "Date Resolved" );
}

sub asRow {
    my $self = shift;
    my $delimeter = shift || "\t";

    #my $catalogPrice = $self->Alert->can( 'CatalogPrice' ) ? $self->Alert->CatalogPrice() : '--';

    my $catalogPrice;

    if ( $self->Alert->Type() eq 'invalid_price' ) {
        $catalogPrice = $self->Alert->CatalogPrice();
    }

    my $advertisedPrice = $self->ProductMonitorItemPrice()->Price || '--';

    my $bookTitle  = $self->Book->Title();
    my $authorName = $self->Book->AuthorName();

    # We need to do some escaping for the csv format.
    #
    if ( $delimeter eq ',' ) {
        $bookTitle =~ s/\"/\"\"/g;
        $bookTitle = '"' . $bookTitle . '"';
        $authorName =~ s/\"/\"\"/g;
        $authorName = '"' . $authorName . '"';
    }

    return join( $delimeter,
        $self->Alert->DateCreate(),              $self->Service->ServiceName(), $self->Alert->Text(),
        $self->AverageRank(),                    $bookTitle,                    $authorName,
        $self->ISBN13,                           $self->ReleaseDate(),          $catalogPrice,
        $self->ProductMonitorItemPrice()->Price, $self->Alert->DateResolved(), );
}

###
1;    #
###
