package BookPub::Import::Importer::InAudio::Version7;

use strict;

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

use Date::Calc qw(Decode_Month);

sub _headerIdentifier { ( rListPrice => 'Unit price' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        7 => {
            dateBegin              => 'O',
            rPurchaseType          => 'Q',
            rFormat                => 'AD',
            rPriceType             => 'AN',
            priceTypeQualifierID   => 'Q',
            rAuthor                => 'Y',
            rTitle                 => 'X',
            rIsbn13                => 'U',
            rImprint               => 'AC',
            countryCode            => 'AL',
            serviceProductID       => 'W',
            rReturns               => 'AG',
            rSales                 => 'AF',
            rUnits                 => 'AH',
            rListPrice             => 'AM',
            rListPriceCurrency     => 'AO',
            rPurchasePrice         => 'AQ',
            rPurchasePriceCurrency => 'AO',
            rDiscount              => 'AP',
            rRevenue               => 'BQ',
            currencyCode           => 'BQ1',
            isFree                 => 'AI',
            rChannel               => 'N',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub priceType {
    my $self = shift;
    my $type = lc( $self->rPriceType ) || return;

    if ( $type == 41 ) {
        $type = 'agency';
    } elsif ( $type == 6 && Common::RSApp::GetClientID == 318 && $self->sheetname() =~ /NonAgency/i ) { # Macmillan is agency for this value
        $type = 'agency';
    } elsif ( $type == 6 || $type == 1 ) {
        $type = 'rrp';
    }

    return $type;
}

sub productType { return BookPub::DB::Item::Sale::kProductTypeAudioBook }

sub purchaseType {
    my $self = shift;
    my $type = $self->_getByFieldName('rPurchaseType');

    if ( $type =~ /^(?:retail|library||Library-Limited Circ)$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }
    elsif ( $type =~ /^(?:subscription-(?:credit based|unlimited|supplemental credit|combined portions))?(?:pool)?$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    }
    elsif ( lc($type) eq 'library-pay per use' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    }
    else {
        $self->fail("Could not determine purchase type from $type");
    }

}

sub priceTypeQualifierID {
    my $self = shift;
    my $type = $self->_getByFieldName('priceTypeQualifierID');

    if ( $type =~ /library/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;    # Library
    } elsif ( $type =~ /(?:library-pay per use|Library-Limited Circ)/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;    # Also Library
    }

    return undef;
}

sub currencyCode {
    my $self = shift;
    my $code = $self->_getByFieldName('currencyCode');

    if ( $code =~ /royalty payable in (\w{3})/i ) {
        return $1;
    }

    $self->fail("Could not determine currency code from $code");
}

sub rReturns {
    my $self    = shift;

    my $returns = $self->_getByFieldName("rReturns");

    return abs($returns);
}

sub isFree {
    my $self = shift;
    my $free = $self->_getByFieldName('isFree');

    if ( $free && $free > 0 ) {
        return 'Y';
    } else {
        return 'N';
    }
}

sub discount {
    my $self      = shift;
    my $unitPrice = $self->unitPrice;
    my $listPrice = $self->listPrice + 0;
    my $discount;

    my $listPriceCurrency = $self->rListPriceCurrency();
    my $unitPriceCurrency = $self->currencyCode();

    if (   defined($unitPrice)
        && defined($listPrice)
        && $listPrice != 0
        && ( !$listPriceCurrency || $listPriceCurrency eq $unitPriceCurrency ) ) {
        $discount = 1 - $unitPrice / $listPrice;
    } else {

        # No discount stored if we dont't have it and can't derive it.
    }

    return $discount && $discount > 1 ? $discount / 100 : $discount;
}

sub rServiceName { BookPub::Tracker::Service::IN_AUDIO }

sub _isSaleRec {
    my $self = shift;

    return ( $self->rTitle || $self->dateBegin || $self->rUnits );
}

1;
