#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package RPS::Import::Universal::v40;

use strict;

use Date::Calc qw(Days_in_Month);

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

sub _fieldMap {
    {
        dateBegin     => 'E',
        countryCode   => 'F',
        units         => 'I',
        price         => 'S',
        upc           => 'A',
        albumName     => 'B',
        isrc          => 'A',
        trackName     => 'B',
        formatType    => 'H',
        productType   => 'C',
        serviceID     => 'G',
    }
}

sub _headerIdentifier { ( dateBegin => 'Sales Period' ) }

sub currencyCode { 'EUR' }

sub countryCode {
    my $self = shift;

    my $ccode;
    my $cname = $self->_getByFieldName('countryCode');

    $cname = "Czech Republic" if( $cname =~ /^CZECH$/i );

    if( $cname =~ /^(\w{2,3})$/ ) # 2 or 3 character 
    {
        $ccode = $cname;
    }
    else
    {
        my $o = Common::Country->Get($cname);
        if( !$o )
        {
            $self->handleError("Unknown country: $cname", RPS::DB::Item::SaleImportError::kErrorCountry );
            return $cname;
        }
        $ccode = $o->alpha2();
    }
    return $ccode;
}

sub serviceID {
    my $self = shift;
    my $service = $self->_getByFieldName('serviceID');
    my $serviceID = $self->getServiceID(lc ($service)) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };
    return $serviceID if( $serviceID );

    return $self->handleError("Unknown service: $service", RPS::DB::Item::SaleImportError::kErrorService);
}

sub saleDates {
    my $self = shift;
    my $dt = $self->_getByFieldName('dateBegin');
    my $startDate;
    my $endDate;
    if( $dt =~ /^(\d{4})(\d{2})-(\d{4})(\d{2})$/ )  # YYYYMM-YYYYMM
    {
        my($sYear, $sMonth, $eYear, $eMonth) = ($1, $2, $3, $4);
        $startDate = sprintf("%04d-%02d-01", $sYear, $sMonth);
        $endDate = sprintf("%04d-%02d-%02d", $eYear, $eMonth, Days_in_Month($eYear, $eMonth));
    }
    return( $startDate, $endDate );
}

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

    if( $code =~ /^(B1|B4)$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    } 
    elsif( $code =~ /^(ID|IV)$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    } 
    return $self->handleError("Unknown product type: $code", RPS::DB::Item::SaleImportError::kErrorProductType);
}

sub formatType {
    my $self = shift;
    my $code = $self->_getByFieldName('formatType') || return;
    if( $code =~ /^PERMANENT DOWNLOAD$/i ||
        $code =~ /^TIMED OUT DOWNLOAD$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif( $code =~ /^AD FUNDED STREAMING$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    return $self->handleError("Unknown format: $code", RPS::DB::Item::SaleImportError::kErrorFormatType);
}

sub mediaType {
    my $self = shift;
    my $code = $self->_getByFieldName('productType') || return;
    
    if( $code =~ /^(ID|B1)$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    } 
    elsif( $code =~ /^IV|B4$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    } 
    return $self->handleError("Unknown media type: $code", RPS::DB::Item::SaleImportError::kErrorMediaType);
}

sub upc {
    my $self = shift;
    my $upc = $self->_getByFieldName('upc');
    if( !$self->productType )
    {
        # If we don't have a product type, see if the UPC looks like a UPC or an ISRC
        #
        return ( $upc =~ /^[a-z]{2}.+/i ) ?  undef  : $upc;
    }
    return ( $self->productType eq RPS::File::Sale::TYPE_ALBUM ) ? $upc : undef;
}

sub isrc {
    my $self = shift;
    my $isrc = $self->_getByFieldName('isrc');
    if( !$self->productType )
    {
        # If we don't have a product type, see if the ISRC looks like an ISRC or UPC
        #
        return ( $isrc =~ /^[a-z]{2}.+/i ) ? $isrc : undef;
    }
    return ( $self->productType eq RPS::File::Sale::TYPE_TRACK ) ? $isrc : undef;
}

sub albumName {
    my $self = shift;
    my $name = $self->_getByFieldName('albumName');
    if( !$self->productType )
    {
        # If we don't have a product type, peek at the UPC/ISRC field and make a best guess
        #
        return ( $self->SUPER::isrc =~ /^[a-z]{2}.+/i ) ?  undef  : $name;
    }
    return ($self->productType eq RPS::File::Sale::TYPE_ALBUM) ? $name : undef;
}

sub trackName {
    my $self = shift;
    my $name = $self->_getByFieldName('trackName');
    if( !$self->productType )
    {
        # If we don't have a product type, peek at the UPC/ISRC field and make a best guess
        #
        return ( $self->SUPER::isrc =~ /^[a-z]{2}.+/i ) ?  $name  : undef;
    }
    return ($self->productType eq RPS::File::Sale::TYPE_TRACK) ? $name : undef;
}

#####    MapFace    #####
#

sub _mapFaceCompatibleVersions {
    return( 40 );
}

sub _mapFaceServiceID {
    return('serviceID');
}

sub _mapFaceCountryCode {
    return('countryCode');
}

sub _mapFaceProductType {
    return('productType');
}

sub _mapFaceFormatType {
    return('formatType');
}

sub _mapFaceMediaType {
    return('productType');
}

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