#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::Bleep::v2;

use strict;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Client;

use lib '/app/tools/common/lib';
use Common::Consts;
use Common::Assert;
use Common::Log;

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

use lib '/app/tools/rps/lib';
use RPS::Import::SaleRec;
use RPS::DB::Item::MediaType;
use RPS::File::Sale;

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

sub _fieldMap {
    {
        country_code => 13,    # N
        date_month   => 0,     # A
        date_year    => 1,     # B
        type         => 6,     # G
        format       => 7,     # H
        artist       => 8,     # I
        album_name   => 9,     # J
        track_name   => 10,    # K
        upc          => 11,    # L
        isrc         => 12,    # M
        quantity     => 18,    # S
        net_price    => 28,    # AC
    };
}

sub _importLines {
    my $self        = shift;
    my %args        = @_;
    my $fileObj     = $args{file};
    my $version     = $args{file}->VersionNum();
    my $fileName    = $args{file}->OrigFileName();
    my $serviceID   = $args{file}->ServiceID;
    my $sheet_count = 0;

    assert( defined $args{sheets} );
    assert( defined $version );

    my $offsets     = $self->_fieldMap;
    my $sheet_names = $args{sheet_names};
    my $sheets      = $args{sheets};
    my $linenum     = 1;

    for ( my $sheetIndex = 0 ; $sheetIndex < scalar @{ $args{sheets} } ; $sheetIndex++ ) {
        my $sheet = $sheets->[$sheetIndex];

        my $foundHeader = 0;
        for ( my $lineIndex = 0 ; $lineIndex < scalar @$sheet ; $lineIndex++ ) {
            $linenum = $lineIndex + 1;
            my $line = $sheet->[$lineIndex];

            if ( !$foundHeader ) {
                if ( 'LabelShare' eq $line->[ $offsets->{net_price} ] ) {
                    $foundHeader = 1;
                }
                next;
            }

            my $country      = $line->[ $offsets->{country_code} ];
            my $currencyCode = "GBP";

            my $mediaType = RPS::DB::Item::MediaType::kMediaTypeAudio;

            my $trackTitle = $line->[ $offsets->{track_name} ];
            my $albumTitle = $line->[ $offsets->{album_name} ];

            my $artist   = $line->[ $offsets->{artist} ];
            my $upc      = $line->[ $offsets->{upc} ];
            my $isrc     = $line->[ $offsets->{isrc} ];
            my $units    = $line->[ $offsets->{quantity} ];
            my $netPrice = $line->[ $offsets->{net_price} ];

            # Probably wise to allow for blank lines...
            #
            if ( $units || $artist || $albumTitle || $trackTitle ) {

                my $productType;
                my $_type = $line->[ $offsets->{type} ];
                if ( $_type =~ /Track/i ) {
                    $productType = RPS::File::Sale::TYPE_TRACK;
                } elsif ( $_type =~ /Full Release/i ) {
                    $productType = RPS::File::Sale::TYPE_ALBUM;
                } else {
                    $self->errstr("Unknown product type '$_type' in line $linenum\n");
                    return undef;
                }

                my $formatType;
                my $format = $line->[ $offsets->{format} ];
                if ( $format =~ /MP3/i ) {
                    $formatType = RPS::File::Sale::FORMAT_DOWNLOAD;
                } elsif ( $format =~ /WAV/i || $format =~ /FLAC/i ) {
                    $formatType = RPS::File::Sale::FORMAT_DOWNLOADPREMIUM;
                }

                my $dateMonth = $line->[ $offsets->{date_month} ];
                my $dateYear  = $line->[ $offsets->{date_year} ];
                my $date      = sprintf( "%4d-%02d-%02d", $dateYear, $dateMonth, 1 );
                my ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $date, type => "iso" );

                my $unitPrice = $netPrice / $units;

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

                $sale->mediaType($mediaType);
                $sale->productType($productType);
                $sale->formatType($formatType);
                $sale->dateBegin($dateBegin);
                $sale->dateEnd($dateEnd);
                $sale->trackName($trackTitle);
                $sale->albumName($albumTitle);
                $sale->artistName($artist);
                $sale->upc($upc);
                $sale->isrc($isrc);
                $sale->units($units);
                $sale->price($unitPrice);
                $sale->countryCode($country);
                $sale->currencyCode($currencyCode);
                $sale->lineNum($linenum);

                $self->_insertSale( sale => $sale );
            }
        }
    }

    return $linenum;
}

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