package BookPub::Import::Importer::ShawDigital;

use strict;

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

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

use Date::Calc qw( Add_Delta_YM );
use Data::Dumper;

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            currencyCode       => 'M',
            dateBegin          => 'A2',
            dateEnd            => 'A2',
            countryCode        => 'A',
            rIsbn13            => 'B',
            rTitle             => 'C',
            rAuthor            => 'D',
            rImprint           => 'F',
            rUnits             => 'J',
            rSales             => 'H',
            rReturns           => 'I',
            rListPrice         => 'G',
            rListPriceCurrency => 'G',
            rDiscount          => 'K',
            rRevenue           => 'M',
        },
        2 => {
            dateBegin    => 'A2',
            dateEnd      => 'A2',
            rAuthor      => 'D',
            rTitle       => 'C',
            rIsbn13      => 'B',
            rImprint     => 'F',
            countryCode  => 'A',
            rReturns     => 'L',
            rSales       => 'K',
            rUnits       => 'M',
            rDiscount    => 'N',
            rRevenue     => 'P',
            currencyCode => 'P',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            subTotals => 'l',
        },
        2 => {
            subTotals    => 'O',
            listPriceGBP => 'G',
            listPriceUSD => 'H',
            listPriceAUD => 'I',
            listPriceINR => 'J',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub priceType                  { 'rrp' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

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

    if ( $date && $date =~ /(\d{2})\/(\d{2})\/(\d{4}) to \d{2}\/\d{2}\/\d{4}/ ) {
        return sprintf( "%04d-%02d-%02d", $3, $2, $1 );
    }
}

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

    if ( $date && $date =~ /\d{2}\/\d{2}\/\d{4} to (\d{2})\/(\d{2})\/(\d{4})/ ) {
        return sprintf( "%04d-%02d-%02d", $3, $2, $1 );
    }
}

sub _processHeader {
    my $self = shift;
    my $currency;
    my $header = $self->_getByFieldName('currencyCode');

    if ( $header && $header =~ m/^Amount Due ([A-Z]{3})$/ ) {
        $currency = "$1";
    } else {
        return;
    }

    if ( Common::CurrencyFormat::CodeIsValid($currency) ) {
        $self->{_currencyCode} = $currency;
    } else {
        $self->fail( "Couldn't fine a valid currencyCode in header column M " . $header );
    }
}

sub currencyCode {
    my $self = shift;

    if ( $self->{_currencyCode} ) {
        return $self->{_currencyCode};
    }
}

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

    # field has currency code in it, like: GBP 4.43, so we strip it out
    if ( $listPrice && $listPrice =~ m/^([A-Z]{3}) (\w.*)$/ ) {
        return $2;
    }
}

sub rListPriceCurrency {
    my $self              = shift;
    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');

    # field has currency code in it, like: GBP 4.43, so we strip it out
    if ( $listPriceCurrency && $listPriceCurrency =~ m/^([A-Z]{3}) (\w.*)$/ ) {
        return $1;
    }
}

sub rRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('rRevenue');

    # field has currency code in it, like: GBP 4.43, so we strip it out
    if ( $revenue && $revenue =~ m/^([A-Z]{3}) (\w.*)$/ ) {
        return $2;
    }
}

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

    if ( $sub && $sub =~ m/Sub-Totals/i ) {
        $self->{_endOfData} = 1;
        return;
    }

    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;
