package RPS::Import::DownloadPunk;

use strict;


use Spreadsheet::ParseExcel::Utility;

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

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

#private constants
use constant FIELD_DATEBEGIN	=> 2;
use constant FIELD_DATEEND		=> 4;

#standard record as of downloadpunk version 3
use constant FIELD_LABEL		=> 0;
use constant FIELD_AGGREGATOR	=> 1;
use constant FIELD_DATE			=> 2;
use constant FIELD_ISRC			=> 3;
use constant FIELD_UPC			=> 4;
use constant FIELD_UNITS		=> 5;
use constant FIELD_PRICE		=> 6;
use constant FIELD_EXTPRICE		=> 7;
use constant FIELD_RETAIL		=> 8;
use constant FIELD_ARTIST		=> 9;
use constant FIELD_TITLE		=> 10;
use constant FIELD_PRODTYPE		=> 11;

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

#public methods

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

	my $retval = undef;
	my $lines = $args{lines};
	my $fileObj = $args{file};
	my $version = $fileObj->VersionNum;
	#my $version = $args{version};

	if ($lines && $version)
	{
		my $offsetISRC		= FIELD_ISRC;
		my $offsetUPC		= FIELD_UPC;
		my $offsetUnits		= FIELD_UNITS;
		my $offsetPrice		= FIELD_PRICE;
		my $offsetArtist	= FIELD_ARTIST;
		my $offsetTitle		= FIELD_TITLE;
		my $offsetType		= FIELD_PRODTYPE;

		if ($version == 1)
		{
			$offsetPrice--;
			$offsetArtist -= 2;
			$offsetTitle -= 2;
			$offsetType -= 2;
		}
		elsif ($version == 2)
		{
			$offsetISRC--;
			$offsetUPC--;
			$offsetUnits--;
			$offsetPrice--;
			$offsetArtist--;
			$offsetTitle--;
			$offsetType--;
		}
		elsif ($version == 4)
		{
			$offsetArtist++;
			$offsetTitle++;
			$offsetType++;
		}
		elsif ($version == 5)
		{
			$offsetArtist += 2;
			$offsetTitle += 2;
			$offsetType += 2;
		}
		elsif ($version == 6)
		{
			$offsetArtist += 2;
			$offsetTitle += 2;
			$offsetType += 2;
		}

		my $dateBegin;
		my $dateEnd;
		my $linenum = 1;
		foreach my $line(@$lines)
		{
			if ($linenum == 1)
			{
                $dateBegin = _parseDate($line->[FIELD_DATEBEGIN]);
                $dateEnd   = _parseDate($line->[FIELD_DATEEND]);
                unless ($dateBegin && $dateEnd)
                {
                    $self->errstr("couldn't get dates");
                    return undef;
                }
			}
			elsif ($linenum > 4 || ($linenum > 3 && $version == 6))
			{
				my $prodtype;
				if ($line->[$offsetType] eq 'A')
				{
					$prodtype = RPS::File::Sale::TYPE_ALBUM;
				}
				elsif ($line->[$offsetType] eq 'S')
				{
					$prodtype = RPS::File::Sale::TYPE_TRACK;
				}
				my $upc = Common::Util::normalize_upc($line->[$offsetUPC]);
				my $isrc = Common::Util::normalize_isrc($line->[$offsetISRC]);
				my $artist = $line->[$offsetArtist];
				my $album;
				my $track;
				my $title = $line->[$offsetTitle];
				if ($prodtype eq RPS::File::Sale::TYPE_ALBUM)
				{
					$album = $title;
				}
				elsif ($prodtype eq RPS::File::Sale::TYPE_TRACK)
				{
					$track = $title;
				}
				my $label = $line->[FIELD_LABEL];
				my $format = RPS::File::Sale::FORMAT_DOWNLOAD;
				my $units;
				if ($version == 1)
				{
					$units = 1;
				}
				else
				{
					($units) = $line->[$offsetUnits] =~ m/^(-?\d+)$/;
				}
				my ($price) = $line->[$offsetPrice] =~ m/^-?(\d*\.?\d*)$/; # we don't want the minus sign

				if ($prodtype && $title && $units && $price)
				{
					my $sale = RPS::Import::SaleRec->new();

					$sale->productType($prodtype);
					$sale->formatType($format);
					$sale->dateBegin($dateBegin);
					$sale->dateEnd($dateEnd);
					$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');

					$self->_insertSale(sale => $sale);
				}
			}
			$linenum++;
		}
		$retval = $linenum;
	}

	return $retval;
}

sub _parseDate
{
    my $d = shift;
    # we have seen some files dated after 1/1/06 where the date is NOT identified
    # as a date type field but it's still stored as days-since-1-1-1900
    # 38716 is the number of days from 1-1-1900 to 1-1-2006
    if ($d =~ m/(\d{4})\D(\d+)\D(\d+)/) {
       return $1."-".sprintf("%02d",$2)."-".sprintf("%02d",$3);
    }
    elsif ($d =~ /^\d+$/ and $d > 38716)
    {
        $d = Spreadsheet::ParseExcel::Utility::ExcelFmt("yyyy-mm-dd", $d);
        return Common::Util::normalize_date($d);
    }
}
	
###
1;# Play nicely.
###
