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

use strict;

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

use RPS::Import::ServiceMap;

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

sub _fieldMap {
    {
        labelName     => 'A',
        serviceID     => 'L',
        countryCode   => 'O',
        productType   => 'E',
        formatType    => 'P',
        artistName    => 'C',
        clientProductID  => 'B',
        serviceProductID => 'J',
        upc           => 'G',
        albumName     => 'D',
        isrc          => 'H',
        trackName     => 'D',
        units         => 'U',
        price         => 'AC',
    }
}

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

sub currencyCode { 'USD' }

sub mediaType {
    my $self = shift;
    my $type = $self->_getByFieldName('mediaType');
    if( '' eq $type ||
        $type =~ /^192 track$/i ||
        $type =~ /^320 release$/i ||
        $type =~ /^320 track$/i ||
        $type =~ /^AIFF$/i ||
        $type =~ /^Album.Plus \(DRM-free\)$/i ||
        $type =~ /^AUDIO - Streaming - ADS$/i ||
        $type =~ /^Device Play Counts$/i ||
        $type =~ /^Device Plays$/i ||
        $type =~ /^Download$/i ||
        $type =~ /^Downloads$/i ||
        $type =~ /^DTO$/i ||
        $type =~ /^FLAC Release$/i ||
        $type =~ /^FLAC Track$/i ||
        $type =~ /^Korean Stream\/Download$/i ||
        $type =~ /^MOBILE$/i ||
        $type =~ /^MOBILE - Download$/i ||
        $type =~ /^MP3$/i ||
        $type =~ /^MP3 Release$/i ||
        $type =~ /^MP3 Track$/i ||
        $type =~ /^MUSIC - Streaming - ADS$/i ||
        $type =~ /^MUSIC-INTRODUCTORY-PLAN$/i ||
        $type =~ /^MUSIC-PREMIUM-PLAN$/i ||
        $type =~ /^Non-portable Stream$/i ||
        $type =~ /^On Demand$/i ||
        $type =~ /^Permanent MP3 A la Carte Tracks$/i ||
        $type =~ /^Play Counts$/i ||
        $type =~ /^Portable Stream$/i ||
        $type =~ /^Pre-cut ringtone$/i ||
        $type =~ /^Redeemed$/i ||
        $type =~ /^Song.Match Download.Plus \(DRM-free\)$/i ||
        $type =~ /^Song.Match Download.Standard \(DRMprotected\)$/i ||
        $type =~ /^Song.Plus \(DRM-free\)$/i ||
        $type =~ /^Stream$/i ||
        $type =~ /^Stream - Audio - UGC$/i ||
        $type =~ /^Streams AAC+$/i ||
        $type =~ /^Streams MP3$/i ||
        $type =~ /^Subscription - Annual/i ||    # starts with...
        $type =~ /^Subscription - Paid/i ||      # starts with...
        $type =~ /^Subscription Album Download$/i ||
        $type =~ /^Subscription Track Download$/i ||
        $type =~ /^UGC-block$/i ||
        $type =~ /^UGC-monetize$/i ||
        $type =~ /^UGC-track$/i ||
        $type =~ /^WAV$/i ||
        $type =~ /^wav release$/i ||
        $type =~ /^wav track$/i ||
        $type =~ /^WEB$/i ||
        $type =~ /^WEB - Download$/i ||
        $type =~ /^Web Plays$/i ||
        $type =~ /^Web\/Device Plays$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    elsif( $type =~ /^Music video./i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }
    $self->fail("Unable to get media type from '$type'");
}

sub serviceID {
    my $self = shift;
    my $service = $self->_getByFieldName('serviceID');
    my $serviceID = $self->getServiceID(lc ($service)) || $RPS::Import::ServiceMap::SERVICE_ALIASES{ lc $service };
    return $serviceID if( $serviceID );
    $self->fail("Unknown service '$service'");
}

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

    if( $type =~ /^Ringtone$/i ||
        $type =~ /^Track$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    }
    elsif( $type =~ /^Album$/i ||
           $type =~ /^Compilation$/i ||
           $type =~ /^EP$/i ||
           $type =~ /^Mini Album$/i ||
           $type =~ /^Single$/i ||
           $type =~ /^Video Single$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    }

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

sub formatType  {
    my $self = shift;
    my $format = $self->_getByFieldName('formatType');
    if( $format =~ /^(?:Conditional)?Download$/i ||
        $format =~ /^Ringtone$/i ||
        $format =~ /^PermanentDownload$/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif( $format =~ /^Cloud$/i ||
           $format =~ /^Downloads$/i  ||
           $format =~ /^(?:OnDemand|NonInteractive)?Stream$/i ||
           $format =~ /^Subscription$/i ||
           $format =~ /^streams$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif( $format =~ /^ERROR - New Delivery Type$/i )
    {
        return RPS::File::Sale::FORMAT_PERFORMANCE if( $self->serviceID == Client::Service::DSP_PANDORA ); # Case 19907
    }
    
    $self->fail( "Could not parse format type from '$format'" );
}

sub albumName { 
    my $self = shift;
    my $albumName = $self->_getByFieldName( 'albumName' ) || return;    
    return $self->productType eq RPS::File::Sale::TYPE_ALBUM ? $albumName : undef;   
}

sub trackName { 
    my $self = shift;
    my $trackName = $self->_getByFieldName( 'trackName' ) || return;    
    return $self->productType eq RPS::File::Sale::TYPE_TRACK ? $trackName : undef;   
}

sub isrc {
    my $self = shift;
    my $isrc = $self->_getByFieldName( 'isrc' );
    if ( $isrc ) {
        return Common::Util::normalize_isrc($isrc);
    }  
}

sub upc {
    my $self = shift;
    my $upc = $self->_getByFieldName( 'upc' );
    if ( $upc ) {
        return Common::Util::normalize_upc($upc);
    }  
}

sub saleDates {
    my $self = shift;
    return( $self->{_startDate}, $self->{_endDate} ) if( $self->{_startDate} && $self->{_endDate} );

    my $filename = $self->filename;
    if( $filename =~ /(\d{2})(\d{4})\.xls(?:x)?$/i || 
        $filename =~ /(\d{2})(\d{4})\.txt$/i ) # MMYYYY
    {
        my($month, $year) = ($1, $2);
        my $date = sprintf("%04d-%02d-01", $year, $month);

        ( $self->{_startDate}, $self->{_endDate} ) =
            $self->_getDates( date_begin => $date );
    }
    return( $self->{_startDate}, $self->{_endDate} );
}

sub price {
    my $self = shift;
    my $_price = $self->_getByFieldName('price');

    # deal with numbers from text files
    $_price = ($1 * -1) if( $_price =~ /\((\d+\.\d+)\)/ );
    $_price =~ s/ $//;

    return $_price;
}

sub units {
    my $self = shift;
    my $_units = $self->_getByFieldName('units');
    my $_price = $self->_getByFieldName('price');

    # deal with numbers from text files
    $_units =~ s/,//;
    $_units =~ s/ $//;
    $_price =~ s/ $//;
    $_units = ($1 * -1) if( $_units =~ /\((\d+)\)/ );
    $_price = ($1 * -1) if( $_price =~ /\((\d+\.\d+)\)/ );

    # Making units follow the sign of the revenue.
    # See https://royaltyshare.fogbugz.com/default.asp?W21 
    #
    if ( ($_price < 0 && $_units > 0) || ($_price > 0 && $_units < 0) ) {
        $_units *= -1;
    }

    return $_price ? $_units : undef;  # This will effectively skip lines with no revenue
}

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