package RPS::Import::SlagHuis;

use strict;


use Date::Calc qw(Days_in_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 base 'RPS::Import::Importer';

my %field_map = (
                 1 => {
                       'date'   => 1,
                       'label'  => 3,
                       'artist' => 4,
                       'track'  => 5,
                       'retail' => 6,
                       'rate'   => 7,
                       'fee'    => 9,
                       },
                 2 => {
                       'date'   => 1,
                       'label'  => 3,
                       'artist' => 4,
                       'track'  => 5,
                       'retail' => 6,
                       'price'  => 8,
                       },
);

sub _importLines
{
    my $self = shift;
    my %args = @_;
    my $lines = $args{lines};
    my $fileObj = $args{file};
    my $version = $fileObj->VersionNum;
    #my $version = $args{version}; # for cmd-line testing

    my $offsets = $field_map{$version};

    return undef unless $lines;

    my $linenum = 1;
    foreach my $line(@$lines)
    {
        my $prodtype    = RPS::File::Sale::TYPE_TRACK;
        my $format      = RPS::File::Sale::FORMAT_DOWNLOAD;
        my $units       = 1;
        my ($dateBegin, $dateEnd) = _get_dates($line->[$offsets->{'date'}]);
        next if ($line->[$offsets->{'date'}] eq 'Date' || $line->[$offsets->{'date'}] eq '');
        unless ($dateBegin && $dateEnd)
        {
            $self->errstr("couldn't get dates from ".$line->[$offsets->{'date'}]);
            return undef;
        }

        my $label       = $line->[$offsets->{'label'}];
        my $artist      = $line->[$offsets->{'artist'}];
        my $track       = $line->[$offsets->{'track'}];
        my ($retail)    = $line->[$offsets->{'retail'}] =~ /\$?(\d+\.\d+)$/;
        my $price;
        my $is_album = 0;
        if ($version == 1) {
            my $royRate     = $line->[$offsets->{'rate'}];
            my ($fee)       = $line->[$offsets->{'fee'}] =~ /\$?(\d+\.\d+)$/;
            $royRate =~ s/[^\d\.]*//;
            $royRate /= 100 if ($royRate > 1);
            $price = $retail - ($retail * $royRate) - $fee;
        } elsif ($version == 2) {
            ($price) = $line->[$offsets->{'price'}] =~ /\$?(\d+\.\d+)$/;
        }

        if ($retail > 1.5) {
            ## no explicit way to differentiate tracks & albums
            $is_album = 1;
            $prodtype = RPS::File::Sale::TYPE_ALBUM;
        }

        if ($artist && $track && $price)
        {

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

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->albumName($track) if ($is_album == 1);
            $sale->trackName($track) if ($is_album == 0);
            $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 = shift;
    return unless ($date =~ m/\d+[-\/]\d+/);

    my ($y,$m,$d);
    if ($date =~ m/^(\d{4})-(\d{2})-(\d{2})$/) {
        $y = $1;
        $m = $2;
        $d = $3;
    } else {
        $date = Common::Util::normalize_date($date);
        ($y,$m,$d) = split /-/, $date;
    }

    return (
            sprintf("%d-%02d-01", $y,$m),
            sprintf("%d-%02d-%02d", $y, $m, Days_in_Month($y, $m)),
           );
}

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