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

package RPS::Import::Zed::v1;

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 RPS::Import::ServiceMap;

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

sub _fieldMap {
    {
        # From summary tab
        #
        countryCode      => 'A',

        # detail section on realtone and ringback tone tabs
        # (the columns are the same for both worksheets)
        #
        dateBegin        => 'R',
        labelName        => 'A',
        productType      => 'F',
        serviceProductID => 'D',
        artistName       => 'M',
        isrc             => 'L',
        trackName        => 'E',
        units            => 'G',
        price            => 'J',
    }
}

sub _headerIdentifier { ( isrc => 'Song Code' ) }

sub currencyCode { 'USD' }

sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub saleDates {
    my $self = shift;
    my $startDate = $self->_getByFieldName('dateBegin');

    my $dateBegin;
    my $dateEnd;

    if( $startDate =~ /\d{5}/ )
    {
        ($dateBegin,$dateEnd) = $self->_getDates( date_begin => $startDate, type => 'msft' );
    }
    else
    {
        ($dateBegin,$dateEnd) = $self->_getDates( date_begin => $startDate );
    }

    return( $dateBegin, $dateEnd );
}

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    if( $type =~ /^realtone$/i || $type =~ /^ringback tone$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    }
    $self->fail("Unknown product type '$type'");
}

sub formatType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    if( $type =~ /^realtone$/i || $type =~ /^ringback tone$/i )
    {
        return RPS::File::Sale::FORMAT_RINGTONE;
    }
    $self->fail("Unknown format type '$type'");
}

sub countryCode {
    my $self = shift;
    return $self->{_countryCode} if ( $self->{_countryCode} );
}

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

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


sub _preProcess {
    my( $self, %args) = @_;
    $self->SUPER::_preProcess(%args);

    my $sheetNames = $args{sheet_names};

    # Get the country information from the summary tab
    #
    my $sheetCount = 0;
    SHEET: foreach my $sheet(@{ $args{sheets} })
    {
        my $sheetName = $sheetNames->[$sheetCount];

        next if( $sheetName !~ /^summary$/i );

        foreach my $line(@$sheet)
        {
            $self->{line} = $line;

            my $date = $self->_getByFieldName( 'countryCode' );

            if( $date =~ /zed usa/i )
            {
                $self->{_countryCode} = 'US';
                last SHEET;
            }
        }
        $sheetCount++;
    }
}


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