package BookPub::Import::Importer::TuneCore::Version1;

use strict;
use warnings;

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

use lib '/app/tools/bookpub/lib';
use BookPub::Tracker::Service;
use base 'BookPub::Import::Importer';


sub _fieldMap {
    {
        dateBegin          => 'A',
        purchaseType       => 'N',
        rAuthor            => 'E',
        rTitle             => 'G',
        rIsbn13            => 'K',
        countryCode        => 'D',
        serviceProductID   => 'I',
        rUnits             => 'O',
        rUnitPrice         => 'P',
        rListPriceCurrency => 'R',
        rRevenue           => 'T',
        currencyCode       => 'U',
        rDistributor       => 'C',
        conversionRate     => 'S',
    };
}

sub _headerIdentifier { countryCode => 'Country Of Sale' }

sub _useIsSaleRec              { 1 }
sub priceType                  { 'rrp' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateNormalization         { 'month' }

sub rServiceName { 'Tunecore' }

sub _getDateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || return;

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    return $date

}


sub _validTabNames { [ 'Sheet1', 'Sheet 1', 'tunecore-musicsales-sales_p' ] }

sub _isValidSaleRecord {
    my $self = shift;

    # Only process sales first sheet
    my $validTab;
    my $validTabNames = _validTabNames();
    foreach my $validTabName (@$validTabNames) {
        if ( $self->sheetname() =~ /$validTabName/i ) {
            $validTab = 1;
        }
    }
    return undef unless $validTab;

    return $self->SUPER::_isValidSaleRecord();
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || 0;

    # Checking for scientific notation
    if ( $revenue =~ /e/ ) {
        $revenue = sprintf( "%.8f", $revenue );
    }

    return $revenue;
}

sub units {
    my $self = shift;

    my $units     = $self->_getByFieldName('rUnits')     || 0;
    my $unitPrice = $self->_getByFieldName('rUnitPrice') || 0;

    # Checking for scientific notation
    if ( $unitPrice =~ /e/ ) {
        $unitPrice = sprintf( "%.8f", $unitPrice );
    }

    return ( $unitPrice < 0 && $units > 0 ) ? -1 * $units : $units;
}

sub unitPrice {
    my $self = shift;

    my $unitPrice = $self->_getByFieldName('rUnitPrice') || 0;

    # Checking for scientific notation
    if ( $unitPrice =~ /e/ ) {
        $unitPrice = sprintf( "%.8f", $unitPrice );
    }

    return abs($unitPrice);
}

sub purchaseType {
    my $self = shift;

    my $type = $self->_getByFieldName('purchaseType') || '';

    if ( lc($type) eq 'download' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } elsif ( lc($type) eq 'streaming' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeStream;
    } else {
        $self->fail( "Unexpected purchase type of " . $type . "encountered" );
    }
}


1;
