package RPS::Import::VerizonVOD;

# Video On Demand

use strict;

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 %field_map = (
    4 => {
        date_row => 5,
        date_col => 2,
        artist   => 1,
        track    => 2,
        grid     => 3,
        retail   => 4,
        units    => 5,
        price    => 6,
    },
    5 => {
        date_row => 5,
        date_col => 2,
        artist   => 1,
        track    => 2,
        grid     => 3,
        units    => 4,
        retail   => 5,
        price    => 6,
    },
);

#public methods

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

    my $version = $self->_version();
    my $offsets = $field_map{$version};

    my $lines = scalar $args{sheets}[-1];    # 1st (and only) sheet if 1, last sheet if multiple
    return 0 unless ($lines);

    my ( $dateBegin, $dateEnd ) = $self->_getDates( $lines->[ $offsets->{date_row} ]->[ $offsets->{date_col} ] );
    unless ( $dateBegin && $dateEnd ) {
        $self->errstr("couldn't get dates");
        print STDERR "date cell: $lines->[$offsets->{date_row}]->[$offsets->{date_col}]\n";
        return undef;
    }

    my $prodtype  = RPS::File::Sale::TYPE_TRACK;
    my $format    = RPS::File::Sale::FORMAT_DOWNLOAD;            # was FORMAT_VIDEO (FB1324)
    my $mediaType = RPS::DB::Item::MediaType::kMediaTypeVideo;

    my $priceType = '';

    if ( $self->{clientID} == RPS::Import::Importer::WMG )       # warner
    {
        # check for warner specific stuff
        require RPS::Import::WMG::Util;

        my $formatName = 'video';
        my @billTo     = RPS::Import::WMG::Util::getWirelessBillto( $self->{fileServiceID} );
        if ( $billTo[0] ) {
            unless ( RPS::Import::WMG::Util::getWirelessSellto( $self->{fileServiceID}, $billTo[0] ) ) {
                $self->warning();
                print STDERR "unknown sellto mapping: $billTo[0]\n";
            }
        } else {
            $self->warning();
            print STDERR "unknown billto mapping: $self->{fileServiceID}\n";
        }

        unless ( $priceType = RPS::Import::WMG::Util::getWirelessPriceType($formatName) ) {
            $self->warning();
            print STDERR "unknown format mapping: $formatName\n";
        }

        my $fileObj = $args{file};
        $fileObj->Atomic(1);
        $fileObj->Save();
    }

    my $linenum = 1;
    foreach my $line (@$lines) {
        my $artist  = $line->[ $offsets->{artist} ];
        my $track   = $line->[ $offsets->{track} ];
        my $grid    = $line->[ $offsets->{grid} ];
        my $retail  = $line->[ $offsets->{retail} ];
        my ($units) = $line->[ $offsets->{units} ] =~ m/^(\d+)$/;
        my ($price) = $line->[ $offsets->{price} ] =~ m/^(-?\d+\.?\d*)$/;

        if ( $units && defined $price && $artist && $track ) {
            my $rate = $retail ? $price / $retail : 0;
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->mediaType($mediaType);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->upc($grid);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->units($units);
            $sale->price($price);
            $sale->lineNum($linenum);
            $sale->wholesaleRate($rate);
            $sale->retail($retail);
            $sale->priceType($priceType);

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

#
# Private methods
#

sub _getDates {
    my $self  = shift;
    my $dates = shift;
    my ( $dateBegin, $dateEnd ) = split /-/, $dates;
    $dateBegin = _fixDate($dateBegin);
    $dateEnd   = _fixDate($dateEnd);

    return ( $dateBegin, $dateEnd );
}

sub _fixDate {
    my $date = shift;    # 02/01/06 (2 digit year is the last pair)
    my (@parts) = split /\D/, $date;

    return sprintf( "%d-%02d-%02d", 2000 + $parts[2], $parts[0], $parts[1] );
}

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