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

use strict;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Client;

use lib '/app/tools/common/lib';
use Common::Consts;
use Common::Assert;
use Common::Log;

use lib '/app/tools/data_classes/lib';

use lib '/app/tools/rps/lib';

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

sub _fieldMap {
    {
        albumName        => 'E',
        countryCode      => 'M',
        currencyCode     => 'N',
        isrc             => 'D',
        labelName        => 'H',
        price            => 'P',
        serviceID        => 'K',
        serviceProductID => 'A',
        trackName        => 'B',
        trackNum         => 'I',
        type             => 'O',
        upc              => 'G',
        units            => 'Q',
    };
}

sub _unverifiedFieldMap {
    {
        album_artist => 'F',
        track_artist => 'C',
    }

}

sub artistName {

    my $self = shift;
    my $type = $self->_getByFieldName('type');

    my $artist;

    if ( $type =~ /single full-track/i ) {
        $artist = $self->_getByFieldName('album_artist');
    } elsif ( $type =~ /album full-track/i ) {
        $artist = $self->_getByFieldName('track_artist');
    } else {
        $self->fail("Unknown type '$type'");
    }

    return $artist;
}

sub serviceID {
    my $self    = shift;
    my $service = $self->_getByFieldName('serviceID');

    my $serviceID = $self->getServiceID($service) if ($service);

    $self->fail("Unknown service '$service'") unless ($serviceID);

    return $serviceID;
}

sub units {
    my $self  = shift;
    my $units = $self->_getByFieldName('units');
    return ( defined $units ) ? $units : 1;
}

sub _headerIdentifier { ( upc => 'Album Upc' ) }

sub _productTypes {
    {
        'single full-track' => {
            productType => RPS::File::Sale::TYPE_TRACK,
            formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
            mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
        'album full-track' => {
            productType => RPS::File::Sale::TYPE_ALBUM,
            formatType  => RPS::File::Sale::FORMAT_DOWNLOAD,
            mediaType   => RPS::DB::Item::MediaType::kMediaTypeAudio,
        },
    };
}

# formatType - get the format from the filename (this effectively overrides
#  the format type returned by _productTypes)
#
sub formatType {
    my $self = shift;
    return _getFormat( $self->filename );
}

sub _getFormat {
    my ($f) = @_;

    my $format;

    return if ( !$f );

    if ( $f =~ m/dto_/i ) {
        $format = RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $f =~ m/redeemed_/i ) {
        $format = RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $f =~ m/stream_/i || $f =~ m/streamazp_/i || $f =~ m/streaming_/i ) {
        $format = RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $f =~ m/playcount_/i ) {
        $format = RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $f =~ m/subscription_/i ) {
        $format = RPS::File::Sale::FORMAT_STREAM;
    }
    return $format;
}

sub saleDates {
    my $self = shift;
    assert( $self->filename, "Unknown filename" );

    my $fileName = $self->filename;

    return ( $self->{_startDate}, $self->{_endDate} ) if ( $self->{_startDate} );

    unless ( $fileName =~ /(\d{4})(\d{2})(\d{2})_(\d{4})(\d{2})(\d{2})/ ) {
        die "Could not get date from filename: $fileName";
    }

    my ( $startDate, $endDate );

    $startDate = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    $endDate   = sprintf( "%04d-%02d-%02d", $4, $5, $6 );

    $self->{_startDate} = $startDate;
    $self->{_endDate}   = $endDate;

    return ( $startDate, $endDate );
}

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