package RPS::Import::LimeWire;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use lib '/app/tools/common/lib';

#use Common::Util;
use Common::Util qw(normalize_date normalize_isrc);
use Common::Consts qw(%COUNTRY_CODE);

use lib '/app/tools/data_classes/lib';
use Client::Service;

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

my %field_map = (
    1 => {
        sub_units_col         => 2,
        sub_units_row         => 2,
        sub_royalty_tag_col   => 4,
        sub_royalty_total_col => 5,
        client_product_id     => 1,
        album                 => 2,
        upc                   => 3,
        track                 => 4,
        isrc                  => 5,
        type                  => 6,
        date                  => 8,
        price                 => 9,
        label                 => 10,
    },
    2 => {
        sub_units_col         => 2,
        sub_units_row         => 2,
        sub_royalty_tag_col   => 4,
        sub_royalty_total_col => 5,
        client_product_id     => 1,
        album                 => 2,
        upc                   => 3,
        track                 => 4,
        isrc                  => 5,
        type                  => 6,
        date                  => 8,
        price                 => 9,
    },
);

my %type_format_map = (
    "track"        => [ RPS::File::Sale::TYPE_TRACK, RPS::File::Sale::FORMAT_DOWNLOAD ],
    "subscription" => [ RPS::File::Sale::TYPE_TRACK, RPS::File::Sale::FORMAT_STREAM ],
    "album"        => [ RPS::File::Sale::TYPE_TRACK, RPS::File::Sale::FORMAT_DOWNLOAD ],
);

#public methods

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

    my $lines    = $args{lines};
    my $version  = $args{file}->VersionNum();
    my $fileName = $args{file}->OrigFileName();
    my @sheets   = @{ $args{sheets} };

    return undef unless ($lines);

    my $linenum = 1;

    my $offsets = $field_map{$version};
    my %errors  = ();

    my $sheet = $sheets[0];

    my $sub_price;
    my $sub_revenue;

    my $sub_units =
      $sheet->[ $offsets->{sub_units_row} ]->[ $offsets->{sub_units_col} ];

    my $currency = "USD";
    my $country  = "US";

    foreach my $line (@$lines) {
        if ( $line->[ $offsets->{sub_royalty_tag_col} ] =~ /subscription royalty/i ) {
            if ( $sub_units !~ /^\d+$/ ) {

                $self->errstr( "could not get subscription units from row "
                      . ( $offsets->{sub_units_row} + 1 )
                      . ", column "
                      . ( $offsets->{sub_units_col} + 1 ) );
                print STDERR "could not get subscription units from row "
                  . ( $offsets->{sub_units_row} + 1 )
                  . ", column "
                  . ( $offsets->{sub_units_col} + 1 );

                return undef;
            }
            $sub_revenue = $line->[ $offsets->{sub_royalty_total_col} ];
            $sub_revenue =~ s/\$//g;
            $sub_price = $sub_revenue / $sub_units;
        }

        my $client_product_id = $line->[ $offsets->{client_product_id} ];
        my $album             = $line->[ $offsets->{album} ];
        my $upc               = $line->[ $offsets->{upc} ];
        my $track             = $line->[ $offsets->{track} ];
        my $isrc              = $line->[ $offsets->{isrc} ];
        my $type              = $line->[ $offsets->{type} ];
        my $f_date            = $line->[ $offsets->{date} ];
        my $price             = $line->[ $offsets->{price} ];
        my $label;
        $label = $line->[ $offsets->{label} ] if ( defined $offsets->{label} );

        my $units = 1;

        my $type_format_array_ref = $type_format_map{$type};

        my ( $product_type, $format );

        if ($type_format_array_ref) {
            ( $product_type, $format ) = @$type_format_array_ref;
        }

        if ( $type eq "subscription" ) {

            $price = $sub_price;
        }

        $f_date =~ s/\.\d+//;

        my ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $f_date, type => "msft" );

        if (   $client_product_id
            && ( $album || $track )
            && ( $upc   || $isrc )
            && $type
            && $currency
            && $country
            && $f_date
            && $price =~ /\d+/ ) {

            unless ( $dateBegin && $dateEnd ) {
                $self->errstr("couldn't get dates from $f_date, line: $linenum");
                print STDERR "couldn't get dates from $f_date, line: $linenum";
                return undef;
            }

            my $sale = RPS::Import::SaleRec->new();
            $sale->productType($product_type);
            $sale->formatType($format);
            $sale->mediaType(1);
            $sale->labelName($label);
            $sale->clientProductID($client_product_id);
            $sale->albumName($album);
            $sale->trackName($track);
            $sale->units($units);
            $sale->price($price);
            $sale->upc($upc);
            $sale->isrc($isrc);
            $sale->currencyCode($currency);
            $sale->countryCode($country);
            $sale->labelName($label);
            $sale->dateBegin($dateBegin);
            $sale->dateEnd($dateEnd);
            $sale->lineNum($linenum);

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

        $linenum++;
    }

    return $linenum;
}

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