package BookPub::Import::Importer::Bloomsbury::v2;

use strict;
use warnings;

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

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

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

sub _fieldMap {
    {
        dateBegin                => 'Z',
        dateEnd                  => 'Z',
        rProductType             => 'P',
        rTitle                   => 'O',
        rIsbn13                  => 'N',
        countryCode              => 'K',
        rUnits                   => 'M',
        rListPrice               => 'AB',
        rListPriceCurrency       => 'C',
        rRevenue                 => 'AD',
        currencyCode             => 'C',
        rTaxAmount               => 'T',
    };
}

sub _unverifiedFieldMap {{}}
sub _headerIdentifier { rTitle => 'ProductName' }


sub rServiceName  { 'Bloomsbury.com' }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType     { 'rrp' }
sub _useIsSaleRec { 1 }


sub _getDateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    # YYYY/MM/DD
    if ( $date =~ /^(\d{4}).(\d{1,2}).(\d{1,2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }
    # MM/DD/YYYY
    if ( $date =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/ ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    $self->fail("Unable to determine date begin from: '$date'");
}

sub _getDateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';
    # YYYY/MM/DD
    if ( $date =~ /^(\d{4}).(\d{1,2}).(\d{1,2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }
    # MM/DD/YYYY
    if ( $date =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/ ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    $self->fail("Unable to determine date begin from: '$date'");
}

sub productType {
    my $self = shift;

    my $productType = $self->_getByFieldName('rProductType') || '';

    return BookPub::DB::Item::Sale::kProductTypeMultiEBookBundle if $productType =~ /^ebook.*?bundle/i;
    return BookPub::DB::Item::Sale::kProductTypeEBook if $productType =~ /^ebook/i;
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if $productType =~ /^audiobook$/i;

    $self->fail("Unable to determine product type from: '$productType'");

}

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName('countryCode') || '';

    return 'CZ' if $code eq 'Czechia';
    return 'KR' if $code eq 'Korea (Republic of)';
    return 'MD' if $code eq 'Moldova (Republic of)';

    my $countryObj = Common::Country->Get($code);

    $self->fail("Unable to determine country code from: '$code'") unless $countryObj;

    return $countryObj->alpha2();
}

sub rUnitPrice {
    my $self = shift;

    if ( $self->rUnits ) {
        return $self->rRevenue / $self->rUnits;
    }

    return 0;
}

sub rUnits {
    my $self = shift;

    my $rUnits = $self->_getByFieldName('rUnits') || 0;
    return $rUnits;
}

sub rListPrice {
    my $self = shift;

    my $rListPrice = $self->_getByFieldName('rListPrice') || 0;
    return $rListPrice;
}

sub discount {
    my $self = shift;

    if ( $self->rListPrice && $self->rUnits ) {
        return 1 - ( $self->rUnitPrice / $self->rListPrice );
    }

    $self->fail("Unable to calculte discount, since rListPrice or rUnits is zero");
}

sub rRevenue {
    my $self = shift;

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

my %territoryCurrencyMap = (
    'australia' => 'AUD',
    'canada' => 'CAD',
    'united kingdom' => 'GBP',
    'united states' => 'USD',
);

sub currencyCode {
    my $self = shift;

    my $territory = lc($self->_getByFieldName('currencyCode')) || '';

    return $territoryCurrencyMap{$territory} if (exists $territoryCurrencyMap{$territory});

    $self->fail("Unable to determine currency code from: '$territory'");
}

sub rListPriceCurrency {
    my $self = shift;

    my $territory = lc($self->_getByFieldName('currencyCode')) || '';

    return $territoryCurrencyMap{$territory} if (exists $territoryCurrencyMap{$territory});

    $self->fail("Unable to determine List Price Currency from: '$territory'");

    return $self->currencyCode;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y': 'N';
}

1;
