package RPS::Import::Tunecore;

use strict;

use Date::Calc qw(Days_in_Month);

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

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';

## not 100% sure what all is used in the tunecore format...
my %countries = (
    'USA' => 'US',
    'GBR' => 'GB',
    'CAN' => 'CA',
);

# map version number to data columns
my %fieldMap = (
    'service'       => 17,
    'territory'     => 19,
    'start_date'    => 1,
    'end_date'      => 2,
    'artist'        => 13,
    'upc'           => 4,
    'album_title'   => 14,
    'isrc'          => 6,
    'track_title'   => 15,
    'units_sold'    => 7,
    'units_stream'  => 9,
    'download_rate' => 8,
    'stream_rate'   => 10,
    'currency_code' => 12,
    'net_price'     => 23,
    'label'         => 16,
);

#public methods

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

    my $lines = $args{lines};

    shift @$lines;
    my $linenum = 2;

    foreach my $line (@$lines) {
        $self->_storeDownload( $linenum, $line );
        $self->_storeStream( $linenum, $line );
        $linenum++;
    }

    return $linenum;
}

#
# Private methods
#

sub _storeDownload {
    my $self    = shift;
    my $lineNum = shift;
    my $line    = shift;
    my $offset  = \%fieldMap;

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

    my $quantity = $line->[ $offset->{'units_sold'} ];
    my $price    = $line->[ $offset->{'download_rate'} ];

    # Don't store unless units have been downloaded
    return unless ($quantity);

    $sale->formatType(RPS::File::Sale::FORMAT_DOWNLOAD);
    $sale->units($quantity);
    $sale->price($price);
    $sale->lineNum($lineNum);

    $self->_storeSale( $line, $sale );

}

sub _storeStream {
    my $self    = shift;
    my $lineNum = shift;
    my $line    = shift;
    my $offset  = \%fieldMap;

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

    my $quantity = $line->[ $offset->{'units_stream'} ];
    my $price    = $line->[ $offset->{'stream_rate'} ];

    # Don't store unless units have been downloaded
    return unless ($quantity);

    $sale->formatType(RPS::File::Sale::FORMAT_STREAM);
    $sale->units($quantity);
    $sale->price($price);
    $sale->lineNum($lineNum);

    $self->_storeSale( $line, $sale );
}

sub _storeSale {
    my $self   = shift;
    my $line   = shift;
    my $sale   = shift;
    my $offset = \%fieldMap;

    my $serviceName  = $line->[ $offset->{'service'} ];
    my $startDate    = $line->[ $offset->{'start_date'} ];
    my $endDate      = $line->[ $offset->{'end_date'} ];
    my $artist       = $line->[ $offset->{'artist'} ];
    my $upc          = $line->[ $offset->{'upc'} ];
    my $album        = $line->[ $offset->{'album_title'} ];
    my $isrc         = $line->[ $offset->{'isrc'} ];
    my $track        = $line->[ $offset->{'track_title'} ];
    my $label        = $line->[ $offset->{'label'} ];
    my $countryCode  = $line->[ $offset->{'territory'} ];
    my $currencyCode = $line->[ $offset->{'currency_code'} ];

    # Convert from three char country code to 2 char.  It would be nice to have a lib
    # to do this.
    #my $country = $countries{$countryCode} || die "Unknown country code '$countryCode'";
    # Now we do have a lib
    my $countryObj = Common::Country->Get($countryCode) || die "Unknown country code '$countryCode'";
    my $country = $countryObj->alpha2();

    my $productType = $track ? RPS::File::Sale::TYPE_TRACK : RPS::File::Sale::TYPE_ALBUM;
    my ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $startDate );

    if ( $artist && ( $album || $track ) ) {
        $sale->mediaType(RPS::DB::Item::MediaType::kMediaTypeAudio);
        $sale->productType($productType);
        $sale->dateBegin($dateBegin);
        $sale->dateEnd($dateEnd);
        $sale->labelName($label);
        $sale->trackName($track);
        $sale->albumName($album);
        $sale->artistName($artist);
        $sale->upc($upc);
        $sale->isrc($isrc);
        $sale->countryCode($country);
        $sale->currencyCode($currencyCode);

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

}

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