package BookPub::Import::Importer::BISG::Version4;

use strict;

use lib '/app/tools/common/lib';
use Common::Country;

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

sub _fieldMap {
    {
        # Detail
        rPriceType         => 'AM',
        currencyCode       => 'H',
        countryCode        => 'AK',
        dateBegin          => 'N',
        rProductType       => 'AC',
        rIsbn13            => 'T',
        rTitle             => 'W',
        rAuthor            => 'X',
        rImprint           => 'AB',
        rSales             => 'AE',
        rReturns           => 'AF',
        rListPrice         => 'AL',
        rListPriceCurrency => 'AN',
        rPurchasePrice     => 'AP',
        rDiscount          => 'AO',
        rRevenue           => 'BB',
    };
}

sub _unverifiedFieldMap {
    { returnPrice => 'AQ', };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _headerIdentifier          { ( rTitle => 'Product title' ) }
sub _validTabNames             { [ 'AgencyTransaction', 'NonAgencyTransaction', 'NonAgencyTransction', 'NonAgencyTransation' ] }

sub _isSaleRec {
    my $self = shift;

    # Only process sales from the transaction sheets
    my $validTab;
    my $validTabNames = _validTabNames();
    foreach my $validTabName (@$validTabNames) {
        if ( $self->sheetname() =~ /$validTabName/i ) {
            $validTab = 1;
        }
    }
    return undef unless $validTab;

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

sub units {
    my $self = shift;

    my $units   = $self->SUPER::units();
    my $revenue = $self->SUPER::revenue;

    # flip units signage if it doesn't have the same polarity as revenue
    $units *= -1 if ( ( $units > 0 && $revenue < 0 ) || ( $units < 0 && $revenue > 0 ) );

    return $units;
}

sub purchaseType {
    return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
}

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

    if ( $type == 6 && ( $self->countryCode eq 'US' || $self->countryCode eq 'CA' ) ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;
    } else {
        return undef;
    }
}

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

    if ( $type eq 'other' || $type eq 'e101' || $type eq 'e107' ) {
        $type = BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( $type eq 'a103' || $type eq 'a106' ) {
        $type = BookPub::DB::Item::Sale::kProductTypeAudioBook;
    }

    return $type;
}

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

    if ( $type == 41 ) {
        $type = 'agency';
    } elsif ( $type == 6 || $type == 1 ) {
        $type = 'rrp';
    }

    return $type;
}

sub rPurchasePrice {
    my $self = shift;
    my $price;
    my $purchasePrice = $self->_getByFieldName('rPurchasePrice');
    my $returnPrice   = $self->_getByFieldName('returnPrice');
    if ( $self->units() ) {
        if ( $purchasePrice && $purchasePrice ne '' && $purchasePrice != 0 ) {
            $price = $purchasePrice / $self->units();
        } elsif ( $returnPrice && $returnPrice ne '' && $returnPrice != 0 ) {
            $price = $returnPrice / $self->units();
        }
    }
    return abs($price);
}

sub rListPrice {
    my $self = shift;
    if ( $self->units() ) {
        my $listPrice = $self->_getByFieldName('rListPrice');
        $listPrice /= $self->units();
        my $priceCurrency = $self->_getByFieldName('rListPriceCurrency');
        my $currencyCode  = $self->_getByFieldName('currencyCode');

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

            # We're just going to have to use purchase price as the list price.
            $listPrice = $self->rPurchasePrice;
        }

        return abs($listPrice);
    }
}

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

    # For the clients that require list price currency to match the revenue currency.
    # We'll do the actual conversion in rListPrice.
    if ( $self->listPriceRequired && $priceCurrency ne $currencyCode ) {
        $priceCurrency = $currencyCode;
    }

    return $priceCurrency;
}

1;
