package RPS::Import::Google;

use strict;

use Date::Calc qw(Decode_Month Days_in_Month);

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

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

use constant FIELD_TITLE => 0;
use constant FIELD_UNITS => 1;
use constant FIELD_PRICE => 2;

#public methods

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

    my $sheetName = $args{sheet_names}->[0];
    my $lines     = $args{lines};
    return 0 unless ($lines);

    my ( $dateBegin, $dateEnd ) = $self->_getDates($sheetName);
    unless ( $dateBegin && $dateEnd ) {
        $self->errstr("couldn't get dates");
        print STDERR "sheet name: $sheetName\n";
        return undef;
    }

    my $priceType = $self->{clientID} == RPS::Import::Importer::WMG ? 'SUBVSON' : undef;
    my $prodType  = RPS::File::Sale::TYPE_TRACK;
    my $linenum   = 1;

    foreach my $line (@$lines) {
        my ($units) = $line->[FIELD_UNITS] =~ m/^(\d+)$/;
        my ($price) = $line->[FIELD_PRICE] =~ m/^(\d+\.?\d*)$/;
        my $title   = $line->[FIELD_TITLE];

        my @multiCol = split /\s-\s/, $title;
        my ( $track, $artist, $isrc, $format );
        my $mediaType;

        if ( defined $multiCol[1] ) {
            $artist = pop @multiCol;
            $track = join( ' - ', @multiCol );

            $format = RPS::File::Sale::FORMAT_DOWNLOAD;
            if ( $track =~ m/\bvideo\b/i ) {
                $mediaType = RPS::DB::Item::MediaType::kMediaTypeVideo;    # download/video (FB1324)
            } else {
                $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;
            }

        } else {
            if ( $title =~ m/^(\w+\d+)_\w+\.(\w+)$/ )                      # USATV0400364_QF.avi
            {
                $isrc   = $1;
                $format = RPS::File::Sale::FORMAT_DOWNLOAD;
                if ( $2 =~ m/avi/i ) {
                    $mediaType = RPS::DB::Item::MediaType::kMediaTypeVideo;    # download/video (FB1324)
                } else {
                    $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;
                }

            } else {
                if ( lc $title ne 'titles' and lc $title ne 'grand total' and $title ne '' ) {
                    $self->errstr('invalid title');
                    print STDERR "title: $title\n";
                }
                $linenum++;
                next;
            }
        }

        if ( $format && $price && $units && ( $track || $isrc ) ) {
            $price /= $units;

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

            $sale->productType($prodType);
            $sale->formatType($format);
            $sale->mediaType($mediaType);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->isrc($isrc);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->units($units);
            $sale->price($price);
            $sale->priceType($priceType);
            $sale->lineNum($linenum);

            #$args{dumper}($sale);  ## for cmd-line testing
            $self->_insertSale( sale => $sale );
        } else {
            print STDERR "skip: f: $format p: $price u: $units t: $track i: $isrc\n";
        }
        $linenum++;
    }
    return $linenum;
}

#
# Private methods
#

sub _getDates {
    my $self  = shift;
    my $sName = shift;

    return undef unless ( $sName =~ m/\W(\w+)\D(\d\d)$/ );
    my ( $month, $year ) = ( Decode_Month($1), $2 + 2000 );
    return undef unless ( $month > 0 );

    return (
        Common::Util::normalize_date( join( '-', $year, $month, 1 ) ),
        Common::Util::normalize_date( join( '-', $year, $month, Days_in_Month( $year, $month ) ) ),
    );
}

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