package RPS::Import::Ogangi3;

use strict;

use lib '/app/tools/rps/lib';
use RPS::File::Sale;
use RPS::Import::ServiceMap;
use base qw/RPS::Import::MapFaceBase RPS::Import::Importer/;

sub _fieldMap {
    {
        # header
        #
        dateBegin    => 'B',
        currencyCode => 'N',

        # detail
        #
        countryCode      => 'C',
        artistName       => 'Q',
        serviceProductID => 'A',
        upc              => 'P',
        trackName        => 'B',
        isrc             => 'O',
        units            => 'I',
        price            => 'N',
    };
}

sub _headerIdentifier { ( countryCode => 'Territory' ) }
sub productType       { RPS::File::Sale::TYPE_TRACK }
sub formatType        { RPS::File::Sale::FORMAT_RINGTONE }
sub mediaType         { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub saleDates {
    my $self = shift;
    return ( $self->{_startDate}, $self->{_endDate} );    # from header; see _processHeader
}

sub currencyCode {
    my $self = shift;
    return $self->{_currencyCode};                        # from header; see _processHeader
}

# This is called for every row up to and including the header row
#
sub _processHeader {
    my $self = shift;

    unless ( $self->{_startDate} ) {
        my $dt = $self->_getByFieldName('dateBegin');
        if ( $dt =~ /^(\d{1,2} \w+ \d{4}) - (\d{1,2} \w+ \d{4})$/ ) {
            my $startDate = $1;
            my $endDate   = $2;
            ( $self->{_startDate}, $self->{_endDate} ) = $self->_getDates( date_begin => $1, date_end => $2 );
        }
    }

    unless ( $self->{_currencyCode} ) {
        my $cc = $self->_getByFieldName('currencyCode');
        ( $self->{_currencyCode} ) = $1 if ( $cc =~ /^(\w{3})\s/ );
    }
}

sub countryCode {
    my $self = shift;

    my $name    = $self->_getByFieldName('countryCode') || "";

    return "VG" if ( $name =~ /^British Virgin Islands$/i );
    return "KN" if ( $name =~ /^Saint Kitts & Nevis$/i );
    return "VC" if ( $name =~ /^Saint Vincent & the Grenadines$/i );
    return "TC" if ( $name =~ /^Turks and Caicos$/i );

    my $country = Common::Country->Get($name);
    if ($country) {
        return $country->alpha2();
    }

    $self->handleError( "Unknown country: '$name'.", RPS::DB::Item::SaleImportError::kErrorCountry );
    return $name;
}

sub _mapFaceProductType { 'productType'}
sub _mapFaceServiceID   { 'serviceID' }
sub _mapFaceCountryCode { 'countryCode' }
sub _mapFaceFormatType  { 'formatType' }
sub _mapFaceMediaType   { 'mediaType' }

1;
