package RPS::Import::Rdio::v1;

use strict;

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

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

use Data::Dumper;

sub _headerIdentifier { ( upc => 'EAN UPC' ) }

sub _fieldMap {
    {
        # Header
        dateBegin => 'A',

        # Data
        labelName    => 'I',
        countryCode  => 'C',
        formatType   => 'O',
        artistName   => 'F',
        upc          => 'E',
        albumName    => 'H',
        isrc         => 'D',
        trackName    => 'G',
        units        => 'J',
        currencyCode => 'M',
        price        => 'L',
        mediaType    => 'N',
    };
}

sub _processHeader {
    my $self = shift;

    unless ( $self->{_dateBegin} ) {
        my $dateStr = $self->_getByFieldName('dateBegin') || return;
        my ( $year, $month ) = $dateStr =~ /(\d{4}) (\d{2})/;

        $self->{_dateBegin} = "$month-$year" if ( $month && $year );
    }
}

sub _getDateBegin {
    my $self = shift;
    $self->fail("Unable to determine date from header") unless ( $self->{_dateBegin} );
    return $self->{_dateBegin};
}

sub productType {
    my $self = shift;

    if ( $self->isrc && $self->trackName ) {
        return RPS::File::Sale::TYPE_TRACK;
    } else {
        return RPS::File::Sale::TYPE_ALBUM;
    }
}

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

    if ( lc $format eq 'on-demand' ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( lc $format eq 'download' ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }

    $self->fail("Unknown format type '$format'");
}

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

    if ( lc $type eq 'audio' ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }

    $self->fail("Unknown media type '$type'");
}

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