package RPS::Import::7Digital;

use strict;

use Date::Calc qw(Days_in_Month Add_Delta_Days Decode_Month);

use lib '/app/tools/common/lib';
use Common::Consts qw(%COUNTRY_TO_CODE);

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

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

my %fieldMap = (
    1 => {
        date     => 0,
        artist   => 1,
        track    => 4,
        album    => 2,
        isrc     => 5,
        upc      => 6,
        label    => 7,
        price    => 9,
        currency => 10,
    },
    2 => {
        date     => 0,
        artist   => 1,
        track    => 4,
        album    => 2,
        isrc     => 5,
        upc      => 7,
        label    => 8,
        country  => 13,
        currency => 14,
        price    => 17,
    },
    3 => {
        date         => 0,
        artist       => 1,
        album        => 2,
        track        => 4,
        isrc         => 5,
        upc          => 6,
        label        => 7,
        shop_country => 9,
        country      => 16,
        currency     => 20,
        price        => 27,
    },
    4 => {
        date         => 0,
        artist       => 1,
        album        => 2,
        track        => 4,
        isrc         => 5,
        upc          => 6,
        label        => 7,
        shop_country => 9,
        country      => 12,
        currency     => 14,
        price        => 17,
    },
    5 => {
        date         => 0,
        artist       => 1,
        album        => 2,
        track        => 4,
        isrc         => 5,
        upc          => 6,
        label        => 7,
        shop_country => 9,
        country      => 12,
        currency     => 13,
        price        => 16,
    },
    6 => {
        date         => 0,
        artist       => 1,
        album        => 2,
        track        => 4,
        isrc         => 5,
        upc          => 6,
        label        => 7,
        shop_country => 9,
        country      => 16,
        currency     => 22,
        price        => 29,
    },
    8 => {
        date     => 0,
        artist   => 1,
        album    => 2,
        track    => 4,
        isrc     => 5,
        upc      => 6,
        label    => 7,
        country  => 9,
        currency => 11,
        price    => 14,
    },
);

#public methods

sub _importLines {
    my $self        = shift;
    my %args        = @_;
    my $fileObj     = $args{file};
    my $version     = $args{file}->VersionNum();
    my $fileName    = $args{file}->OrigFileName();
    my $serviceID   = $args{file}->ServiceID;
    my $sheet_count = 0;

    return undef unless ( defined $args{sheets} && defined $version );

    my $offsets      = $fieldMap{$version};
    my $sheet_names  = $args{sheet_names};
    my $header_found = 0;
    my $linenum      = 1;

    foreach my $sheet ( @{ $args{sheets} } ) {
        $linenum = 1;
        if ( $sheet_names->[$sheet_count] !~ /summary/i ) {

            foreach my $line (@$sheet) {

                my $country  = $line->[ $offsets->{country} ];
                my $currency = $line->[ $offsets->{currency} ];
                my $track    = $line->[ $offsets->{track} ];
                my $album    = $line->[ $offsets->{album} ];
                my $artist   = $line->[ $offsets->{artist} ];
                my $format   = RPS::File::Sale::FORMAT_DOWNLOAD;
                my $product  = RPS::File::Sale::TYPE_TRACK;
                my $units    = 1;
                my $price    = $line->[ $offsets->{price} ];
                my $label    = $line->[ $offsets->{label} ];
                my $isrc     = $line->[ $offsets->{isrc} ];
                my $upc      = $line->[ $offsets->{upc} ];
                $product = RPS::File::Sale::TYPE_ALBUM if ( $track eq "" );

                if ( $version == 1 ) {
                    $country = "GB";
                }

                if ( $version == 2 || $version == 3 || $version == 4 || $version == 5 || $version == 8 ) {
                    $currency = "GBP";
                } elsif ( $version == 6 ) {
                    $currency = "EUR";
                }

                if ( $version == 4 || $version == 5 || $version == 6 ) {
                    unless ($country) {
                        $country = $line->[ $offsets->{shop_country} ];
                    }
                }
                my $conversionRate = 1;

                if (   $units
                    && $artist
                    && $price
                    && ( $track && $track ne "trackname" || $album && $album ne "productname" )
                    && $album ne "Product Name" ) {
                    my ( $dateBegin, $dateEnd ) = _getDates( $line->[ $offsets->{date} ] );

                    unless ( defined $dateBegin && defined $dateEnd ) {
                        print STDERR "Couldn't get dates from: " . $fileName . "\n";
                        $self->errstr( "couldn't get dates from: " . $fileName );
                        return undef;
                    }

                    unless ($country) {
                        $self->errstr("line $linenum: no country entered");
                        print STDERR "line $linenum: no country entered\n";
                        return undef;
                    }

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

                    $sale->mediaType(2) if ( $format =~ /video/i );
                    $sale->productType($product);
                    $sale->dateBegin($dateBegin);
                    $sale->dateEnd($dateEnd);
                    $sale->labelName($label);
                    $sale->trackName($track) if ( $track ne "" );
                    $sale->albumName($album);
                    $sale->artistName($artist);
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->isrc($isrc);
                    $sale->upc($upc);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

                    # !!! conversion_rate?
                    #
                    $sale->conversionRate($conversionRate);

                    $self->_insertSale( sale => $sale );
                }

#else { print STDERR "skip sh: ".$sheet_names->[$sheet_count] . ": l: " . $linenum . " - u:".$units." p:".$price." a:".$artist." t:".$track." f:".$format."\n"; }
                $linenum++;
            }
        }
        $sheet_count++;
    }
    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my ( $year, $month, $passed_date, $dateBegin, $dateEnd );
    $passed_date = shift;

    #print STDERR "passed date: $passed_date\n";

    if ( $passed_date =~ m/(\d{4})(\d\d)(\d\d)/ ) {
        $year  = $1;
        $month = $2;
    }
    if ( $year && $month ) {
        return ( $year . "-" . $month . "-01", $year . "-" . $month . "-" . Days_in_Month( $year, $month ) );
    }
    return undef;
}

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