package RPS::Import::Caroline::v24;

use strict;
use warnings;

use Date::Calc qw(Days_in_Month Decode_Month);

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

use base qw/RPS::Import::MapFaceBase RPS::Import::Importer/;

my %formatMediaMap = (
    'digital online aud track stream' => {
        formatType => RPS::File::Sale::FORMAT_STREAM,
        mediaType  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'digital online aud track stream - subscr' => {
        formatType => RPS::File::Sale::FORMAT_STREAM,
        mediaType  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'audio track stream portable - subscr' => {
        formatType => RPS::File::Sale::FORMAT_STREAM,
        mediaType  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'audio cloud services - subscr' => {
        formatType => RPS::File::Sale::FORMAT_STREAM,
        mediaType  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'digital allocations streams (audio)' => {
        formatType => RPS::File::Sale::FORMAT_STREAM,
        mediaType  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
    'digital online aud track permanent' => {
        formatType => RPS::File::Sale::FORMAT_DOWNLOAD,
        mediaType  => RPS::DB::Item::MediaType::kMediaTypeAudio,
    },
);

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        24 => {
            dateBegin    => 'H1',
            #dateEnd      => 'H1',
            productType  => 'E',
            formatType   => 'G',
            artistName   => 'B',
            upc          => 'C',
            albumName    => 'D',
            isrc         => 'E',
            trackName    => 'F',
            units        => 'H',
            currencyCode => 'I4',
            price        => 'I',
            mediaType    => 'G4',
        },
    );

    return $fieldMap{ $self->_version };
}

sub _headerIdentifier { upc => 'UPC' }

sub countryCode {
    my $self = shift;

    my ($country) = $self->filename =~ /Caroline\s+([A-Z]{2})\s+/i;
    my $oCountry = Common::Country->new( search => $country );
    return $oCountry->alpha2 if $oCountry;

    $self->fail("Unable to determine country code from '$country'");
}

sub saleDates {
    my $self = shift;

    my $period = $self->_getByFieldName('dateBegin') || '';
    $period =~ /^\d?(\d{2})\.(\d{4})\.\.\d?(\d{2})\.(\d{4})$/;
    my $startDate = sprintf "%04d-%02d-%02d", $2, $1, 1;
    my $endDate   = sprintf "%04d-%02d-%02d", $4, $3, Days_in_Month( $4, $3 );

    return $self->_getDates( date_begin => $startDate, date_end => $endDate );
}

sub productType {
    my $self = shift;

    my $isrc = $self->_getByFieldName('isrc') || '';
    return $isrc ? RPS::File::Sale::TYPE_TRACK : RPS::File::Sale::TYPE_ALBUM;
}

sub formatType {
    my $self = shift;

    my $type = lc( $self->_getByFieldName('formatType') || '' );
    if ( exists $formatMediaMap{$type} ) {
        return $formatMediaMap{$type}{formatType};
    }

    $self->fail("Could not parse fortmat type from '$type'");
}

sub mediaType {
    my $self = shift;

    my $type = lc( $self->_getByFieldName('formatType') || '' );
    if ( exists $formatMediaMap{$type} ) {
        return $formatMediaMap{$type}{mediaType};
    }

    $self->fail("Could not parse media type from '$type'");
}

# mapFace
sub _mapFaceServiceID {
    return;
}

sub _mapFaceCountryCode {
    return;
}

sub _mapFaceProductType {
    return ('productType');
}

sub _mapFaceFormatType {
    return ('formatType');
}

sub _mapFaceMediaType {
    return ('formatType');
}

sub _mapFaceCompatibleVersions {
    return ( 2, 6, 14, 19, 20, 24 );
}

1;