#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Region::CountryList;

use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::Region::RegionCountryMap;

use Common::FormObject;
use base 'Common::FormObject';

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

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

    my $regionID = $args{regionID};
    $self->{_regionID} = $regionID;

    $self->{CountryCodes} = [];
    $self->{Country}      = [];

    if ($regionID) {
        my $collection = RPS::DB::Item::RegionCountryMap->GetByRegionID($regionID);
        while ( $collection->hasNext() ) {
            my $obj = $collection->next();
            push @{ $self->{Country} }, RPS::Region::RegionCountryMap->new( _dbItem => $obj );
            push @{ $self->{CountryCodes} }, $obj->country_code;
        }
        @{ $self->{Country} }      = sort { $a->CountryName() cmp $b->CountryName() } @{ $self->{Country} };
        @{ $self->{CountryCodes} } = sort { $a cmp $b } @{ $self->{CountryCodes} };
    }

    return $self;
}

sub validate {
    my ($self) = @_;

    my $valid = $self->SUPER::validate();

    if ( !@{ $self->{CountryCodes} } ) {
        $self->setError(Common::FormObject::kErrFieldBlank);
        $valid = 0;
    }

    return $valid;
}

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

    my $newRegionID = $args{region_id};

    if ( !$self->{_regionID} ) {
        $self->{_regionID} = $newRegionID;
    }

    # let's look at the new country codes
    #
    foreach my $newCode ( @{ $self->{CountryCodes} } ) {
        my $inList = 0;

        # see if the new code is in the existing list of countries
        #
        foreach my $map ( @{ $self->{Country} } ) {
            if ( $map->CountryCode() eq $newCode ) {
                $inList = 1;
                last;
            }
        }

        # if it's not in the list then add it
        #
        if ( !$inList ) {
            my $newMap = RPS::Region::RegionCountryMap->new();
            $newMap->RegionID( $self->{_regionID} );
            $newMap->CountryCode($newCode);
            push @{ $self->{Country} }, $newMap;
        }
    }

    foreach my $countryMap ( @{ $self->{Country} } ) {

        # is this country still in the list of new countries?
        # if yes, then save
        # else, delete it
        #
        my $existingCode = $countryMap->CountryCode();
        if ( grep /$existingCode/, @{ $self->{CountryCodes} } ) {
            $countryMap->save();
        } else {
            $countryMap->delete();
        }
    }
}

sub _propertyIsEditable {
    my ( $self, $propertyName ) = @_;

    # The Product (and contained objects) can be edited.
    #
    if ( $propertyName eq 'CountryCodes' ) {
        return 1;
    }
    return 0;
}

sub CountryCodes {
    my $self = shift;

    if (@_) {
        my $newVal = shift @_;
        if ( ref($newVal) eq 'ARRAY' ) {
            $self->{CountryCodes} = $newVal;
        } else {
            $self->{CountryCodes} = [$newVal];
        }
    } else {
        return $self->{CountryCodes};
    }
}

sub getCountries {
    my ($self) = @_;

    return $self->{Country};
}

###
1;    #
###

