package RPS::Import::GrayV;

use strict;

use Date::Calc qw(Days_in_Month Add_Delta_Days);

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;

use lib '/app/tools/data_classes/lib';

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

# This import supports versions 1 and 3.  Version 3 does not have the country column
# and will default to 'US', otherwise the file processing is the same.
#

# A - Count
use constant FIELD_DATE1   => 1;    # B - Start Date
use constant FIELD_DATE2   => 2;    # C - End Date
use constant FIELD_UNITS   => 3;    # D - Distribution
use constant FIELD_PRICE   => 4;    # E - Royalty
                                    # F - Extended Price
use constant FIELD_ARTIST  => 6;    # G - Artist
use constant FIELD_TRACK   => 7;    # H - Title
use constant FIELD_LABEL   => 8;    # I - Label
use constant FIELD_COUNTRY => 9;    # J - Country Of Sale
use constant FIELD_ISRC    => 9;    # J - ISRC only for v4

#public methods

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

    my $version = $args{file}->VersionNum();

    my $retval = undef;
    my $lines  = $args{lines};

    my $linenum = 1;
    foreach my $line (@$lines) {

        unless ( $line->[0] =~ m/^\d+$/ ) {
            $linenum++;
            next;
        }

        my $prodtype = RPS::File::Sale::TYPE_TRACK;
        my $format   = RPS::File::Sale::FORMAT_PERFORMANCE;
        my $artist   = $line->[FIELD_ARTIST];
        my $track    = $line->[FIELD_TRACK];
        my $label    = $line->[FIELD_LABEL];

        my ( $dateBegin, $dateEnd ) =
          ( $line->[FIELD_DATE1], $line->[FIELD_DATE2] );
        for my $date ( $dateBegin, $dateEnd ) {
            $date = $self->_getDate($date);
        }
        unless ( $dateBegin && $dateEnd ) {
            $self->errstr( "couldn't get dates from " . $line->[FIELD_DATE1] . " " . $line->[FIELD_DATE2] );
            return undef;
        }

        my $countryCode;
        if ( $version =~ /^(?:3|4)$/ ) {
            $countryCode = 'US';
        } else {
            my $country = $line->[FIELD_COUNTRY];
            if ( my $obj = Common::Country->Get($country) ) {
                $countryCode = $obj->alpha2();
            }
        }

        my ($units) = $line->[FIELD_UNITS] =~ m/^(\d+)$/;
        my ($price) = $self->_version == 4
            ? $line->[FIELD_PRICE + 1] =~ m/^(\d*\.?\d*)$/
            : $line->[FIELD_PRICE] =~ m/^(\d*\.?\d*)$/;

        if ( $self->_version == 4 ) {
            $price = $price / $units if $units;
        }

        if ( $units && defined $price && $artist && $dateBegin && $dateEnd && $track ) {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->labelName($label);
            $sale->units($units);
            $sale->price($price);
            $sale->countryCode($countryCode);
            $sale->lineNum($linenum);
            $sale->isrc( $line->[FIELD_ISRC] );
            $sale->serviceID( Client::Service::DSP_GRAYV ) if $version == 4;

            ##$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale( sale => $sale );
        }
        $linenum++;
    }
    return $linenum;
}

#
# Private methods
#

my $DATE_SEP = qr([/-]);

sub _getDate {
    my $self = shift;
    my $date = shift;

    my ( $y, $m, $d );
    if ( $date =~ /^(\d\d?) $DATE_SEP (\d\d?) $DATE_SEP (\d{4})$/x ) {
        ( $y, $m, $d ) = ( $3, $1, $2 );
    } elsif ( $date =~ /^(\d{4}) $DATE_SEP (\d\d?) $DATE_SEP (\d\d?)$/x ) {
        ( $y, $m, $d ) = ( $1, $2, $3 );
    } elsif ( $date =~ /^(\d+)$/ ) {
        ## stupid excel dates...
        ( $y, $m, $d ) = Add_Delta_Days( 1900, 1, 1, $1 - 2 );
    } else {
        return;
    }
    return sprintf( '%4d-%02d-%02d', $y, $m, $d );
}

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