package RPS::Import::PCMusic;

use strict;

use lib '/app/tools/rps/lib';
use RPS::File::Sale;

use base 'RPS::Import::Importer';

sub _fieldMap {
    my $self      = shift;
    my %field_map = (
        1 => {
            dateBegin    => 'A',
            dateEnd      => 'B',
            countryCode  => 'D',
            isrc         => 'F',
            mediaType    => 'G',
            units        => 'H',
            currencyCode => 'K',
            price        => 'J',
            artistName   => 'L',
            albumName    => 'M',
            trackName    => 'N',
        },
        2 => {
            dateBegin        => 'A',
            dateEnd          => 'B',
            countryCode      => 'D',
            serviceProductID => 'E',
            isrc             => 'F',
            mediaType        => 'G',
            units            => 'H',
            price            => 'J',
            artistName       => 'K',
            albumName        => 'L',
            trackName        => 'M',
        },
        3 => {
            countryCode      => 'D',
            dateBegin        => 'A',
            dateEnd          => 'B',
            serviceProductID => 'E',
            artistName       => 'K',
            isrc             => 'F',
            trackName        => 'L',
            units            => 'H',
            price            => 'J',
            mediaType        => 'G',
        },
    );
    return $field_map{ $self->_version };
}

sub _headerIdentifier { ( dateBegin => 'Start Date' ) }

sub productType { RPS::File::Sale::TYPE_TRACK }
sub formatType  {
    my $self = shift;

    return RPS::File::Sale::FORMAT_BACKGROUNDMUSIC if $self->_version =~ /^(?:2|3)$/;
    return RPS::File::Sale::FORMAT_STREAM;
}

sub currencyCode {
    my $self = shift;
    return 'USD' if ( $self->_version == 2 );
    return $self->SUPER::currencyCode;
}

sub isrc {
    my $self = shift;
    my $isrc = $self->SUPER::isrc;

    return undef if ( $isrc =~ /^na$/i );

    return $isrc;
}

sub mediaType {
    my $self = shift;

    my $mediaType = $self->_getByFieldName('mediaType') || '';

    if ( $mediaType =~ /^audio$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    } elsif ( $mediaType =~ /^video$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }

    return;
}

sub _processLine {
    my $self = shift;

    return $self->SUPER::_processLine(@_) if ( $self->_version == 1 );

    # Due to a quirk in our CSV parsing, we're not properly recognizing
    # commas escaped with a backslash.  E.g., something like
    # "Luckenbach\, Texas (Back To The Basics Of Love)"
    # will be split into two separate fields ("Luckenbach\" and
    # "Texas (Back To The Basics Of Love)") rather than a single field.
    # The following will look for fields ending in a backslash and if
    # found, will "unsplit" that field with the following field.
    #
    if ( $self->filename =~ /\.csv/i ) {
        my $album = $self->_getByFieldName('albumName');
        if ( $album =~ /\\$/ )    # ends in backslash (escape); check for data in following columns
        {
            my $i = @{ $self->{line} } - 1;
            foreach my $cell ( reverse @{ $self->{line} } ) {
                my $current = @{ $self->{line} }[$i];
                my $previous = ( $i > 0 ) ? @{ $self->{line} }[ $i - 1 ] : '';
                if ( $previous =~ /\\$/ ) {
                    $previous =~ s/\\$/, /;
                    $current =~ s/\\'/'/g;
                    @{ $self->{line} }[ $i - 1 ] = $previous . $current;    # merge the split elements
                    splice @{ $self->{line} }, $i, 1;                       # remove the now extra element that was merged
                }
                $i--;
            }
        }
    }

    return $self->SUPER::_processLine(@_);
}

###
1;                                                                          #
###
