#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::Universal::v14;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::UTF8;
use Common::Date;

use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/rps/lib';
use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        dateBegin        => 'L',
        productType      => 'H',
        formatType       => 'J',
        artistName       => 'B',
        upc              => 'G',
        isrc             => 'F',
        trackName        => 'D',
        albumName        => 'C',
        price            => 'P',
        units            => 'O',
        countryCode      => 'M',
        mediaType        => 'I',
    }
}

sub _headerIdentifier { ( dateBegin => 'Calendar Month' ) }

sub currencyCode { 'USD' }

sub countryCode {
    my $self = shift;
    my $name = $self->_getByFieldName('countryCode');

    my %countryMap = (
        'bonaire'                  => 'nl',
        'bosnia and herzegowina'   => 'ba',
        'central america'          => 'sv', # El Salvador
        'china export'             => 'cn',
        'curacao'                  => 'nl',
        'east timor'               => 'tl',
        'faeroe islands'           => 'fo',
        'falklands islands (malvinas)' => 'fk',
        'micronesia'               => 'fm',
        'sint maarten'             => 'nl',
        'south sudan'              => 'sd',
        'st. helena'               => 'sh',
        'st. kitts-nevis'          => 'kn',
        'st. pierre and miquelon'  => 'pm',
        'svalbard  mayen islands'  => 'sj',
        'unknown'                  => 'us',
        'vatican city state (holy see)' => 'va',
        'virgin islands (british)' => 'vg',
    );

    my $_country =  $countryMap{ lc $name } || $name;

    my $obj = Common::Country->Get( $_country );
    if ( !$obj )
    {
        $self->errstr("Unknown country '$name' in line number: ". $self->linenum );
        return undef;
    }
    return $obj->alpha2();
}

sub saleDates {
    my $self = shift;

    my $dateBegin;
    my $dateEnd;

    my $startDate = $self->_getDateBegin();
    my $_startDate;

    if( $startDate =~ /(\w+) (\d{2,4})/ ) # month YY[YY]
    {
        my $month = Decode_Month($1);
        my $year  = $2;
        $year += 2000 if( $year < 99 );
        $_startDate = sprintf("%04d-%02d-%02d", $year, $month, 1);
    } elsif ($startDate =~ m/^\d+$/) {
        $_startDate = new Common::Date(date => $startDate, type => 'excel');    
    }    
     
    if ($_startDate) {
        ($dateBegin,$dateEnd) = $self->_getDates( date_begin => $_startDate ); 
    }
        
    return ($dateBegin, $dateEnd);
}

sub price {
    my $self = shift;
    my $_price = $self->_getByFieldName('price') || return;
    $_price =~ s/\$//;
    if( $_price =~ /^\((\d+\.?\d*)\)$/ )
    {
        $_price = $1 * -1;
    }
    return $_price;
}

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName( 'productType' ) || return;

    if( $type =~ /^(album|single)$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    } 
    elsif( $type =~ /^track$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    } 
    else
    {
        $self->fail( "Could not parse product type from '$type'" );
    }
}

sub trackName {
    my $self = shift;
    my $name = $self->_getByFieldName('trackName');
    return $name if( $self->productType eq RPS::File::Sale::TYPE_TRACK );
}

sub formatType  {
    my $self = shift;
    my $type = $self->_getByFieldName('formatType');

    if( $type =~ /^Play \/ Stream Portable \(Subscr\.\)$/i ||
        $type =~ /^Play \/ Stream \(Free Service\)$/i      ||
        $type =~ /^Soft bundled subscription plays$/i      ||
        $type =~ /^Hard bundled subscription plays$/i      ||
        $type =~ /^Fixed Plays$/i                          ||
        $type =~ /^Play \/ Stream \(Subscr\.\)$/i          ||
        $type =~ /^Cached Plays$/i                         ||
        $type =~ /^Ephemeral Play$/i                       ||
        $type =~ /^Play \/ Stream \(B2B\)$/i        ||
        $type =~ /^Play \/ Stream \(Webcasting\)$/i        ||
        $type =~ /^Ad-funded portable webcasting$/i        ||   # FB 9215
        $type =~ /^User Generated Content Stream$/i        ||
        $type =~ /Subscription\s+Webcasting/i              ||
        $type =~ /^Unlimited Subscription \(Mobile\)$/i    ||
        $type =~ /^Premium Webcasting$/i                   ||
        $type =~ /^Play \/ Stream subscription via mobile application$/i ||
        $type =~ /^Play \/ Stream \(Mobile OTA\)$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif( $type=~ /^Permanent Download \(burnable\)$/i ||
           $type=~ /^Mobile Download$/i ||
           $type=~ /^Permanent Download \(Prepaid Bundles\/Tracks\)$/i ||
           $type=~ /^Permanent Download \(SMP\)$/i ||
           $type=~ /^Rental$/i ||
           $type=~ /^Mobile Download \(Regular Phone\)$/i ||
           $type=~ /^Mobile Download \(Smartphone\)$/i ||
           $type=~ /^Kiosk$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif( $type=~ /^Permanent Download \(Subscription\)$/i ||
           $type=~ /^Temporary Download$/i )
    {
        return RPS::File::Sale::FORMAT_TETHERED;
    }
    elsif( $type=~ /^Callrings \/ Ringbacks with Music$/i ||
           $type=~ /^Ringtones \(poly\)$/i ||
           $type=~ /^Master \(Real\) Tones$/i )
    {
        return RPS::File::Sale::FORMAT_RINGTONE;
    }
    else
    {
        $self->fail( "Could not parse format type from '$type'" );
    }
}

sub mediaType {
    my $self = shift;
    my $type = $self->_getByFieldName('mediaType');

    if( $type =~ /^(bundle|track)$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    elsif( $type =~ /^video$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }
    else
    {
        $self->fail( "Could not parse media type from '$type'" );
    }
}

###
1;# Play nicely.
###
