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',
        },
    );
    return $field_map{$self->_version};
}

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

sub productType { RPS::File::Sale::TYPE_TRACK }
sub formatType  { 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->SUPER::mediaType;
    
    if (lc($mediaType) eq 'audio')
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    elsif (lc($mediaType) eq 'video')
    {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }

    return undef;
}

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;#
###
