package RPS::Import::WMG::Verizon;
use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/common/lib';
use Common::Util;

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

use lib '/app/tools/sale_import/lib';
use RPS::File::Sale;
use base 'RPS::Import::Importer';
use RPS::Import::WMG::Util;

my %field_map = (
    6 => {
        track     => 1,
        artist    => 2,
        isrc      => 3,
        retail    => 4,
        units     => 5,
        pricetype => 6,
        price     => 7,
    },
    7 => {
        track     => 2,
        artist    => 3,
        isrc      => 4,
        retail    => 5,
        units     => 6,
        pricetype => 7,
        price     => 8,
    },
    8 => {
        track     => 1,
        artist    => 2,
        isrc      => 3,
        units     => 4,
        retail    => 5,
        pricetype => 6,
        price     => 7,
    },
);

#public methods

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

    my $lines    = $args{lines};
    my $version  = $self->_version();
    my $fileObj  = $args{file};
    my $fileName = $fileObj->OrigFileName();

    #my $fileName = $args{OrigFileName}; # for cmd-line testing

    return 0 unless $lines;
    my $offsets = $field_map{$version} || return undef;
    my ( $dateBegin, $dateEnd ) = $self->_getDates($fileName);

    unless ( $dateBegin && $dateEnd ) {
        $self->errstr("couldn't get dates");
        print STDERR "filename: $fileName\n";
        return 0;
    }

    my $prodType          = RPS::File::Sale::TYPE_TRACK;
    my $formatTypeDefault = 'realtone';

    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";
    }

    $fileObj->Atomic(1);
    $fileObj->Save();

    my $linenum = 1;
    my %errors  = ();
    foreach my $line (@$lines) {
        my $formatType = $formatTypeDefault;
        my $track      = $line->[ $offsets->{track} ];
        my $artist     = $line->[ $offsets->{artist} ];
        my $isrc       = Common::Util::normalize_isrc( $line->[ $offsets->{isrc} ] );
        my ($units)    = $line->[ $offsets->{units} ] =~ m/^(-?\d+)$/;
        my $retail     = $line->[ $offsets->{retail} ];
        my ($price)    = $line->[ $offsets->{price} ] =~ m/^(-?\d*\.?\d+)$/;

        if ( $track && $artist && $units && $retail && $price ) {

            # why use the format column when you can decorate the track name???
            if ( $track =~ m/(voice\s*ringer|ringtone)\)?$/i )    # voice ringers and ringtones mixed in
            {
                $formatType = $1;
            }

            my $formatID = RPS::Import::WMG::Util::getWirelessFormatTypeID($formatType);
            unless ( $formatID or $errors{$formatType} ) {
                $self->errstr('unknown format');
                print STDERR "unknown format: $formatType ($linenum)\n";
                $errors{$formatType} = 1;
                $linenum++;
                next;
            }

            my $priceType = $line->[ $offsets->{pricetype} ];
            $price /= $units;
            my $wRate = $price / $retail;

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

            $sale->productType($prodType);
            $sale->formatType($formatID);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->isrc($isrc);
            $sale->units($units);
            $sale->price($price);
            $sale->retail($retail);
            $sale->wholesaleRate($wRate);
            $sale->lineNum($linenum);
            $sale->priceType($priceType);

            #$args{dumper}($sale);
            $self->_insertSale( sale => $sale );
        }    #else { print STDERR "skip t: $track a: $artist u: $units r: $retail p: $price\n" }
        $linenum++;
    }

    return $linenum;
}

#
# Private methods
#

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

    my ( $month, $year );
    if ( $fileName =~ m/\D(\d\d)\D(\d\d)\D/ ) {
        ( $month, $year ) = ( $1, 2000 + $2 );
    } elsif ( $fileName =~ m/\D(\d\d\d\d)\D(\d\d)/ ) {
        ( $year, $month ) = ( $1, $2 );
    }

    return undef unless ( $month && $year );
    return ( sprintf( "%04d-%02d-01", $year, $month ), sprintf( "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month ) ) );
}

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