package BookPub::Import::Importer::Bookshop::v3;

use strict;
use warnings;

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

sub _fieldMap {
    my $self = shift;

    my %map = (
        3 => {
            dateBegin   => 'B',
            dateEnd     => 'B',
            rAuthor     => 'H',
            rTitle      => 'F',
            rIsbn13     => 'E',
            countryCode => 'C',
            rReturns    => 'P',
            rSales      => 'O',
            rUnits      => 'Q',
            rListPrice  => 'I',
            rDiscount   => 'L',
            rRevenue    => 'S',
        },
        5 => {
            dateBegin          => 'B',
            dateEnd            => 'B',
            rAuthor            => 'G',
            rTitle             => 'F',
            rIsbn13            => 'E',
            countryCode        => 'C',
            rReturns           => 'P',
            rSales             => 'O',
            rUnits             => 'Q',
            rListPrice         => 'I',
            rListPriceCurrency => 'H',
            rDiscount          => 'L',
            rRevenue           => 'S',
            currencyCode       => 'H',
        },
    );

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

sub _headerIdentifier { rTitle => 'title' }

sub _useIsSaleRec { 1 }
sub rServiceName  { 'Bookshop' }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }


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 $date = $self->_getByFieldName('dateBegin') || '';
    $date = int($date) if ( $date =~/\./ );

    # YYYYMMDD or YYYY-MM-DD
    if ( my ($y, $m, $d) = $date =~ /(\d{4}).(\d{2}).(\d{2})$/) {
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }
    # 1-Oct-25
    elsif ( ($d, $m, $y) = $date =~ /(\d{1,2})\-(\w{3})\-(\d{2,4})$/i ) {
        $m = $months{lc $m} if exists $months{lc $m};
        $y = length($y) == 2 ? "20$y" : $y;
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }

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

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';
    $date = int($date) if ( $date =~/\./ );

    # YYYYMMDD or YYYY-MM-DD
    if ( my ($y, $m, $d) = $date =~ /(\d{4}).(\d{2}).(\d{2})$/) {
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }
    # 1-Oct-25
    elsif ( ($d, $m, $y) = $date =~ /(\d{1,2})\-(\w{3})\-(\d{2,4})$/i ) {
        $m = $months{lc $m} if exists $months{lc $m};
        $y = length($y) == 2 ? "20$y" : $y;
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }

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

sub priceType {
    my $self = shift;

    my $clientID = Common::RSApp::GetClientID();

    return 'rrp'    if $clientID =~ /^(?:624)$/;      # BBUK
    return 'agency' if $clientID =~ /^(?:355|359)$/;  # HUK, MMUK
}

sub rListPrice {
    my $self = shift;

    my $rListPrice = $self->_getByFieldName('rListPrice') || 0;
    $rListPrice =~ s/[^0-9().-]//g;

    my $sign = 1;
    if ( $rListPrice =~ /\(|\)/ ) {
        $sign = ($rListPrice =~ s/^\((.+)\)$/$1/) ? -1 : 1;
    }

    return $rListPrice * $sign;
}

sub rRevenue {
    my $self = shift;

    my $rRevenue = $self->_getByFieldName('rRevenue') || 0;
    $rRevenue =~ s/[^0-9().-]//g;

    my $sign = 1;
    if ( $rRevenue =~ /\(|\)/ ) {
        $sign = ($rRevenue =~ s/^\((.+)\)$/$1/) ? -1 : 1;
    }

    return $rRevenue * $sign;
}

sub rUnits {
    my $self = shift;

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

sub currencyCode {
    my $self = shift;

    return 'GBP' if $self->version == 3;
    return $self->_getByFieldName('currencyCode') if $self->version == 5;
}
sub rListPriceCurrency {
    my $self = shift;

    return 'GBP' if $self->version == 3;
    return $self->_getByFieldName('currencyCode') if $self->version == 5;
}

sub isFree {
    my $self = shift;

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

sub _isSaleRec {
    my $self = shift;

    return $self->rIsbn13 && ($self->rRevenue || $self->rUnits);
}


1;