package RPS::Import::WMG::MobiTV;

use strict;

use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_isrc);

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

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

my %fieldMap = (
    1 => {
        artist => 0,
        track  => 1,
        isrc   => 2,
        usage  => 3,
        units  => 4,
        date   => 5,
    },
);

#public methods

sub _importLines {
    my $self    = shift;
    my %args    = @_;
    my $fileObj = $args{file};
    my $version = $self->_version();

    #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;
    my ( $dateBegin, $dateEnd );

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

        foreach my $line (@$sheet) {
            my $country  = "US";
            my $currency = "USD";
            my $product  = RPS::File::Sale::TYPE_TRACK;
            my $artist   = $line->[ $offsets->{artist} ];
            my $isrc     = $line->[ $offsets->{isrc} ];
            my $track    = $line->[ $offsets->{track} ];
            my $usage    = $line->[ $offsets->{usage} ];
            my $format   = RPS::File::Sale::FORMAT_STREAM;    # was FORMAT_VIDEOSTREAM (FB1324)
            my $units    = $line->[ $offsets->{units} ];
            $isrc =~ s/\-//g;
            ( $dateBegin, $dateEnd ) = _getDates( $line->[ $offsets->{date} ] );

            if ( $units && $country && $track && $track ne "Title (T)" && $track ne "Title" ) {
                unless ( defined $dateBegin && defined $dateEnd ) {
                    print STDERR "bad dates: $line->[$offsets->{date}]\n";
                    $self->errstr("Couldn't get dates");
                    return undef;
                }

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

                $sale->mediaType(2);
                $sale->productType($product);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->isrc($isrc);
                $sale->artistName($artist);
                $sale->trackName($track);
                $sale->formatType($format);
                $sale->units($units);
                $sale->outlet($usage);    # need to save this flag in a field not used by wmg output spec
                $sale->countryCode($country);
                $sale->currencyCode($currency);
                $sale->conversionRate(0) if ( $currency ne "USD" );
                $sale->lineNum($linenum);

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

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

#
# Private methods
#

sub _getDates {
    my ( $year, $month, $day, $monthName, $qtr, $passed_date, $dateBegin, $dateEnd );
    $passed_date = shift;
    $passed_date =~ s/\s.*$//;

    #print STDERR "|>".$passed_date."<|\n";
    if ( $passed_date =~ m/(\d{4})\D(\d+)\D(\d+)/ ) {
        $year  = $1;
        $month = $2;
    } elsif ( $passed_date =~ m/(\d+)\D(\d+)\D(\d{4})/ ) {
        $year  = $3;
        $month = $1;
    } elsif ( $passed_date =~ /(\d{4})\D(\d+)\D(\d+)/ ) {
        $year  = $1;
        $month = $2;
    }

    if ( $year && $month ) {
        return ( $year . "-" . sprintf( "%02d", $month ) . "-01",
            $year . "-" . sprintf( "%02d", $month ) . "-" . Days_in_Month( $year, $month ) );
    }

    return undef;
}

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