#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::Catalog::Market::MarketWithPricingByCurrency;
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::Util;
use Common::Assert;
use Common::FormObject;
use Common::CurrencyFormat;

use BookPub::Catalog::Region::RegionWithCountries;

use BookPub::DB::Item::Market;
use BookPub::DB::Item::ProductMarketPrice;

use base 'BookPub::Catalog::Market';

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

    assert( $args{productID} );

    $self->{ProductID} = $args{productID};
    $self->{MarketID}  = $args{marketID};

    assert( $self->ProductID );
    assert( $self->MarketID );

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

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

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

    foreach my $code ( $self->_getDistinctCurrency() ) {
        my $item = { CurrencyCode => $code };

        my $format = Common::CurrencyFormat->new( currencyCode => $code );
        $item->{CurrencyCodeName} = $format->description();

        $item->{Prices} =
          BookPub::Price::ProductMarketPriceList->new( currencyCode => $code, marketID => $self->MarketID, productID => $self->ProductID );
        push( @result, $item );
    }

    $self->{Currency} = \@result;
}

sub _getDistinctCurrency {
    my $self = shift;
    my @codes;

    my $collection = BookPub::DB::Item::ProductMarketPrice->GetAllCurrencyCodes(
        productID => $self->ProductID,
        marketID  => $self->MarketID
    );

    while ( my $row = $collection->next ) {
        push( @codes, $row->currency_code );
    }

    return @codes;
}

sub getCurrentCurrencyPrice {
    my ( $self, $currencyCode ) = @_;

    my $price;

    my $currencyElements = $self->{Currency};
    foreach my $element (@$currencyElements) {
        if ( $element->{CurrencyCode} eq $currencyCode ) {
            my $priceList               = $element->{Prices};
            my $arrayName               = $priceList->_itemName();
            my $productMarketPriceArray = $priceList->{$arrayName};

            foreach my $priceItem (@$productMarketPriceArray) {
                if ( 'current' eq $priceItem->Status() ) {
                    $price = $priceItem->Price();
                    last;
                }
            }
        }
    }

    return $price;
}

sub getCurrentCurrencyPriceObject {
    my ( $self, $currencyCode ) = @_;

    my $price;

    my $currencyElements = $self->{Currency};
    foreach my $element (@$currencyElements) {
        if ( $element->{CurrencyCode} eq $currencyCode ) {
            my $priceList               = $element->{Prices};
            my $arrayName               = $priceList->_itemName();
            my $productMarketPriceArray = $priceList->{$arrayName};

            foreach my $priceItem (@$productMarketPriceArray) {
                if ( 'current' eq $priceItem->Status() ) {
                    $price = $priceItem;
                    last;
                }
            }
        }
    }

    return $price;
}

###
1;    #
###
###
1;    #
###
