package RPS::Import::WhatPeoplePlay;

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::File::Sale;
use RPS::Import::Importer;
use base 'RPS::Import::Importer';

my %fieldMap = (
    1 => {
        date_start => 3,
        date_end   => 4,
        country    => 8,
        format     => 9,
        product    => 11,
        units      => 13,
        upc        => 14,
        isrc       => 15,
        catalog    => 16,
        artist     => 17,
        track      => 18,
        label      => 19,
        price      => 28,
        currency   => 29,
    },
);

my %format_map = ( download => RPS::File::Sale::FORMAT_DOWNLOAD, );

my %product_map = (
    track  => RPS::File::Sale::TYPE_TRACK,
    bundle => RPS::File::Sale::TYPE_ALBUM,
);

#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   = uc( $line->[ $offsets->{country} ] );
                my $currency  = uc( $line->[ $offsets->{currency} ] );
                my $track     = $line->[ $offsets->{track} ];
                my $artist    = $line->[ $offsets->{artist} ];
                my $format    = $format_map{ lc( $line->[ $offsets->{format} ] ) };
                my $product   = $product_map{ lc( $line->[ $offsets->{product} ] ) };
                my $units     = $line->[ $offsets->{units} ];
                my $price     = $line->[ $offsets->{price} ];
                my $label     = $line->[ $offsets->{label} ];
                my $dateBegin = _getDates( $line->[ $offsets->{date_start} ] );
                my $dateEnd   = _getDates( $line->[ $offsets->{date_end} ] );
                my $isrc      = $line->[ $offsets->{isrc} ];
                my $upc       = $line->[ $offsets->{upc} ];
                my $catalog   = $line->[ $offsets->{catalog} ];
                $price =~ s/\,/\./;
                $price /= $units if ( $units != 0 );

                if ( $units && $artist && $country && $price && $track && $track ne "Title" ) {

                    unless ( defined $dateBegin && defined $dateEnd ) {
                        print STDERR "Couldn't get dates from: "
                          . $line->[ $offsets->{date_start} ] . " "
                          . $line->[ $offsets->{date_end} ] . "\n";
                        $self->errstr(
                            "couldn't get dates from: " . $line->[ $offsets->{date_start} ] . " " . $line->[ $offsets->{date_end} ] );
                        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->serviceProductID($catalog);
                    if ( $product eq RPS::File::Sale::TYPE_TRACK ) {
                        $sale->trackName($track);
                    }
                    if ( $product eq RPS::File::Sale::TYPE_ALBUM ) {
                        $sale->albumName($track);
                    }
                    $sale->artistName($artist);
                    $sale->formatType($format);
                    $sale->units($units);
                    $sale->price($price);
                    $sale->upc($upc);
                    $sale->isrc($isrc);
                    $sale->countryCode($country);
                    $sale->currencyCode($currency);
                    $sale->lineNum($linenum);

                    $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 ( $smonth, $syear, $sday, $emonth, $eyear, $eday );
    my $passed_date = shift;
    if ( $passed_date =~ m/(\d+)\.(\d+)\.(\d{4})/ ) {
        $syear  = $3;
        $smonth = $2;
        $sday   = $1;
    }
    if ( $syear && $smonth && $sday ) {
        return ( $syear . "-" . $smonth . "-" . $sday );
    }
    return undef;
}

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