package BookPub::Import::Importer::BFC;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( rIsbn13 => 'ISBN' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            rServiceName           => 'D',
            dateBegin              => 'A',
            dateEnd                => 'B',
            rPurchaseType          => 'M',
            purchaseType           => 'M',
            rProductType           => 'K',
            rFormat                => 'L',
            priceType              => 'S',
            rAuthor                => 'H',
            rTitle                 => 'F',
            rSubtitle              => 'G',
            rIsbn13                => 'E',
            rImprint               => 'J',
            rInstitution           => 'Q',
            rState                 => 'N',
            rPostalCode            => 'O',
            countryCode            => 'P',
            rUnits                 => 'T',
            rUnitPrice             => 'AA',
            rListPrice             => 'W',
            rListPriceCurrency     => 'X',
            rPurchasePrice         => 'U',
            rPurchasePriceCurrency => 'V',
            rDiscount              => 'Y',
            rRevenue               => 'AD',
            currencyCode           => 'V',
            rTaxAmount             => 'Z',
            rDuration              => 'R',
            rDistributor           => 'C',
            rChannel               => 'D',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec { 1 }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }

sub priceType {
    my $self = shift;

    my $priceType = $self->_getByFieldName('priceType');

    if ( !$priceType ) {
        $priceType = 'rrp';
    }

    return $priceType;
}

sub rState {
    my $self = shift;

    my $state = $self->_getByFieldName('rState');

    if ( $state =~ /Rest of world/i ) {
        return;
    }

    if ( $state =~ /Washington DC/i ) {
        $state = "DC";
    }

    return $state;
}

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');

    return substr( $date, 0, 4 ) . "-" . substr( $date, 4, 2 ) . "-" . substr( $date, 6, 2 );
}

sub _getDateEnd {
    my $self = shift;
    my $date = $self->_getByFieldName('dateEnd');

    return substr( $date, 0, 4 ) . "-" . substr( $date, 4, 2 ) . "-" . substr( $date, 6, 2 );
}

sub _parseFormat {
    my $self   = shift;
    my $format = shift;

    return unless ( defined($format) );

    $format = sprintf( "%03d", $format ) if ( $format =~ /^\d+$/ );

    return
        $format =~ /^000$/ ? BookPub::DB::Item::Sale::kFormatTypeContentPackage
      : $format =~ /^001$/ ? BookPub::DB::Item::Sale::kFormatTypeHTML
      : $format =~ /^002$/ ? BookPub::DB::Item::Sale::kFormatTypePDF
      : $format =~ /^003$/ ? BookPub::DB::Item::Sale::kFormatTypePDFMerchant
      : $format =~ /^004$/ ? BookPub::DB::Item::Sale::kFormatTypeAdobeEBook
      : $format =~ /^005$/ ? BookPub::DB::Item::Sale::kFormatTypeMSLITLvl1_3
      : $format =~ /^006$/ ? BookPub::DB::Item::Sale::kFormatTypeMSLITLvl5
      : $format =~ /^007$/ ? BookPub::DB::Item::Sale::kFormatTypeNetLibrary
      : $format =~ /^008$/ ? BookPub::DB::Item::Sale::kFormatTypeMetaText
      : $format =~ /^009$/ ? BookPub::DB::Item::Sale::kFormatTypeMightyWords
      : $format =~ /^010$/ ? BookPub::DB::Item::Sale::kFormatTypeEReader
      : $format =~ /^011$/ ? BookPub::DB::Item::Sale::kFormatTypeSoftbook
      : $format =~ /^012$/ ? BookPub::DB::Item::Sale::kFormatTypeRocketBook
      : $format =~ /^013$/ ? BookPub::DB::Item::Sale::kFormatTypeGemStar1100
      : $format =~ /^014$/ ? BookPub::DB::Item::Sale::kFormatTypeGemStar1200
      : $format =~ /^015$/ ? BookPub::DB::Item::Sale::kFormatTypeFranklinEBookman
      : $format =~ /^016$/ ? BookPub::DB::Item::Sale::kFormatTypeBooks24x7
      : $format =~ /^017$/ ? BookPub::DB::Item::Sale::kFormatTypeDigitalOwl
      : $format =~ /^018$/ ? BookPub::DB::Item::Sale::kFormatTypeHandheldmed
      : $format =~ /^019$/ ? BookPub::DB::Item::Sale::kFormatTypeWizeUp
      : $format =~ /^020$/ ? BookPub::DB::Item::Sale::kFormatTypeTK3
      : $format =~ /^021$/ ? BookPub::DB::Item::Sale::kFormatTypeLitraweb
      : $format =~ /^022$/ ? BookPub::DB::Item::Sale::kFormatTypeMOBI
      : $format =~ /^023$/ ? BookPub::DB::Item::Sale::kFormatTypeOPF
      : $format =~ /^024$/ ? BookPub::DB::Item::Sale::kFormatTypeTownCompasData
      : $format =~ /^025$/ ? BookPub::DB::Item::Sale::kFormatTypePlainText
      : $format =~ /^026$/ ? BookPub::DB::Item::Sale::kFormatTypeExeBook
      : $format =~ /^027$/ ? BookPub::DB::Item::Sale::kFormatTypeSonyBBeB
      : $format =~ /^028$/ ? BookPub::DB::Item::Sale::kFormatTypeVitalSource
      : $format =~ /^029$/ ? BookPub::DB::Item::Sale::kFormatTypeEPub
      : $format =~ /^030$/ ? BookPub::DB::Item::Sale::kFormatTypeMyiLibrary
      : $format =~ /^031$/ ? BookPub::DB::Item::Sale::kFormatTypeKindle
      : $format =~ /^032$/ ? BookPub::DB::Item::Sale::kFormatTypeGoogle
      : $format =~ /^033$/ ? BookPub::DB::Item::Sale::kFormatTypeVook
      : $format =~ /^034$/ ? BookPub::DB::Item::Sale::kFormatTypeDXReader
      : $format =~ /^035$/ ? BookPub::DB::Item::Sale::kFormatTypeEBL
      : $format =~ /^036$/ ? BookPub::DB::Item::Sale::kFormatTypeEbrary
      : $format =~ /^037$/ ? BookPub::DB::Item::Sale::kFormatTypeISilo
      : $format =~ /^038$/ ? BookPub::DB::Item::Sale::kFormatTypePlucker
      : $format =~ /^039$/ ? BookPub::DB::Item::Sale::kFormatTypeVitalBook
      : $format =~ /^098$/ ? BookPub::DB::Item::Sale::kFormatTypeMultipleFormats
      : $format =~ /^099$/ ? BookPub::DB::Item::Sale::kFormatTypeUnknownEBook
      :                      undef;
}

sub purchaseType {
    my $self         = shift;
    my $purchaseType = $self->_getByFieldName('purchaseType');
    if ( $purchaseType =~ /loan/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } else {
        $self->fail("Unrecognized purchase type: $purchaseType");
    }
}

sub _isSaleRec {
    my $self = shift;

    # We're going to skip the non-digital product types.
    my $productType = lc( $self->_getByFieldName('rProductType') );

    if ( $productType eq "paperback" ) {
        return 0;
    } elsif ( $productType eq "loose leaf" ) {
        return 0;
    } elsif ( $productType eq "hardback" ) {
        return 0;
    }

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

1;
