#------------------------------------------------------------
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Common::Country;

use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::XMLObject;
use Common::Log;

use Common::DB::Item::Country;

use base 'Common::XMLObject';

###
#   Provides a general interface to search for and translate country codes.
#
#   A country object can be constructed using the new method or one of the public
#   Get methods.  All construction methods will return a Common::Country object
#   if a match is found otherwise it will return undef.
#
#   new( %args )
#      Valid Args:
#          search   -  Search all string fields for a match.  If multiple matches
#                      are found an exception is thrown
#          alpha2   -  Search for the country in the alpha2 column only
#          alpha3   -  Search for the country in the alpha3 column only
#          numeric  -  Search for the country in the numeric3 column only
#          name     -  Search for the country in the name column only
#          custom   -  Search for the country in the country_custom table
#          dbItem   -  Construt the object with the passed dbItem object
#
#  Get          - same as calling new with 'search'
#  GetByAlpha2  - same as calling new with 'search'
#  GetByAlpha3  - same as calling new with 'search'
#  GetByNumeric - same as calling new with 'search'
#  GetByName    - same as calling new with 'search'
#  GetByCustom  - same as calling new with 'search'
#
###

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

    if ( $args{search} ) {
        $dbItem = Common::DB::Item::Country->SearchByString( $args{search} );
    } elsif ( $args{alpha2} ) {
        $dbItem = Common::DB::Item::Country->Lookup( alpha2 => $args{alpha2} );
    } elsif ( $args{alpha3} ) {
        $dbItem = Common::DB::Item::Country->Lookup( alpha3 => $args{alpha3} );
    } elsif ( $args{numeric} ) {
        $dbItem = Common::DB::Item::Country->Lookup( numeric3 => $args{numeric} );
    } elsif ( $args{name} ) {
        $dbItem = Common::DB::Item::Country->Lookup( name => $args{name} );
    } elsif ( $args{custom} ) {
        $dbItem = Common::DB::Item::Country->SearchByCustom( $args{custom} );
    } elsif ( $args{dbItem} ) {
        $dbItem = $args{dbItem};
    } else {
        die "Search criteria required to construct county object";
    }

    return undef unless ($dbItem);

    $self->_propertiesFromDBItem( $properties, $dbItem );
    $self->_initProperties($properties);

    return $self;
}

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

    assert($dbItem);

    $hrProperties->{country_id} = $dbItem->country_id;
    $hrProperties->{alpha2}     = $dbItem->alpha2;
    $hrProperties->{alpha3}     = $dbItem->alpha3;
    $hrProperties->{numeric}    = $dbItem->numeric3;
    $hrProperties->{name}       = $dbItem->name;
}

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

    $self->{CountryID} = $props->{country_id};
    $self->{Code}      = $props->{alpha2};
    $self->{Alpha2}    = $props->{alpha2};
    $self->{Alpha3}    = $props->{alpha3};
    $self->{Numeric}   = $props->{numeric};
    $self->{Name}      = $props->{name};
}

# Accessors
sub countryID { shift->{CountryID} }
sub alpha2    { shift->{Alpha2} }
sub alpha3    { shift->{Alpha3} }
sub numeric   { shift->{Numeric} }
sub name      { shift->{Name} }

# Public contruction methods
sub Get {
    my $class         = shift;
    my $countryString = shift || die "Search criteria required";
    my $self          = bless {}, $class;

    return $self->_init( search => $countryString );
}

sub GetByAlpha2 {
    my $class         = shift;
    my $countryString = shift || die "Search criteria required";
    my $self          = bless {}, $class;

    return $self->_init( alpha2 => $countryString );
}

sub GetByAlpha3 {
    my $class         = shift;
    my $countryString = shift || die "Search criteria required";
    my $self          = bless {}, $class;

    return $self->_init( alpha3 => $countryString );
}

sub GetByNumeric {
    my $class         = shift;
    my $countryString = shift || die "Search criteria required";
    my $self          = bless {}, $class;

    return $self->_init( numeric => $countryString );
}

