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

package RPS::Import::WMG::WarnerAUDigital;

use strict;

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

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

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

sub _fieldMap {
    {
        # header
        dateBegin => 'B5',

        # detail
        artistName      => 'A',
        clientProductID => 'D',
        units           => 'J',
        price           => 'N',
    };
}

sub _unverifiedFieldMap {
    {
        config => 'E',
        title  => 'B',
    };
}

sub _headerIdentifier { ( artistName => 'Artist' ) }

sub countryCode { 'AU' }

sub currencyCode { 'AUD' }

sub productType {
    my $self   = shift;
    my $config = $self->_getByFieldName('config');
    if ( $config =~ /^(DEPA|DLP)$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif ( $config =~ /^(DSING|DVSING|IMAGEWVID)$/i ) {
        return RPS::File::Sale::TYPE_TRACK;
    }
    $self->fail("Unable to get product type from '$config'");
}

sub formatType {
    my $self   = shift;
    my $config = $self->_getByFieldName('config');
    if ( $config =~ /^(DEPA|DLP|DSING|DVSING)$/i ) {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    } elsif ( $config =~ /^IMAGEWVID$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    $self->fail("Unable to get format type from '$config'");
}

sub mediaType {
    my $self   = shift;
    my $config = $self->_getByFieldName('config');
    if ( $config =~ /^(DEPA|DLP|DSING)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    } elsif ( $config =~ /^(DVSING|IMAGEWVID)$/i ) {
        return RPS::DB::Item::MediaType::kMediaTypeVideo;
    }
    $self->fail("Unable to get media type from '$config'");
}

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

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

sub saleDates {
    my $self = shift;

    if ( !$self->{_dateBegin} && !$self->{_dateEnd} ) {
        my $dt = $self->_getByFieldName('dateBegin');
        if ( $dt =~ /\d{5}/ ) {    # Excel format, days since 1/1/1900
            ( $self->{_dateBegin}, $self->{_dateEnd} ) = $self->_getDates( date_begin => $dt, type => "msft" );
        } else {
            ( $self->{_dateBegin}, $self->{_dateEnd} ) = $self->_getDates( date_begin => $dt );
        }
    }

    return ( $self->{_dateBegin}, $self->{_dateEnd} );
}

sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    return $self->SUPER::_processSheet( $sheet, $sheetnum ) if ( $self->{sheetname} =~ /^digital/i );
}

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