package RPS::Import::Mixxer;

use strict;

use Date::Calc qw(Decode_Month Days_in_Month);

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

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 = (
    1 => {
        label  => 1,
        track  => 2,
        artist => 3,
        retail => 4,
        isrc   => 5,
        units  => 6,
        price  => 7,
    },
);

#public methods

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

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

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

    my $offsets = $field_map{$version};

    return 0 unless ($lines);

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

    my $priceTypeDefault = '';
    my $priceTypeVR      = '';

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

        my ( $billTo, $sellTo );
        ( $billTo, $sellTo, $priceTypeDefault ) = RPS::Import::WMG::Util::getWirelessInfo( $self->{fileServiceID}, 'mastertone' );
        unless ( $billTo && $sellTo ) {
            $self->warning();
            print STDERR "unknown billto/sellto mapping: $self->{fileServiceID}\n";
        }

        unless ($priceTypeDefault) {
            $self->warning();
            print STDERR "unknown format mapping: mastertone\n";
        }

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

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

    my $linenum = 1;

    foreach my $line (@$lines) {
        my $prodtype = RPS::File::Sale::TYPE_TRACK;

        my $label   = $line->[ $offsets->{label} ];
        my $artist  = $line->[ $offsets->{artist} ];
        my $track   = $line->[ $offsets->{track} ];
        my $isrc    = normalize_isrc( $line->[ $offsets->{isrc} ] );
        my ($units) = $line->[ $offsets->{units} ] =~ /^(\d+)$/;
        my $price   = $line->[ $offsets->{price} ];
        my $retail  = $line->[ $offsets->{retail} ];
        $price = ( $units != 0 ) ? ( $price / $units ) : 0;
        my $ppdRate = ( defined $retail && $retail != 0 ) ? ( $price / $retail ) : '';

        my $format = RPS::File::Sale::FORMAT_RINGTONE;
        my $priceType;
        if ( $track =~ m/voice\s*ringer/i )    # voice ringers mixed in
        {
            $priceType = $priceTypeVR;
        } else {
            $priceType = $priceTypeDefault;
        }

        if ( $label && $units && $price && $artist && $track ) {
            my $sale = RPS::Import::SaleRec->new();

            $sale->productType($prodtype);
            $sale->formatType($format);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->isrc($isrc);
            $sale->labelName($label);
            $sale->artistName($artist);
            $sale->trackName($track);
            $sale->units($units);
            $sale->price($price);
            $sale->retail($retail);
            $sale->wholesaleRate($ppdRate);
            $sale->priceType($priceType);
            $sale->lineNum($linenum);

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

#
# Private methods
#

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

    $fileName =~ /^Mixxer[\s\-_]*(\w+)\D+(\d{4})/;
    my $year  = $2;
    my $month = Decode_Month($1);
    return unless ($month);

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

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