package BookPub::Import::Importer::CopyrightClearance;

use strict;
use Data::Dumper;

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

sub _headerIdentifier { ( rTitle => 'Title' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin     => 'L',
            rPurchaseType => 'K',
            rProductType  => 'E',
            rTitle        => 'G',
            rIsbn13       => 'F',
            rImprint      => 'D',
            countryCode   => 'H',
            rUnitPrice    => 'H',
            rRevenue      => 'H',
            currencyCode  => 'H',
        },
        2 => {
            dateBegin              => 'P',
            rPurchaseType          => 'G',
            rTitle                 => 'J',
            rIsbn13                => 'I',
            countryCode            => 'L',
            rUnitPrice             => 'L',
            rPurchasePrice         => 'M',
            rPurchasePriceCurrency => 'L',
            rDiscount              => 'N',
            rRevenue               => 'L',
            currencyCode           => 'L',
            rTaxAmount             => 'O',
        },
        3 => {
            dateBegin            => 'AF',
            rProductType         => 'AG',
            rAuthor              => 'X',
            rTitle               => 'R',
            rIsbn13              => 'S',
            rInstitution         => 'M',
            countryCode          => 'Q',
            rUnitPrice           => 'Q',
            rRevenue             => 'Q',
            currencyCode         => 'Q',
            rPromoCode           => 'N',
            rChannel             => 'J',
            rModel               => 'Z',
            rTransactionCategory => 'K',
        },
    );

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

sub priceType { 'rrp' }

sub _dateFormat { [ 'iso', 'text' ] }
sub _autoParseTitleAndSubtitle { 1 }
sub _useIsSaleRec              { 1 }

sub units { return ( shift->rRevenue >= 0 ) ? 1 : -1 }

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

    if ( $type =~ /Pay-Per-Use/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $type =~ /Foreign Authorizations - Title Specific/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } else {
        $self->fail("Unexpected purchase type, $type");
    }
}

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

    if ( $type =~ /Book/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( $type =~ /Encyclopedias/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } else {
        $self->fail("Unexpected product type, $type");
    }
}

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

    if ( $self->_isHeader() ) {
        $code =~ /\((\w{3})\)/;
        if ( $1 eq 'USD' ) {
            $self->{_countryCode}  = 'US';
            $self->{_currencyCode} = 'USD';
        } else {
            $self->fail("Unexpected currency value in header, $code");
        }
    }
}

sub countryCode {
    my $self = shift;

    if ( $self->{_countryCode} ) {
        return $self->{_countryCode};
    } else {
        $self->fail("Country code has not been determined");
    }
}

sub currencyCode {
    my $self = shift;

    if ( $self->{_currencyCode} ) {
        return $self->{_currencyCode};
    } else {
        $self->fail("Currency code has not been determined");
    }
}

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

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    return $date

}

1;
