package BookPub::Import::Importer::Vivlio::v2;

use strict;
use warnings;

use Common::UTF8;

use lib '/app/tools/bookpub/lib';
use Date::Calc qw(Days_in_Month);

use parent 'BookPub::Import::Importer';

sub _fieldMap {
    {
        rServiceName           => 'G',
        dateBegin              => 'A',
        dateEnd                => 'A',
        rProductType           => 'E',
        rFormat                => 'E',
        rTitle                 => 'D',
        rIsbn13                => 'C',
        rImprint               => 'B',
        rDistributor           => 'G',
        rState                 => 'L',
        countryCode            => 'K',
        rUnits                 => 'N',
        rListPrice             => 'P',
        rListPriceCurrency     => 'W',
        rPurchasePrice         => 'O',
        rPurchasePriceCurrency => 'W',
        rRevenue               => 'AH',
        currencyCode           => 'AC',
        rTaxAmount             => 'AG',
        rTaxUnitPrice          => 'AF',
        isPreOrder             => 'AI',
        rPromoCode             => 'X',
        rChannel               => 'H',
    };
}

sub _unverifiedFieldMap {
    {
        _sale_cancellation    => 'M'
    }
}

sub _headerIdentifier { rIsbn13 => 'EAN' }

sub _useIsSaleRec { 1 }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rTitle        { Common::UTF8::Encode( shift->_getByFieldName('rTitle')       || '' ) }
sub rInstitution  { Common::UTF8::Encode( shift->_getByFieldName('rInstitution') || '' ) }
sub rChannel      { Common::UTF8::Encode( shift->_getByFieldName('rChannel')     || '' ) }
sub rReturns      { (shift->_getByFieldName("_sale_cancellation")) eq 'A' ? 1 : undef }
sub rSales        { (shift->_getByFieldName("_sale_cancellation")) eq 'V' ? 1 : undef }

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('rProductType') || '';
    return BookPub::DB::Item::Sale::kProductTypeEBook if $type =~ /^(?:epub( fixed layout)?|multi\-format)$/i;
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if $type =~ /^mp3$/i;

    $self->fail("Can't determine productType from: '$type'.");
}

sub priceType {
    # MMUK - rrp
    return Common::RSApp::GetClientID == 359 ? 'rrp' : 'agency';
}

sub rServiceName {
    my $self = shift;

    my $name = $self->_getByFieldName('rServiceName') || '';
    return 'Vivlio' if $name =~ /^TEA$/i;

    $self->fail("Can't determine rServiceName from: '$name'.");
}

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    if ( $date =~ /^(\d{4})-(\d{1,2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, 1;
    }

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

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';
    if ( $date =~ /^(\d{4})-(\d{1,2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, Days_in_Month( $1, $2 );
    }

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

sub rUnits {
    my $self = shift;

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

    return $rUnits;
}

sub rFormat {
    my $self = shift;

    my $rFormat = $self->_getByFieldName('rFormat') || '';
    return 'epub' if $rFormat =~ /(?:epub( fixed layout)?|multi\-format)/i;
    return 'mp3' if $rFormat =~ /^mp3$/i;

    $self->fail("Can't determine rFormat from: '$rFormat'.");
}

sub isPreOrder {
    my $self = shift;

    my $preOrder = $self->_getByFieldName('isPreOrder') || '';
    return "Y" if $preOrder =~ /^OUI|YES$/i;
    return "N" if $preOrder =~ /^NON|NO$/i;

    $self->fail("Can't determine isPreOrder from: '$preOrder'.");
}

sub _isSaleRec {
    my $self = shift;

    return unless (
           $self->rTitle
        && $self->rIsbn13
        && $self->countryCode
        && $self->currencyCode
    );

    return $self->BookPub::Import::Importer::_isSaleRec();
}


1;