sub GetByName {
    my $class         = shift;
    my $countryString = shift || die "Search criteria required";
    my $self          = bless {}, $class;

    return $self->_init( name => $countryString );
}

sub GetByCustom {
    my $class         = shift;
    my $countryString = shift || die "Search criteria required";
    my $self          = bless {}, $class;

    return $self->_init( custom => $countryString );
}


# map of countries in different languages
my %countries = (
	'afghanistan'                      => {
		'fr' => 'afghanistan',
	},
	'albania'                          => {
		'fr' => 'albanie',
	},
	'algeria'                          => {
		'fr' => 'algérie',
	},
	'andorra'                          => {
		'fr' => 'andorre',
	},
	'angola'                           => {
		'fr' => 'angola',
	},
	'antigua and barbuda'              => {
		'fr' => 'antigua-et-barbuda',
	},
	'argentina'                        => {
		'fr' => 'argentine',
	},
	'armenia'                          => {
		'fr' => 'arménie',
	},
	'aruba'                            => {
		'fr' => 'aruba',
	},
	'australia'                        => {
		'fr' => 'australie',
	},
	'austria'                          => {
		'fr' => 'autriche',
	},
	'azerbaijan'                       => {
		'fr' => 'azerbaïdjan',
	},
	'bahamas'                          => {
		'fr' => 'bahamas',
	},
	'bahrain'                          => {
		'fr' => 'bahreïn',
	},
	'bangladesh'                       => {
		'fr' => 'bangladesh',
	},
	'barbados'                         => {
		'fr' => 'barbade',
	},
	'belarus'                          => {
		'fr' => 'biélorussie',
	},
	'belgium'                          => {
		'fr' => 'belgique',
	},
	'belize'                           => {
		'fr' => 'belize',
	},
	'benin'                            => {
		'fr' => 'bénin',
	},
	'bermuda'                          => {
		'fr' => 'bermudes',
	},
	'bhutan'                           => {
		'fr' => 'bhoutan',
	},
	'bolivia'                          => {
		'fr' => 'bolivie',
	},
	'bosnia and herzegovina'           => {
		'fr' => 'bosnie-herzégovine',
	},
	'botswana'                         => {
		'fr' => 'botswana',
	},
	'brazil'                           => {
		'fr' => 'brésil',
	},
	'brunei'                           => {
		'fr' => 'brunei',
	},
	'bulgaria'                         => {
		'fr' => 'bulgarie',
	},
	'burkina faso'                     => {
		'fr' => 'burkina faso',
	},
	'burundi'                          => {
		'fr' => 'burundi',
	},
	'cayman islands'                   => {
		'fr' => 'îles caïmans',
	},
	'cambodia'                         => {
		'fr' => 'cambodge',
	},
	'cameroon'                         => {
		'fr' => 'cameroun',
	},
	'canada'                           => {
		'fr' => 'canada',
	},
	'cape verde'                       => {
		'fr' => 'cap-vert',
	},
	'central african republic'         => {
		'fr' => 'république centrafricaine',
	},
	'chad'                             => {
		'fr' => 'tchad',
	},
	'chile'                            => {
		'fr' => 'chili',
	},
	'china'                            => {
		'fr' => 'chine',
	},
	'colombia'                         => {
		'fr' => 'colombie',
	},
	'comoros'                          => {
		'fr' => 'comores',
	},
	'costa rica'                       => {
		'fr' => 'costa rica',
	},
	'côte d’ivoire'                    => {
		'fr' => 'côte d’ivoire',
	},
	'croatia'                          => {
		'fr' => 'croatie',
	},
	'cuba'                             => {
		'fr' => 'cuba',
	},
	'cyprus'                           => {
		'fr' => 'chypre',
	},
	'czech republic'                   => {
		'fr' => 'république tchèque',
	},
	'democratic republic of the congo' => {
		'fr' => 'république démocratique du congo',
	},
	'denmark'                          => {
		'fr' => 'danemark',
	},
	'djibouti'                         => {
		'fr' => 'djibouti',
	},
	'dominica'                         => {
		'fr' => 'dominique',
	},
	'dominican republic'               => {
		'fr' => 'république dominicaine',
	},
	'east timor'                       => {
		'fr' => 'timor oriental',
	},
	'ecuador'                          => {
		'fr' => 'équateur',
	},
	'egypt'                            => {
		'fr' => 'égypte',
	},
	'salvador'                         => {
		'fr' => 'el salvador',
	},
	'equatorial guinea'                => {
		'fr' => 'guinée équatoriale',
	},
	'eritrea'                          => {
		'fr' => 'érythrée',
	},
	'estonia'                          => {
		'fr' => 'estonie',
	},
	'ethiopia'                         => {
		'fr' => 'éthiopie',
	},
	'fiji'                             => {
		'fr' => 'fidji',
	},
	'finland'                          => {
		'fr' => 'finlande',
	},
	'france'                           => {
		'fr' => 'france',
	},
	'french polynesia'                 => {
		'fr' => 'polynésie française',
	},
	'gabon'                            => {
		'fr' => 'gabon',
	},
	'gambia'                           => {
		'fr' => 'gambie',
	},
	'georgia'                          => {
		'fr' => 'géorgie',
	},
	'germany'                          => {
		'fr' => 'allemagne',
	},
	'ghana'                            => {
		'fr' => 'ghana',
	},
	'greece'                           => {
		'fr' => 'grèce',
	},
	'grenada'                          => {
		'fr' => 'grenade',
	},
	'guadeloupe'                       => {
		'fr' => 'guadeloupe',
	},
	'guatemala'                        => {
		'fr' => 'guatemala',
	},
	'guinea'                           => {
		'fr' => 'guinée',
	},
	'guinea-bissau'                    => {
		'fr' => 'guinée-bissau',
	},
	'guyana'                           => {
		'fr' => 'guyane',
	},
	'haiti'                            => {
		'fr' => 'haïti',
	},
	'honduras'                         => {
		'fr' => 'honduras',
	},
	'hungary'                          => {
		'fr' => 'hongrie',
	},
	'iceland'                          => {
		'fr' => 'islande',
	},
	'india'                            => {
		'fr' => 'inde',
	},
	'indonesia'                        => {
		'fr' => 'indonésie',
	},
	'iran'                             => {
		'fr' => 'iran',
	},
	'iraq'                             => {
		'fr' => 'irak',
	},
	'ireland'                          => {
		'fr' => 'irlande',
	},
	'israel'                           => {
		'fr' => 'israël',
	},
	'italy'                            => {
		'fr' => 'italie',
	},
	'jamaica'                          => {
		'fr' => 'jamaïque',
	},
	'japan'                            => {
		'fr' => 'japon',
	},
	'jordan'                           => {
		'fr' => 'jordanie',
	},
	'kazakhstan'                       => {
		'fr' => 'kazakhstan',
	},
	'kenya'                            => {
		'fr' => 'kenya',
	},
	'kiribati'                         => {
		'fr' => 'kiribati',
	},
	'kuwait'                           => {
		'fr' => 'koweït',
	},
	'kyrgyzstan'                       => {
		'fr' => 'kirghizistan',
	},
	'laos'                             => {
		'fr' => 'laos',
	},
	'latvia'                           => {
		'fr' => 'lettonie',
	},
	'lebanon'                          => {
		'fr' => 'liban',
	},
	'lesotho'                          => {
		'fr' => 'lesotho',
	},
	'liberia'                          => {
		'fr' => 'liberia',
	},
	'libya'                            => {
		'fr' => 'libye',
	},
	'liechtenstein'                    => {
		'fr' => 'liechtenstein',
	},
	'lithuania'                        => {
		'fr' => 'lituanie',
	},
	'luxembourg'                       => {
		'fr' => 'luxembourg',
	},
	'madagascar'                       => {
		'fr' => 'madagascar',
	},
	'malawi'                           => {
		'fr' => 'malawi',
	},
	'malaysia'                         => {
		'fr' => 'malaisie',
	},
	'maldives'                         => {
		'fr' => 'maldives',
	},
	'mali'                             => {
		'fr' => 'mali',
	},
	'malta'                            => {
		'fr' => 'malte',
	},
	'marshall islands'                 => {
		'fr' => 'îles marshall',
	},
	'martinique'                       => {
		'fr' => 'martinique',
	},
	'mauritania'                       => {
		'fr' => 'mauritanie',
	},
	'mauritius'                        => {
		'fr' => 'maurice',
	},
	'mayotte'                          => {
		'fr' => 'mayotte',
	},
	'mexico'                           => {
		'fr' => 'mexique',
	},
	'micronesia'                       => {
		'fr' => 'micronésie',
	},
	'moldova'                          => {
		'fr' => 'moldavie',
	},
	'monaco'                           => {
		'fr' => 'monaco',
	},
	'mongolia'                         => {
		'fr' => 'mongolie',
	},
	'montenegro'                       => {
		'fr' => 'monténégro',
	},
	'morocco'                          => {
		'fr' => 'maroc',
	},
	'mozambique'                       => {
		'fr' => 'mozambique',
	},
	'myanmar'                          => {
		'fr' => 'birmanie',
	},
	'namibia'                          => {
		'fr' => 'namibie',
	},
	'nauru'                            => {
		'fr' => 'nauru',
	},
	'nepal'                            => {
		'fr' => 'népal',
	},
	'netherlands'                      => {
		'fr' => 'pays-bas',
	},
	'new zealand'                      => {
		'fr' => 'nouvelle-zélande',
	},
	'nicaragua'                        => {
		'fr' => 'nicaragua',
	},
	'niger'                            => {
		'fr' => 'niger',
	},
	'nigeria'                          => {
		'fr' => 'nigéria',
	},
	'north korea'                      => {
		'fr' => 'corée du nord',
	},
    'northern mariana islands'         => {
        'fr' => 'îles mariannes du nord',
    },
	'norway'                           => {
		'fr' => 'norvège',
	},
	'oman'                             => {
		'fr' => 'oman',
	},
	'pakistan'                         => {
		'fr' => 'pakistan',
	},
	'palau'                            => {
		'fr' => 'palaos',
	},
	'panama'                           => {
		'fr' => 'panama',
	},
	'papua new guinea'                 => {
		'fr' => 'papouasie-nouvelle-guinée',
	},
	'paraguay'                         => {
		'fr' => 'paraguay',
	},
	'peru'                             => {
		'fr' => 'pérou',
	},
	'philippines'                      => {
		'fr' => 'philippines',
	},
	'poland'                           => {
		'fr' => 'pologne',
	},
	'puerto rico'                      => {
		'fr' => 'porto rico',
	},
	'portugal'                         => {
		'fr' => 'portugal',
	},
	'qatar'                            => {
		'fr' => 'qatar',
	},
	'republic of the congo'            => {
		'fr' => 'république du congo',
	},
	'republic of macedonia'            => {
		'fr' => 'macédoine',
	},
	'romania'                          => {
		'fr' => 'roumanie',
	},
	'russia'                           => {
		'fr' => 'russie',
	},
	'rwanda'                           => {
		'fr' => 'rwanda',
	},
	'saint kitts and nevis'            => {
		'fr' => 'saint-christophe-et-niévès',
	},
	'saint lucia'                      => {
		'fr' => 'sainte-lucie',
	},
	'saint pierre and miquelon'        => {
		'fr' => 'saint-pierre-et-miquelon',
	},
	'saint vincent and the grenadines' => {
		'fr' => 'saint-vincent-et-les-grenadines',
	},
	'samoa'                            => {
		'fr' => 'samoa',
	},
	'san marino'                       => {
		'fr' => 'saint-marin',
	},
	'sao tome and principe'            => {
		'fr' => 'são tomé-et-principe',
	},
	'saudi arabia'                     => {
		'fr' => 'arabie saoudite',
	},
	'senegal'                          => {
		'fr' => 'sénégal',
	},
	'serbia'                           => {
		'fr' => 'serbie',
	},
	'seychelles'                       => {
		'fr' => 'seychelles',
	},
	'sierra leone'                     => {
		'fr' => 'sierra leone',
	},
	'singapore'                        => {
		'fr' => 'singapour',
	},
	'slovakia'                         => {
		'fr' => 'slovaquie',
	},
	'slovenia'                         => {
		'fr' => 'slovénie',
	},
	'solomon islands'                  => {
		'fr' => 'îles salomon',
	},
	'somalia'                          => {
		'fr' => 'somalie',
	},
	'south africa'                     => {
		'fr' => 'afrique du sud',
	},
	'south korea'                      => {
		'fr' => 'corée du sud',
	},
	'south sudan'                      => {
		'fr' => 'soudan du sud',
	},
	'spain'                            => {
		'fr' => 'espagne',
	},
	'sri lanka'                        => {
		'fr' => 'sri lanka',
	},
	'sudan'                            => {
		'fr' => 'soudan',
	},
	'suriname'                         => {
		'fr' => 'suriname',
	},
	'swaziland'                        => {
		'fr' => 'swaziland',
	},
	'sweden'                           => {
		'fr' => 'suède',
	},
	'switzerland'                      => {
		'fr' => 'suisse',
	},
	'syria'                            => {
		'fr' => 'syrie',
	},
	'taiwan'                           => {
		'fr' => 'taïwan',
	},
	'tajikistan'                       => {
		'fr' => 'tadjikistan',
	},
	'tanzania'                         => {
		'fr' => 'tanzanie',
	},
	'thailand'                         => {
		'fr' => 'thaïlande',
	},
	'togo'                             => {
		'fr' => 'togo',
	},
	'tonga'                            => {
		'fr' => 'tonga',
	},
	'trinidad and tobago'              => {
		'fr' => 'trinité-et-tobago',
	},
	'tunisia'                          => {
		'fr' => 'tunisie',
	},
	'turkey'                           => {
		'fr' => 'turquie',
	},
	'turkmenistan'                     => {
		'fr' => 'turkménistan',
	},
	'turks and caicos islands'         => {
		'fr' => 'îles turques-et-caïques',
	},
	'tuvalu'                           => {
		'fr' => 'tuvalu',
	},
	'uganda'                           => {
		'fr' => 'ouganda',
	},
	'ukraine'                          => {
		'fr' => 'ukraine',
	},
	'united arab emirates'             => {
		'fr' => 'émirats arabes unis',
	},
	'united kingdom'                   => {
		'fr' => 'royaume-uni',
	},
	'united states of america'         => {
		'fr' => 'états-unis',
	},
	'uruguay'                          => {
		'fr' => 'uruguay',
	},
	'uzbekistan'                       => {
		'fr' => 'ouzbékistan',
	},
	'vanuatu'                          => {
		'fr' => 'vanuatu',
	},
	'venezuela'                        => {
		'fr' => 'venezuela',
	},
	'vietnam'                          => {
		'fr' => 'viêt nam',
	},
	'yemen'                            => {
		'fr' => 'yémen',
	},
	'zambia'                           => {
		'fr' => 'zambie',
	},
	'zimbabwe'                         => {
		'fr' => 'zimbabwe',
	},
);

# returns a hash ref of countries in desirable language
sub GetListOfCountriesByCountryCode {
    my ($class, $countryFilter) = @_;

    my %output;
    return \%output unless $countryFilter;

    while ( my($country, $hData) = each %countries ) {
        while ( my ($key, $val) = each %$hData ) {
            next unless $key eq $countryFilter;
            $output{$val} = $country;
        }
    }

    return \%output;
}


1;
