package BookPub::Import::Importer::Chegg::Version10;

use strict;
use Data::Dumper;

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

use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::Sale;
use base 'BookPub::Import::Importer::Chegg::Base';

sub _fieldMap {
    {
        rPurchaseType      => 'E',
        currencyCode       => 'R',
        countryCode        => 'W',
        rState             => 'U',
        rPostalCode        => 'V',
        dateBegin          => 'A',
        rIsbn10            => 'L',
        rIsbn13            => 'K',
        rTitle             => 'G',
        rAuthor            => 'J',
        rReturns           => 'Q',
        rListPrice         => 'P',
        rListPriceCurrency => 'P',
        rTaxAmount         => 'O',
        rRevenue           => 'R',
    };
}

sub _useIsSaleRec { 1 }

sub _processHeader {
    my $self = shift;

    if ( !$self->{_headerCurrencyFound} ) {
        my $field = $self->_getByFieldName('currencyCode');

        if ( $field =~ m/^publisher_fee \(([A-Z]{3})\)$/i ) {
            $self->{_headerCurrency}      = $1;
            $self->{_headerCurrencyFound} = 1;
        } else {
            $self->fail("no currency in field: $field");
        }
    }
    if ( !$self->{_listPriceCurrencyFound} ) {
        my $field = $self->_getByFieldName('rListPriceCurrency');

        if ( $field =~ m/^list price \(([A-Z]{3})\)$/i ) {
            $self->{_listPriceCurrency}      = $1;
            $self->{_listPriceCurrencyFound} = 1;
        } else {
            $self->fail("no list price currency in field: $field");
        }

    }

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

sub currencyCode {
    my $self = shift;

    return $self->{_headerCurrency};
}

sub rListPriceCurrency {
    my $self = shift;

    return $self->{_listPriceCurrency};
}

sub rReturns {
    my $self  = shift;
    my $value = $self->_getByFieldName('rReturns');

    return lc($value) eq 'cancellation' ? 1 : 0;
}

###
1;    # Play nicely.
###
