package RPS::Import::FNAC3;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/common/lib';

#use Common::Util;
use Common::Util qw(normalize_date normalize_isrc);
use Common::Consts qw(%COUNTRY_CODE);

use lib '/app/tools/data_classes/lib';
use Client::Service;

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

my %field_map = (
    4 => {
        date_begin  => 0,
        date_end    => 1,
        upc         => 2,
        isrc        => 3,
        units       => 5,
        revenue_alt => 6,
        revenue     => 7,
        artist      => 10,
        title       => 11,
    },
);

#public methods

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

    my $lines       = $args{lines};
    my $version     = $args{file}->VersionNum();
    my $fileName    = $args{file}->OrigFileName();
    my $sheets      = $args{sheets};
    my @sheet_names = @{ $args{sheet_names} };
    my $sheet_num   = 0;

    return undef unless ($lines);

    my $linenum = 1;

    my $offsets = $field_map{$version};
    my %errors  = ();

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

        unless ( ( $sheet_names[$sheet_num] =~ /album/i )
            || ( $sheet_names[$sheet_num] =~ /track/i ) ) {
            $sheet_num++;
            next;
        }

        my $productType;
        my $formatType = RPS::File::Sale::FORMAT_DOWNLOAD;

        if ( $sheet_names[$sheet_num] =~ /album/i ) {
            $productType = RPS::File::Sale::TYPE_ALBUM;
        } elsif ( $sheet_names[$sheet_num] =~ /track/i ) {
            $productType = RPS::File::Sale::TYPE_TRACK;
        }

        foreach my $line (@$lines) {
            my $f_date_begin = $line->[ $offsets->{date_begin} ];
            my $f_date_end   = $line->[ $offsets->{date_end} ];

            my $units   = $line->[ $offsets->{units} ];
            my $revenue = $line->[ $offsets->{revenue} ];
            my $price;

            if ( !$revenue ) {
                $price = $line->[ $offsets->{revenue_alt} ];
            }

            my $artist   = $line->[ $offsets->{artist} ];
            my $currency = "GBP";
            my $country  = "GB";

            my $track;
            my $album;
            my $upc;
            my $isrc;

            my ( $dateBegin, $dateEnd ) = $self->_getDates(
                date_begin => $f_date_begin,
                date_end   => $f_date_end,
                type       => "iso"
            );

            if ( $productType eq RPS::File::Sale::TYPE_TRACK ) {
                $track = $line->[ $offsets->{title} ];
                $isrc  = $line->[ $offsets->{isrc} ];
            } elsif ( $productType eq RPS::File::Sale::TYPE_ALBUM ) {
                $album = $line->[ $offsets->{title} ];
                $upc   = $line->[ $offsets->{upc} ];
            }

            if (   ( $track || $album )
                && $units =~ /\d+/
                && $artist
                && ( $revenue || $price ) ) {

                if ( !( $dateBegin && $dateEnd ) ) {

                    ( $dateBegin, $dateEnd ) = $self->_getDates(
                        date_begin => $f_date_begin,
                        date_end   => $f_date_end,
                        type       => "msft"
                    );
                }

                unless ( $dateBegin && $dateEnd ) {
                    $self->errstr( "couldn't get dates from $f_date_begin, " . "$f_date_end line: $linenum" );
                    print STDERR "couldn't get dates from $f_date_begin, " . "$f_date_end line: $linenum";

                    return undef;
                }

                $price = $revenue / $units if ($revenue);

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

                $sale->productType($productType);
                $sale->formatType($formatType);
                $sale->mediaType(1);
                $sale->albumName($album);
                $sale->trackName($track);
                $sale->artistName($artist);
                $sale->units($units);
                $sale->price($price);
                $sale->upc($upc);
                $sale->isrc($isrc);
                $sale->currencyCode($currency);
                $sale->countryCode($country);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->lineNum($linenum);

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

            #else{print STDERR "line: $linenum track: $track album: $album ".
            #"units: $units artist: $artist revenue: $revenue\n";}

            $linenum++;
        }

        $sheet_num++;
    }

    return $linenum;
}

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