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

package RPS::Import::Merlin::v21;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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

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

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

sub _fieldMap {
    {
        # header
        dateBegin     => 'A',
        dateEnd       => 'B',

        # detail
        countryCode   => 'D',
        currencyCode  => 'M',
        labelName     => 'C',
        formatType    => 'R',
        artistName    => 'G',
        upc           => 'F',
        albumName     => 'I',
        isrc          => 'E',
        trackName     => 'H',
        units         => 'J',
        price         => 'L',
        mediaType     => 'N',
    }
}

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

sub productType { RPS::File::Sale::TYPE_TRACK }

sub mediaType {
    my $self = shift;
    my $type = $self->_getByFieldName('mediaType');
    if( $type =~ /^audio$/i )
    {
        return RPS::DB::Item::MediaType::kMediaTypeAudio;
    }
    $self->fail("Unknown media type '$type'");
}

sub saleDates {
    my $self = shift;
    return ($self->{_startDate}, $self->{_endDate}) if( $self->{_startDate} ); # from header; see _preProcess
}

sub formatType {
    my $self = shift;
    my $fmt = $self->_getByFieldName('formatType');
    if( $fmt =~ /^play$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    elsif( $fmt =~ /^rt$/i )
    {
        return RPS::File::Sale::FORMAT_TETHERED;
    }
    elsif( $fmt =~ /^rbt$/i )
    {
        return RPS::File::Sale::FORMAT_RINGTONE;
    }
    $self->fail("Unknown format type '$fmt'");
}

sub serviceID { Client::Service::DSP_CRICKET }

sub countryCode {
    my $self = shift;
    my $name = $self->_getByFieldName('countryCode');
    my $o = Common::Country->Get( $name );
    if( $o )
    {
        return $o->alpha2;
    }
    $self->fail("Unrecognized country '$name'");
}

sub _preProcess {
    my $self = shift;
    my %args = @_;

    Log->info( "Starting _preProcess" );
    $self->SUPER::_preProcess(%args);

    my $foundHeaderRow; # set when we find the top of the header section

    my $linenum = 0;
    foreach my $line(@{ $args{lines} })
    {
        $self->{line} = $line;

        my $dt = $self->_getByFieldName('dateBegin');

        if( !$foundHeaderRow )
        {
            # in more recent sales files, there are some spurious (but consistent)
            # characters before the S in startdate.  I'm removing the ^ to allow
            # this header to still be recognized, FB Case 1198
            if( $dt =~ /startdate$/i )
            {
                $foundHeaderRow = 1;
            }
        }
        else
        {
            # The dates are in the header section
            #
            my $startDate = $self->_getByFieldName('dateBegin');
            my $endDate   = $self->_getByFieldName('dateEnd');

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


            last;
        }

        $linenum++;
    }

}

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