package BookPub::Import::Importer::HooplaDigital::v2;

use strict;
use warnings;
use POSIX;

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

use constant RE_DATE1 => qr/^\w{3}\s(\w{3})\s(\d{1,2})\s\d{2}:\d{2}:\d{2}\s\w{3}\s(\d{4})$/;
use constant RE_DATE2 => qr/^(\d{4})\-(\d{1,2})\-(\d{1,2})\s\d{2}:\d{2}:\d{2}.*$/;

sub _fieldMap {
    my $self = shift;

    my %map = (
        2 => {
            dateBegin                => 'B',
            dateEnd                  => 'B',
            rProductType             => 'G',
            rAuthor                  => 'F',
            rTitle                   => 'D',
            rIsbn13                  => 'H',
            rInstitution             => 'L',
            rState                   => 'N',
            rPostalCode              => 'O',
            countryCode              => 'M',
            rClientProductID         => 'J',
            rUnitPrice               => 'R',
            rListPrice               => 'P',
            rListPriceCurrency       => 'Q',
            rCOGS                    => 'S',
            rRevenue                 => 'T',
            currencyCode             => 'U',
            rModel                   => 'A',
            rNativeListPrice         => 'P',
            rNativeListPriceCurrency => 'Q',
        },
        4 => {
            dateBegin                => 'B',
            dateEnd                  => 'B',
            rProductType             => 'G',
            rAuthor                  => 'F',
            rTitle                   => 'D',
            rIsbn13                  => 'H',
            rInstitution             => 'M',
            rState                   => 'N',
            rPostalCode              => 'O',
            countryCode              => 'L',
            rClientProductID         => 'J',
            serviceProductID         => 'C',
            rUnitPrice               => 'R',
            rListPrice               => 'P',
            rListPriceCurrency       => 'Q',
            rCOGS                    => 'S',
            rRevenue                 => 'T',
            currencyCode             => 'U',
            rModel                   => 'A',
            rNativeListPrice         => 'P',
            rNativeListPriceCurrency => 'Q',
        },
        5 => {
            dateBegin                => 'B',
            dateEnd                  => 'B',
            rProductType             => 'H',
            rAuthor                  => 'G',
            rTitle                   => 'E',
            rIsbn13                  => 'I',
            rInstitution             => 'M',
            rState                   => 'O',
            rPostalCode              => 'P',
            countryCode              => 'N',
            rClientProductID         => 'K',
            rUnitPrice               => 'S',
            rListPrice               => 'Q',
            rListPriceCurrency       => 'R',
            rNativeListPrice         => 'Q',
            rNativeListPriceCurrency => 'R',
            rCOGS                    => 'T',
            rRevenue                 => 'U',
            currencyCode             => 'V',
            rModel                   => 'A',
        },
    );

    return $map{ $self->version };
}

sub _headerIdentifier    { rIsbn13 => 'ISBN' }

my %months = (
    "jan" => 1,
    "feb" => 2,
    "mar" => 3,
    "apr" => 4,
    "may" => 5,
    "jun" => 6,
    "jul" => 7,
    "aug" => 8,
    "sep" => 9,
    "oct" => 10,
    "nov" => 11,
    "dec" => 12
);

sub dateBegin {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin') || '';
    if ( my ($month, $day, $year) = $dateBegin =~ RE_DATE1 ) {
        return sprintf "%04d-%02d-%02d", $year, $months{lc $month}, $day if exists $months{lc $month};
    }
    if ( my ($year, $month, $day) = $dateBegin =~ RE_DATE2 ) {
        return sprintf "%04d-%02d-%02d", $year, $month, $day;
    }

    $self->fail("Can't determine date: $dateBegin");
}

sub dateEnd { shift->dateBegin }

sub rListPrice {
    my $self = shift;

    my $rCOGS        = $self->rCOGS;
    my $rRevenue     = $self->_getByFieldName('rRevenue')   || 0;
    my $rListPrice   = $self->_getByFieldName('rListPrice') || 0;
    my $currencyCode = $self->_getByFieldName('currencyCode')       || '';
    my $rListPriceCurrency = $self->_getByFieldName('rListPriceCurrency') || 0;

    if ( $self->listPriceRequired && $rListPriceCurrency ne $currencyCode ) {
        $self->fail("Cannot calculate conversion rate, because List Price or COGS is 0") if ($rListPrice == 0 || $rCOGS == 0);
        my $conversionRate = $rRevenue / ( $rListPrice * $rCOGS );
        return $rListPrice * $conversionRate;
    }

    return $rListPrice;
}

sub rListPriceCurrency {
    my $self = shift;

    my $priceCurrency = $self->_getByFieldName('rListPriceCurrency') || '';
    my $currencyCode  = $self->_getByFieldName('currencyCode')       || '';

    if ( $self->listPriceRequired && $priceCurrency ne $currencyCode ) {
        return $self->currencyCode;
    }

    return $priceCurrency;
}

sub countryCode {
    my $self = shift;
    return $self->_getByFieldName('countryCode') || '';
}

sub isFree {
    my $self = shift;

    return 'N' if $self->version != 5;

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

sub currencyCode {
    my $self = shift;

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

    if ($currencyCode =~ /^CAD$/i && $self->productType() eq BookPub::DB::Item::Sale::kProductTypeAudioBook) {
        $self->fail("Wrong currency code '$currencyCode' for product type AudioBook");
    }

    return $currencyCode;
}

1;
