package BookPub::Import::Importer::Yuzu;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            rPurchaseType    => 'F',
            serviceProductID => 'E',
            rPostalCode      => 'H',
            dateBegin        => 'J',
            rIsbn13          => 'D',
            rSales           => 'I',
            rReturns         => 'I',
            rRevenue         => 'C',
        },
        2 => {
            rPurchaseType    => 'L',
            serviceProductID => 'E',
            currencyCode     => 'J',
            rPostalCode      => 'N',
            dateBegin        => 'P',
            rIsbn13          => 'D',
            rTitle           => 'F',
            rAuthor          => 'G',
            rUnits           => 'I',
            rSales           => 'O',
            rReturns         => 'O',
            rListPrice       => 'H',
            rRevenue         => 'C',
        },
        3 => {
            rPurchaseType    => 'H',
            serviceProductID => 'B',
            currencyCode     => 'L',
            rPostalCode      => 'O',
            dateBegin        => 'M',
            rFormat          => 'F',
            rIsbn13          => 'C',
            rTitle           => 'D',
            rAuthor          => 'E',
            rUnits           => 'I',
            rSales           => 'P',
            rReturns         => 'P',
            rListPrice       => 'J',
            rRevenue         => 'K',
            rInstitution     => 'N',
        },
        4 => {
            rPurchaseType    => 'H',
            priceType        => 'G',
            serviceProductID => 'B',
            currencyCode     => 'P',
            countryCode      => 'Q',
            rPostalCode      => 'N',
            dateBegin        => 'L',
            rIsbn13          => 'C',
            rTitle           => 'D',
            rAuthor          => 'E',
            rComments        => 'G',
            rUnits           => 'I',
            rSales           => 'O',
            rReturns         => 'O',
            rListPrice       => 'J',
            rRevenue         => 'K',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            subtotalCheck => 'A',
        },
        2 => {
            subtotalCheck => 'A',
        },
        3 => {
            subtotalCheck => 'A',
        },
        4 => {
            subtotalCheck => 'A',
        },
    );

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

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

sub rFormat {
    my $self   = shift;
    my $format = $self->_getByFieldName('rFormat');

    if ( defined $format && lc($format) == 'ep' ) {
        return BookPub::DB::Item::Sale::kFormatTypeEPub;
    } else {
        return $self->SUPER::rFormat();
    }
}

sub priceType {
    my $self = shift;

    # For Hayhouse only and Version 2 of the importer, we're going to
    # set the price type to RRP.
    if ( $self->_version() == 2 && Common::RSApp::GetClientID == 317 ) {
        return 'rrp';
    }

    # For Version 3 of the importer, we're going to
    # set the price type to RRP for Bloomsbury, Hay House, RLPG and SAGE
    if (
        $self->_version() == 3 && (
            Common::RSApp::GetClientID == 314 ||    # Bloomsbury USA
            Common::RSApp::GetClientID == 317 ||    # Hay House
            Common::RSApp::GetClientID == 346 ||    # RLPG
            Common::RSApp::GetClientID == 365
        )
      ) {                                           # Sage
        return 'rrp';
    }

    # For Macmillan, we're going to derive te price type based on the Contract Key
    if ( defined $self->_getByFieldName('priceType') && Common::RSApp::GetClientID == 318 ) {
        my $priceType = $self->_getByFieldName('priceType');
        if ( $priceType == 100024 ) {
            return 'agency';
        } elsif ( $priceType == 100074 ) {
            return 'agency';
        } else {
            $self->fail("Unknown contract key $priceType");
        }
    }

    return 'agency';
}

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

    return substr( $dateBegin, 0, 10 );
}

sub currencyCode {
    my $self = shift;

    if ( defined $self->_getByFieldName('currencyCode') ) {
        return $self->_getByFieldName('currencyCode');
    } else {
        return 'USD';
    }
}

sub countryCode {
    my $self = shift;

    if ( defined $self->_getByFieldName('countryCode') ) {
        return $self->_getByFieldName('countryCode');
    } else {
        return 'US';
    }
}

sub rSales {
    my $self = shift;

    my $sales = $self->_getByFieldName('rSales');
    if ( $sales =~ /sale/i ) {
        return 1;
    }
}

sub rReturns {
    my $self = shift;

    my $returns = $self->_getByFieldName('rReturns');
    if ( $returns =~ /return/i || $returns =~ /void/i ) {
        return 1;
    }
}

sub rPostalCode {
    my $self = shift;

    my $postalCode = $self->_getByFieldName('rPostalCode');
    if ( $postalCode !~ /N\/A/i ) {
        return $postalCode;
    }
}

sub purchaseType {
    my $self = shift;

    my $purchaseType = uc( $self->_getByFieldName('rPurchaseType') );
    if ( $purchaseType eq 'PURCHASE' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } elsif ( $purchaseType eq 'RENTAL' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    }

    $self->fail("Could not determine purchase type from $purchaseType");
}

sub rListPrice {
    my $self = shift;

    if ( defined $self->_getByFieldName('rListPrice') && $self->_getByFieldName('rListPrice') ne '' ) {
        return $self->_getByFieldName('rListPrice');
    } else {
        my $listPrice    = $self->_getByFieldName('rRevenue');
        my $purchaseType = $self->purchaseType();
        if ( $purchaseType eq BookPub::DB::Item::Sale::kPurchaseTypeDownload ) {
            $listPrice /= .70;
        } elsif ( $purchaseType eq BookPub::DB::Item::Sale::kPurchaseTypeLoan ) {
            $listPrice /= .85;
        } else {
            $self->fail("Unable to calculate list price for purchase type: $purchaseType");
        }
        return abs($listPrice);
    }
}

sub _isSaleRec {
    my $self = shift;

    # We're going to check for the subtotal line
    my $subtotalCheck = $self->_getByFieldName('subtotalCheck');
    if ( $subtotalCheck =~ /total$/i ) {
        return 0;
    }

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

1;
