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

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

use BookPub::DB::Item::RegionIncludedCountry;
use BookPub::DB::Item::RegionExcludedCountry;

use Common::RSApp;
use Common::Util;
use Common::Assert;

use base 'BookPub::Catalog::Region';

# The maximum number of countries we'll try and stuff into the Display field for a region
# before we use the elipsis truncation scheme.
#
use constant kMaxCountries => 12;

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

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

    $self->{Included} = $self->_getIncludedCountries();
    $self->{Excluded} = $self->_getExcludedCountries();

    $self->{Display} = $self->_getDisplay();
}

sub _getIncludedCountries {
    my $self = shift;
    my @countries;

    my $collection = BookPub::DB::Item::RegionIncludedCountry->GetAllByRegionID( $self->RegionID, 'alpha2' );

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

    return @countries ? { Country => \@countries } : undef;
}

sub _getExcludedCountries {
    my $self = shift;
    my @countries;

    my $collection = BookPub::DB::Item::RegionExcludedCountry->GetAllByRegionID( $self->RegionID, 'alpha2' );

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

    return @countries ? { Country => \@countries } : undef;
}

sub _getDisplay {
    my $self = shift;
    my $result;

    if ( $self->{Included} ) {

        # !!! Some clients don't seem to understand the concept of the 'excluded country' list.
        # So when we have 238 countries in the Included array, we're not going to try and list them all.
        #
        $result = _squashedCountryList( $self->{Included}{Country}, kMaxCountries );

        #	    $result = join( ',', @{$self->{Included}->{Country}} );
    } else {
        $result = 'All';
    }

    if ( $self->{Excluded} ) {
        $result .= " excluding " . _squashedCountryList( $self->{Excluded}{Country}, kMaxCountries );

        #		$result .= " excluding " . join( ',', @{$self->{Excluded}->{Country}} );
    }

    return $result;
}

sub _squashedCountryList {
    my ( $list, $maxSize ) = @_;

    my $result;

    my $listSize = scalar @$list;
    if ( $listSize > kMaxCountries ) {

        # We'll show the first 3, '...', and the last 3.
        #
        $result =
            $list->[0] . ','
          . $list->[1] . ','
          . $list->[2]
          . ', ... ,'
          . $list->[ $listSize - 3 ] . ','
          . $list->[ $listSize - 2 ] . ','
          . $list->[ $listSize - 1 ];
    } else {
        $result = join( ',', @$list );
    }

    return $result;
}

###
1;    #
###
