package BookPub::Import::Importer::Booksource;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

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

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            countryCode  => 'E',
            rState       => 'E',
            rIsbn13      => 'D',
            rInstitution => 'F',
            rTitle       => 'B',
            rAuthor      => 'C',
            rUnits       => 'I',
            rListPrice   => 'G',
            rRevenue     => 'J',
        },
        2 => {
            rAuthor              => 'C',
            rTitle               => 'B',
            rIsbn13              => 'D',
            rInstitution         => 'F',
            rState               => 'E',
            countryCode          => 'E',
            rUnits               => 'I',
            rUnitPrice           => 'H',
            rListPrice           => 'G',
            rRevenue             => 'J',
            rChannel             => 'A',
            priceTypeQualifierID => 'F',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            publisher => 'A',
        },
        2 => {
            publisher => 'A',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub rServiceName               { 'Booksource' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType               { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub currencyCode               { 'USD' }
sub rListPriceCurrency         { 'USD' }
sub rDiscount                  { .3 }

sub _dateFormat { [ 'numerical', 'numerical_year_first' ] }

sub priceType {
    my $self = shift;

    return $self->_version == 2 ? 'rrp' : 'agency';
}

sub priceTypeQualifierID {
    my $self = shift;

    if ( $self->_version == 2 ) {
        return BookPub::DB::Item::PriceTypeQualifier::kSchoolLibrary;
    }

    return;
}

# get date from filename
sub _getDateBegin {
    my $self     = shift;
    my $filename = $self->filename();

    if ( $filename && ( $filename =~ /(\d{4})(\d{2})(\d{2})[_ ]to/ ) ) {
        my ( $year, $month, $day ) = ( $1, $2, $3 );
        return sprintf( "%04d-%02d-%02d", $year, $month, $day );
    } else {
        $self->fail( "Couldn't determine date from filename: " . $filename );
    }
}

sub _getDateEnd {
    my $self     = shift;
    my $filename = $self->filename();

    if ( $filename && ( $filename =~ /to[_ ](\d{4})(\d{2})(\d{2})/ ) ) {
        my ( $year, $month, $day ) = ( $1, $2, $3 );
        return sprintf( "%04d-%02d-%02d", $year, $month, $day );
    } else {
        $self->fail( "Couldn't determine date from filename: " . $filename );
    }
}

sub rState {
    my $self  = shift;

    my $state = $self->_getByFieldName('rState');
    if ( $state =~ /, (\w+)$/ ) {
        return $1;
    }
}

sub countryCode {
    my $self    = shift;
    my $country = $self->_getByFieldName('countryCode');
    if ( $country =~ /^(\w{3}) / ) {
        return $1;
    }
    return $country;
}

sub _isSaleRec {
    my $self = shift;

    my $publisher = uc( $self->_getByFieldName('publisher') );

    if ( $publisher eq 'TOTALS' ) {
        return 0;
    }

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

1;
