package BookPub::Import::Importer::InAudio;

use strict;

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

use Date::Calc qw(Decode_Month);

sub _headerIdentifier { ( rRevenue => 'Royalty Earned' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap =
    (
    	1 => {
            dateBegin          => 'A3',
            rIsbn13            => 'C',
            rTitle             => 'A',
            rSales             => 'E',
            rReturns           => 'G',
            rListPrice         => 'D',
            rDiscount          => 'A4',
            rRevenue           => 'I',
        },
        2 => {
            dateBegin          => 'A3',
            rIsbn13            => 'B',
            rTitle             => 'A',
            rSales             => 'D',
            rReturns           => 'F',
            rListPrice         => 'C',
            rDiscount          => 'A4',
            rRevenue           => 'H',
        },
        3 => {
            dateBegin          => 'A3',
            rTitle             => 'A',
            rIsbn13            => 'C',
            rReturns           => 'G',
            rSales             => 'E',
            rListPrice         => 'D',
            rDiscount          => 'B',
            rRevenue           => 'F',
        },
        4 => {
            dateBegin          => 'A3',
            rTitle             => 'A',
            rIsbn13            => 'B',
            rReturns           => 'H',
            rSales             => 'F',
            rListPrice         => 'E',
            rDiscount          => 'D',
            rRevenue           => 'J',
        },
        5 => {
            dateBegin          => 'A3',
            rPurchaseType      => 'B',
            rProductType       => 'G',
            rTitle             => 'A',
            rIsbn13            => 'E',
            rReturns           => 'L',
            rSales             => 'J',
            rListPrice         => 'I',
            rDiscount          => 'C',
            rRevenue           => 'P',
            rPromoCode         => 'H',
            rChannel           => 'F',
        },
        6 => {
            dateBegin          => 'A3',
            rPurchaseType      => 'B',
            rProductType       => 'A2',
            rTitle             => 'A',
            rIsbn13            => 'E',
            serviceProductID   => 'D',
            rReturns           => 'M',
            rSales             => 'K',
            rListPrice         => 'I',
            rDiscount          => 'C',
            rRevenue           => 'O',
            rPromoCode         => 'H',
            rChannel           => 'G',
            priceType          => 'J',
        },
    );

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

sub _useIsSaleRec  { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub currencyCode   { 'USD' }
sub countryCode    { 'US' }

sub priceType    {
    my $self = shift;
    my $clientID = Common::RSApp::GetClientID;
    if ( $clientID == 308 ) { #Hachette
        return 'rrp';
    } elsif ( $clientID == 346 ) { #RLPG
        return 'rrp';
    } elsif ( $clientID == 318 ) { #Macmillan US
        return 'agency';
    } elsif ( $clientID == 359 ) { #MMUK
        return 'rrp';
    } elsif ( $clientID == 335 ) { #BKPub
        return 'rrp';
    }

    $self->fail("Price Type not set for this client");
}

sub productType    {
    my $self = shift;
    my $filename = $self->filename();
    if ($filename =~ /eBook/i) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ($filename =~ /eReader/i) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ($filename =~ /Audio/i) {
        return BookPub::DB::Item::Sale::kProductTypeAudioBook;
    }

    $self->fail("Product type missing from file name");
}

sub formatType {
    my $self = shift;
    if ($self->productType() eq BookPub::DB::Item::Sale::kProductTypeEBook) {
        return BookPub::DB::Item::Sale::kFormatTypeEPub;
    }
}


sub purchaseType    {
    my $self = shift;

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

        if (lc($type) eq 'retail') {
            return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
        } elsif (lc($type) eq 'subscription-credit based') {
            return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
        } elsif (lc($type) eq 'subscription-unlimited') {
            return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
        } elsif (lc($type) eq 'library-pay per use') {
            return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
        } elsif (lc($type) eq 'library') {
            return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
        } 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( 'rPurchaseType' ) ) {
        my $type = $self->_getByFieldName( 'rPurchaseType' );

        if ($type =~ /library/i) {
            return BookPub::DB::Item::PriceTypeQualifier::kCorporate; # Library
        }
    }

    return undef;
}

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

    if( $date && $date =~ /^(\w+) (\d+).* (\d{4})$/i ) {
        my $month = Decode_Month($1);
        my $day = $2;
        my $year  = $3;
        return sprintf("%04d-%02d-%02d", $year, $month, $day);
    } elsif( $date && $date =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})/ ) {
        my $month = $1;
        my $day = $2;
        my $year  = $3;
        return sprintf("%04d-%02d-%02d", $year, $month, $day);
    }
}

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

    if( $date && $date =~ / (\w+) (\d+)\w*, (\d{4})$/i ) {
        my $month = Decode_Month($1);
        my $day = $2;
        my $year  = $3;
        return sprintf("%04d-%02d-%02d", $year, $month, $day);
    } elsif( $date && $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})$/ ) {
        my $month = $1;
        my $day = $2;
        my $year  = $3;
        return sprintf("%04d-%02d-%02d", $year, $month, $day);
    }
}

sub rDiscount {
    my $self = shift;

    my $discount = $self->_getByFieldName( 'rDiscount' );
    if ($discount =~ / (\d+\.?\d*)%/) {
        $discount = 1 - ($1 / 100);
        return $discount;
    }
}

sub _processLine {
    my $self = shift;
    my $listPrice = $self->_getByFieldName( 'rListPrice' );

    if( $listPrice && $listPrice =~ /Grand Total/i ) {
        $self->{_endOfData} = 1;
    }

    return $self->SUPER::_processLine(@_);
}

sub _isSaleRec {
    my $self = shift;

	# If we've set the end of data flag in processLine, we are done.
    return if( $self->{_endOfData} );

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

1;
