package RPS::Import::RealCombined;

use strict;


use Date::Calc qw(Days_in_Month Decode_Month);

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

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';

use constant SHEET_SUMMARY_DATA  => -1;
use constant FIELD_SUMMARY_LABEL => 0;

use constant FIELD_LABEL    => 0;
use constant FIELD_ARTIST   => 1;
use constant FIELD_ALBUM    => 2;
use constant FIELD_TRACK    => 3;
use constant FIELD_UNITS    => 4;
#use constant FIELD_PRICE    => 5;

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

    my $version = $args{file}->VersionNum;
    my $fileName = $args{file}->OrigFileName;
    #my $version = $args{version};
    #my $fileName = $args{OrigFileName};

    return undef unless $lines;

    my ($dateBegin, $dateEnd, $format) = _get_dates_and_format($version == 4 ? $lines->[0][0] : $fileName);

    unless ($dateBegin && $dateEnd && $format)
    {
        $self->errstr("couldn't get dates and format");
        print STDERR "date cell: '$lines->[0][0]'\n";
        return undef;
    }
    #print STDERR "using dates: $dateBegin - $dateEnd, format: $format\n";

    my $unitPrice = _get_unit_price($args{sheets}->[SHEET_SUMMARY_DATA]);
    unless ($unitPrice)
    {
        $self->errstr("unable to get unit price");
        return undef;
    }
    #print STDERR "using unit price: $unitPrice\n";

    my $linenum = 1;
    foreach my $line(@$lines)
    {
        if ($line->[FIELD_TRACK] eq 'ALBUM TOTAL')
        {
            $linenum++;
            next;
        }

        my $prodtype    = RPS::File::Sale::TYPE_TRACK;

        my $label       = $line->[FIELD_LABEL];
        my $artist      = $line->[FIELD_ARTIST];
        my $album       = $line->[FIELD_ALBUM];
        my $track       = $line->[FIELD_TRACK];
        my ($units)     = $line->[FIELD_UNITS] =~ m/^(\d+)$/;
        #my $price       = $line->[FIELD_PRICE];

        if ($artist && $album && $track && $units)
        {
            #$price = $format eq RPS::File::Sale::FORMAT_STREAM ? $streamPrice : $price / $units;

            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->albumName($album);
            $sale->trackName($track);
            $sale->labelName($label);
            $sale->units($units);
            $sale->price($unitPrice);
            $sale->lineNum($linenum);
            $sale->countryCode('US');

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


#
# Private methods
#

sub _get_dates_and_format
{
    my $line = shift;

    my ($qtr, $year, $format);
    if ($line =~ m/Q(1|2|3|4)\s*(\d{4})\s+(\w+)\s/) # from the data sheet
    {
        ($qtr, $year, $format) = ($1, $2, $3);
    }
    elsif ($line =~ m/_(\w+?)_q(\d)_(\d{4})/i) # from the file name
    {
        ($format, $qtr, $year) = ($1, $2, $3);
    }
    my $month = $qtr * 3;

    return undef unless ($year && $qtr);

    $format =
        $format =~ /stream/i ? RPS::File::Sale::FORMAT_STREAM :
        $format =~ /burn/i ? RPS::File::Sale::FORMAT_DOWNLOAD :
        '';

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

sub _get_unit_price
{
    my $sheet = shift;

    foreach my $line(@$sheet)
    {
        next unless ($line->[FIELD_SUMMARY_LABEL] =~ m/Payable Performance/i);
        my $price = $line->[FIELD_SUMMARY_LABEL + 1] || $line->[FIELD_SUMMARY_LABEL + 2];
        $price =~ s/\$?(\d+\.\d+)$/$1/;

        return $price if ($price > 0);
    }
}

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