#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::Price::ProductMarketPrice;
use strict;
use warnings;

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

use base 'Common::FormObject';

sub GetCurrentPrices {
    my $class = shift;
    my %args  = @_;

    # Add assertions here.

    return $class->GetPrices(%args);
}

sub GetPrices {
    my $class = shift;
    my %args  = @_;
    my @prices;

    # Some sale records do not have a single date, but instead have a range.
    # Therefore, there could be multiple Prices return.
    my $collection = BookPub::DB::Item::ProductMarketPrice->GetAllCurrentPrice(%args);

    while ( my $dbItem = $collection->next() ) {
        my $obj = $class->new( dbItem => $dbItem );
        push( @prices, $obj );
    }

    return @prices;
}

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

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

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

    $self->_initProperties( \%properties );

    return $self;
}

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

    my $dbItem = BookPub::DB::Item::ProductMarketPrice->Lookup( product_market_price_id => $id );

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

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

    $hrProperties->{product_market_price_id} = $dbItem->product_market_price_id;
    $hrProperties->{product_market_id}       = $dbItem->product_market_id;
    $hrProperties->{price}                   = $dbItem->price;
    $hrProperties->{currency_code}           = $dbItem->currency_code;
    $hrProperties->{discount_id}             = $dbItem->discount_id;
    $hrProperties->{region_id}               = $dbItem->region_id;
    $hrProperties->{start_date}              = $dbItem->start_date;
    $hrProperties->{end_date}                = $dbItem->end_date;
    $hrProperties->{catalog_import_id}       = $dbItem->catalog_import_id;
    $hrProperties->{price_type_id}           = $dbItem->price_type_id;
    $hrProperties->{price_type_qualifier_id} = $dbItem->price_type_qualifier_id;
}

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

    $self->{ProductMarketPriceID} = $props->{product_market_price_id};
    $self->{ProductMarketID}      = $props->{product_market_id};
    $self->{CatalogImportID}      = $props->{catalog_import_id};
    $self->{PriceTypeID}          = $props->{price_type_id};
    $self->{PriceTypeQualifierID} = $props->{price_type_qualifier_id};
    $self->{RegionID}             = $props->{region_id};
    $self->{Price}        = Common::FormObject::Scalar::Money->new( value => $props->{price}, currencyCode => $props->{currency_code} );
    $self->{CurrencyCode} = $props->{currency_code};
    $self->{DiscountID}   = $props->{discount_id};
    $self->{StartDate}    = Common::FormObject::Scalar::Date->new( value => $props->{start_date} );
    $self->{EndDate}      = Common::FormObject::Scalar::Date->new( value => $props->{end_date} );

    $self->{Status}           = $self->_getPriceStatus();
    $self->{CurrencyCodeName} = $self->_getCurrencyName();

    my $discountCode;
    if ( $props->{discount_id} ) {
        my $discount = BookPub::DB::Item::Discount->Lookup( discount_id => $props->{discount_id} );
        if ($discount) {
            $discountCode = $discount->client_code;
        }
    }

    $self->{DiscountCode} = $discountCode;
}

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

sub _getPriceStatus {
    my $self = shift;

    #	return unless( $self->StartDate );

    my $startDate;

    my $now = time();
    if ( $self->StartDate() ) {
        ($startDate) = split( / /, $self->StartDate );

        # We're seeing some dates earlier than the epoch.  Let's just set them to zero
        # to prevent things from blowing up.
        my $year = substr( $startDate, 0, 4 );
        if ( $year < 1970 ) {
            $startDate = '0000-00-00';
        }
    } else {
        $startDate = '0000-00-00';
    }

    my $start = $startDate eq '0000-00-00' ? 0 : Common::Util::dateTimeToSeconds("$startDate 00:00:00");

    return "future" if ( $now < $start );
    return "current" unless ( $self->EndDate );

    my ($endDate) = split( / /, $self->EndDate ) if ( $self->EndDate );
    my $end = Common::Util::dateTimeToSeconds("$endDate 23:23:59") unless ( $endDate eq '0000-00-00' );

    return "current" unless ($end);

    return $end < $now ? "historical" : "current";
}

sub _getCurrencyName {
    my $self = shift;

    my $format = Common::CurrencyFormat->new( currencyCode => $self->CurrencyCode );
    return $format->description;
}

###
1;    #
###
