#------------------------------------------------------------
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Common::Postal::US;

use strict;
use warnings;

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

use base 'Common::Postal';

my %gState = (
    'AL' => 'Alabama',
    'AK' => 'Alaska',
    'AZ' => 'Arizona',
    'AR' => 'Arkansas',
    'CA' => 'California',
    'CO' => 'Colorado',
    'CT' => 'Connecticut',
    'DE' => 'Delaware',
    'DC' => 'District of Columbia',
    'FL' => 'Florida',
    'GA' => 'Georgia',
    'HI' => 'Hawaii',
    'ID' => 'Idaho',
    'IL' => 'Illinois',
    'IN' => 'Indiana',
    'IA' => 'Iowa',
    'KS' => 'Kansas',
    'KY' => 'Kentucky',
    'LA' => 'Louisiana',
    'ME' => 'Maine',
    'MD' => 'Maryland',
    'MA' => 'Massachusetts',
    'MI' => 'Michigan',
    'MN' => 'Minnesota',
    'MS' => 'Mississippi',
    'MO' => 'Missouri',
    'MT' => 'Montana',
    'NE' => 'Nebraska',
    'NV' => 'Nevada',
    'NH' => 'New Hampshire',
    'NJ' => 'New Jersey',
    'NM' => 'New Mexico',
    'NY' => 'New York',
    'NC' => 'North Carolina',
    'ND' => 'North Dakota',
    'OH' => 'Ohio',
    'OK' => 'Oklahoma',
    'OR' => 'Oregon',
    'PA' => 'Pennsylvania',
    'RI' => 'Rhode Island',
    'SC' => 'South Carolina',
    'SD' => 'South Dakota',
    'TN' => 'Tennessee',
    'TX' => 'Texas',
    'UT' => 'Utah',
    'VT' => 'Vermont',
    'VA' => 'Virginia',
    'WA' => 'Washington',
    'WV' => 'West Virginia',
    'WI' => 'Wisconsin',
    'WY' => 'Wyoming',

    'AS' => 'American Samoa',
    'FM' => 'Federated States of Micronesia',
    'GU' => 'Guam',
    'MH' => 'Marshall Islands',
    'MP' => 'Northern Mariana Islands',
    'PW' => 'Palau',
    'PR' => 'Puerto Rico',
    'VI' => 'Virgin Islands',

    'AA' => 'Armed Forces (AA)',
    'AE' => 'Armed Forces (AE)',
    'AP' => 'Armed Forces (AP)',

);

# invert the name/abbreviation mappings, so we can
# easily search for state abbreviations

my %gStateAbbr;
map { $gStateAbbr{ lc( $gState{$_} ) } = $_ } keys %gState;

# also add any creative names people come up with
# !!! These should all be in lower case !!!
$gStateAbbr{'f.s. of micronesia'}                    = 'FM';
$gStateAbbr{'Virgin Islands, U.S.'}                  = 'VI';
$gStateAbbr{'usvirgin islands'}                      = 'VI';
$gStateAbbr{'u.s. virgin islands'}                   = 'VI';
$gStateAbbr{'u.s.virgin islands'}                    = 'VI';
$gStateAbbr{'Armed Forces Africa'}                   = 'AE';
$gStateAbbr{'Armed Forces Americas'}                 = 'AA';
$gStateAbbr{'Armed Forces Americas (except Canada)'} = 'AA';
$gStateAbbr{'Armed Forces Canada'}                   = 'AE';
$gStateAbbr{'Armed Forces Europe'}                   = 'AE';
$gStateAbbr{'Armed Forces Middle East'}              = 'AE';
$gStateAbbr{'Armed Forces Pacific'}                  = 'AP';

sub new {
    my ($class) = @_;

    my $self = bless {}, $class;

    return $self;
}

sub country {
    my ( $self, $abbr ) = @_;

    if ( defined($abbr) && $self->getName($abbr) ) {
        my %trueCountry = (
            'AS' => 'AS',
            'FM' => 'FM',
            'GU' => 'GU',
            'MH' => 'MH',
            'MP' => 'MP',
            'PR' => 'PR',
            'PW' => 'PW',
            'VI' => 'VI',
        );

        return $trueCountry{$abbr} || 'US';
    }

    return $self->SUPER::country();
}

sub getName {
    my ( $self, $abbr ) = @_;

    return $gState{ uc($abbr) };
}

sub getAbbr {
    my ( $self, $name ) = @_;

    return $gStateAbbr{ lc($name) };
}

sub getAbbrList {
    return \%gStateAbbr;
}

sub formatPostalCode {
    my ( $self, $postalCode ) = @_;

    my $formatted;

    if ( length($postalCode) <= 5 ) {
        if ( $postalCode =~ /^\d\d\d+$/ ) {
            $formatted = sprintf( '%05d', $postalCode );
        }
    } else {
        if ( $postalCode =~ /^\d{5}\-\d{4}$/ ) {
            $formatted = $postalCode;
        }

        if ( $postalCode =~ /^(\d{5})\-$/ ) {
            $formatted = $1;
        }

        if ( $postalCode =~ /^(\d{5})\s?(\d{4})$/ ) {
            $formatted = $1 . "-" . $2;
        }
    }

    return $formatted;
}

1;
