package BookPub::Import::Importer::Perusall::Version2;

use strict;

use lib '/app/tools/bookpub/lib';

use base 'BookPub::Import::Importer';

sub _fieldMap {
    {
        dateBegin      => 'B',
        rPurchaseType  => 'L',
        rAuthor        => 'J',
        rTitle         => 'I',
        rIsbn13        => 'H',
        rInstitution   => 'D',
        rState         => 'F',
        countryCode    => 'G',
        rListPrice     => 'P',
        rRevenue       => 'U',
        rTaxUnitPrice  => 'O',
        rPurchasePrice => 'N',
        rCOGS          => 'V',
        rDuration      => 'M',
        rComments      => 'W',
    };
}

sub _unverifiedFieldMap {
    { notes => 'C', };
}

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

sub _processHeader {
    my $self = shift;

    unless ($self->{_currency}) {
        my $currency = $self->_getByFieldName("rListPrice");
        if ($currency =~ /\((\w{3})\)$/ ) {
            $self->{_currency} = $1;
        }
        else {
            $self->{_currency} = "USD";
        }
    }
    return $self->SUPER::_processHeader();
}

sub _useIsSaleRec          { 1 }
sub productType            { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType           { BookPub::DB::Item::Sale::kProductTypeEBook }
sub currencyCode           { $_[0]->{_currency} if $_[0]->{_currency} }
sub rListPriceCurrency     { shift->currencyCode }
sub rPurchasePriceCurrency { shift->currencyCode }
sub rTaxAmount             { shift->rTaxUnitPrice }
sub priceType              { Common::RSApp::GetClientID == 318 ? 'agency' : 'rrp' }    # MMUS

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName(
        service_id => shift->serviceID
    );
}

sub rIsbn13 {
    return substr(shift->_getByFieldName('rIsbn13'), 0, 13);
}

sub _isSaleRec {
    my $self = shift;

    return $self->rRevenue() && $self->_getByFieldName('notes') !~ /refunded/i;
}

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

    my %months = (
        jan => 1,
        feb => 2,
        mar => 3,
        apr => 4,
        may => 5,
        jun => 6,
        jul => 7,
        aug => 8,
        sep => 9,
        oct => 10,
        nov => 11,
        dec => 12,
    );

    if ( $date =~ /\w{3}\s(\w{3})\s(\d{2})\s(\d{4})/ ) {
        my $day = $2;
        my $year  = $3;
        my $month = $months{lc($1)};

        $date = sprintf( "%04d-%02d-%02d", $year, $month, $day );
    } elsif ( $date =~ /^(\d{4})-(\d{2})-(\d{2})T.+$/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    }

    return $date;
}

sub purchaseType {
    my $self = shift;

    my $purchaseType;
    if ( $self->rPurchaseType =~ /^Perpetual$/i ) {
        $purchaseType = BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    }
    else {
        $purchaseType = BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    }

    return $purchaseType;
}

sub state {
    my $self = shift;

    my ($state) = $self->rState =~ /.*, ([A-Z][A-Z])$/;

    return $state;
}

sub rUnits {
    my $self = shift;

    return $self->revenue < 0 ? -1 : 1;
}

sub rTaxUnitPrice {
    my $self = shift;

    my $tax = $self->_getByFieldName('rTaxUnitPrice');
    $tax =~ s/\$//;

    return $tax;
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue');
    $revenue =~ s/\$//;

    return $revenue;
}

sub rListPrice {
    my $self = shift;

    my $listPrice = $self->_getByFieldName('rListPrice');
    $listPrice =~ s/\$//;

    return abs($listPrice);
}

sub rPurchasePrice {
    my $self          = shift;
    my $purchasePrice = $self->_getByFieldName('rPurchasePrice');
    $purchasePrice =~ s/\$//;

    return abs($purchasePrice);
}

1;

