#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2013 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::WMG::WarnerMobile3;

use strict;

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

use Spreadsheet::ParseExcel;

sub _fieldMap {
    {
        dateBegin   => 'B',
        artistName  => 'G',
        upc         => 'J',
        trackName   => 'H',
        serviceID   => 'D',
        units       => 'P', 
        price       => 'X', 
    }
}

sub _unverifiedFieldMap {
    {
        config => 'I',
    }
};

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

sub countryCode  { 'CA' }
sub currencyCode { 'CAD' }

sub saleDates {
    my $self = shift;
    my $startDate;
    my $endDate;

    my $dt = $self->_getByFieldName('dateBegin');
    if( $dt =~ /\d{5}/ )
    {
        my $_sdate = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $dt );
        ($startDate, $endDate) = $self->_getDates( date_begin => $_sdate );
    }
    else
    {
        ($startDate, $endDate) = $self->_getDates( date_begin => $dt );
    }

    return( $startDate, $endDate );
}

sub serviceID {
    my $self = shift;

    my $service = $self->_getByFieldName( 'serviceID' );
    my $id = $self->getServiceID($service) || $RPS::Import::ServiceMap::SERVICE_ALIASES{lc $service};

    $self->fail("Unknown service: $service") if (!$id);

    return $id;
}


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

    if( $config =~ /^(mm|sa|sv|ta|tv|is)/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    }

    $self->fail("Unable to determine product from: $config");
}

sub formatType  {
    my $self = shift;
    my $config = $self->_getByFieldName( 'config' );

    if( $config =~ /^(sa|sv|ta|tv)/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif( $config =~ /^mm/i )
    {
        return RPS::File::Sale::FORMAT_RINGTONE;
    }
    elsif( $config =~ /^is/i )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }

    $self->fail("Unable to determine format from: $config");
}

sub mediaType {
    my $self = shift;
    my $config = $self->_getByFieldName( 'config' );

    if( $config =~ /^(mm|sa|ta|is)/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    elsif( $config =~ /^(tv|sv)/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }

    $self->fail("Unable to determine media type from: $config");
}

sub artistName {
    my $self = shift;
    my $name = $self->_getByFieldName( 'artistName' );

    my $artistName = $name;

    if( $name =~ /(\w+),\s+(.*)$/i )  # lastname, firstname
    {
        $artistName = $2 . " " . $1;
    }
    return $artistName;
}

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