package RPS::Import::GillianWelch;

use strict;

use Date::Calc qw(Days_in_Month);

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

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

use constant FIELD_LABEL      => 0;
use constant FIELD_ARTIST     => 2;
use constant FIELD_ALBUM      => 3;
use constant FIELD_TRACK      => 4;
use constant FIELD_UPC        => 5;
use constant FIELD_ISRC       => 8;
use constant FIELD_UNITS      => 9;
use constant FIELD_PRICE      => 10;
use constant FIELD_PROD_TYPE  => 12;
use constant FIELD_PROD_ID    => 14;
use constant FIELD_DATE_BEGIN => 16;

#use constant FIELD_DATE_END   => 17;

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

    die "Gillian Welch is no longer supported";

    return undef unless $lines;

    my $linenum;
    foreach my $sheet ( @{ $args{sheets} } ) {
        $linenum = 2;
        foreach my $line ( @{$sheet} ) {
            my $date_begin = $line->[FIELD_DATE_BEGIN];
            next unless ( $date_begin =~ m/^\d+$/ );    # skip header
            my ( $dateBegin, $dateEnd ) = _get_dates($date_begin);
            unless ( $dateBegin && $dateEnd ) {
                $self->errstr("failed getting dates: $date_begin");
                return undef;
            }

            my $prodType = lc( $line->[FIELD_PROD_TYPE] ) eq 't' ? RPS::File::Sale::TYPE_TRACK : RPS::File::Sale::TYPE_ALBUM;
            my $format = RPS::File::Sale::FORMAT_DOWNLOAD;

            my $prodID = $line->[FIELD_PROD_ID];
            my $upc    = $line->[FIELD_UPC];
            my $isrc   = $line->[FIELD_ISRC];
            my $artist = $line->[FIELD_ARTIST];
            my $album  = $line->[FIELD_ALBUM];
            my $track  = $line->[FIELD_TRACK];
            my $label  = $line->[FIELD_LABEL];
            my $units  = $line->[FIELD_UNITS];
            my $price  = $line->[FIELD_PRICE];

            if ( $artist && ( $album || $track ) && $units > 0 ) {
                my $sale = RPS::Import::SaleRec->new();

                $sale->productType($prodType);
                $sale->formatType($format);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->clientProductID($prodID);
                $sale->upc($upc);
                $sale->isrc($isrc);
                $sale->artistName($artist);
                $sale->albumName($album);
                $sale->trackName($track);
                $sale->labelName($label);
                $sale->units($units);
                $sale->price($price);
                $sale->lineNum($linenum);
                $sale->countryCode('US');

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

#
## Private methods
#

sub _get_dates {
    my $date_begin = shift;
    return undef unless ( $date_begin =~ /^(\d{4})(\d\d)\d\d$/ );

    my ( $year, $month ) = ( $1, $2 );

    return ( sprintf( "%04d-%02d-01", $year, $month ), sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) ) );
}

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