package BookPub::Import::Importer::PanAustralia;

use strict;
use Date::Calc qw(Days_in_Month);

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

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

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

    my %fieldMap = (
        1 => {
            priceType          => 'V',
            currencyCode       => 'S',
            countryCode        => 'K',
            dateBegin          => 'O',
            rIsbn10            => 'B',
            rIsbn13            => 'C',
            rTitle             => 'W',
            rImprint           => 'X',
            rUnits             => 'E',
            rListPrice         => 'P',
            rListPriceCurrency => 'Q',
            rPurchasePrice     => 'R',
            rDiscount          => 'T',
            rRevenue           => 'I',
        },
        2 => {
            purchaseType         => 'W',
            rPurchaseType        => 'W',
            priceType            => 'V',
            currencyCode         => 'S',
            countryCode          => 'K',
            dateBegin            => 'O',
            rProductType         => 'X',
            rIsbn10              => 'B',
            rIsbn13              => 'C',
            rTitle               => 'Y',
            rImprint             => 'Z',
            rUnits               => 'E',
            rListPrice           => 'P',
            rListPriceCurrency   => 'Q',
            rPurchasePrice       => 'R',
            rDiscount            => 'T',
            rRevenue             => 'I',
            rTransactionCategory => 'H',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub rServiceName { 'Pan Australia' }

sub productType {
    my $self = shift;

    if ( defined $self->_getByFieldName('rProductType') ) {
        my $type = $self->_getByFieldName('rProductType');

        if ( lc($type) eq 'physical book' ) {
            return BookPub::DB::Item::Sale::kProductTypePhysicalBook;
        } elsif ( lc($type) eq 'ebook' ) {
            return BookPub::DB::Item::Sale::kProductTypeEBook;
        } elsif ( lc($type) eq 'ebook bundle' ) {
            return BookPub::DB::Item::Sale::kProductTypeMultiEBookBundle;
        } elsif ( lc($type) eq 'audio book' || lc($type) eq 'audb' ) {
            return BookPub::DB::Item::Sale::kProductTypeAudioBook;
        } elsif ( lc($type) eq 'echapter' ) {
            return BookPub::DB::Item::Sale::kProductTypeEChapter;
        } elsif ( lc($type) eq 'audio chapter' ) {
            return BookPub::DB::Item::Sale::kProductTypeAudioChapter;
        } else {
            $self->fail("Could not determine product type from $type");
        }

    } else {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }
}

sub purchaseType {
    my $self = shift;

    if ( defined $self->_getByFieldName('purchaseType') ) {
        my $type = $self->_getByFieldName('purchaseType');

        if ( lc($type) eq 'physical retail' ) {
            return BookPub::DB::Item::Sale::kPurchaseTypePhysicalRetail;
        } elsif ( lc($type) eq 'download' ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
        } elsif ( lc($type) eq 'subscription' ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
        } elsif ( lc($type) eq 'stream' ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeStream;
        } elsif ( lc($type) eq 'pod' ) {
            return BookPub::DB::Item::Sale::kPurchaseTypePrintOnDemand;
        } elsif ( lc($type) eq 'in app purchase' ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeInAppPurchase;
        } elsif ( lc($type) eq 'loan' ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
        } elsif ( lc($type) eq 'library' ) {

            #return BookPub::DB::Item::Sale::kPurchaseTypeLibrarySale;
            return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
        } elsif ( lc($type) eq 'promotional' ) {
            return BookPub::DB::Item::Sale::kPurchaseTypePromotional;
        } else {
            $self->fail("Could not determine purchase type from $type");
        }

    } else {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }
}

sub priceTypeQualifierID {
    my $self = shift;

    if ( defined $self->_getByFieldName('purchaseType') ) {
        my $type = $self->_getByFieldName('purchaseType');

        if ( lc($type) eq 'library' ) {
            return BookPub::DB::Item::PriceTypeQualifier::kCorporate;
        }
    }

    return undef;
}

sub priceType {
    my $self = shift;

    my $type = $self->_getByFieldName('priceType');

    # Need to change some of the names to the ones we use internally.
    if ( lc($type) eq 'retail' ) {
        $type = 'rrp';
    } elsif ( $type =~ /(?:rrp|retail) plus tax/i ) {
        $type = 'rrpplustax';
    } elsif ( lc($type) eq 'agency plus tax' ) {
        $type = 'agencyplustax';
    }

    return $type;
}

sub _getDateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin');

    # Need to normalize this range to the start and end dates of the first month
    if ( $date =~ /^(\d{2})(\d{2})$/ ) {
        my $year  = $1;
        my $month = $2;
        $date = "20" . $year . "-" . $month . "-" . "01";
    }

    return $date;
}

sub _getDateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin');

    # Need to normalize this range to the start and end dates of the first month
    if ( $date =~ /^(\d{2})(\d{2})$/ ) {
        my $year  = $1;
        my $month = $2;
        $date = "20" . $year . "-" . $month . "-" . Days_in_Month( $year, $month );
    }

    return $date;
}

sub _isSaleRec {
    my $self = shift;

    my $country = $self->_getByFieldName('countryCode');
    my $isbn10  = $self->_getByFieldName('rIsbn10');
    my $isbn13  = $self->_getByFieldName('rIsbn13');
    my $title   = $self->_getByFieldName('rTitle');
    my $revenue = $self->_getByFieldName('rRevenue');

    # We're going to skip the subtotal line, which is blank for these fields
    if ( !$country && !$isbn10 && !$isbn13 && !$title && !$revenue ) {
        return 0;
    }

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

1;
