package BookPub::Import::Importer::Downpour::Version2;

use strict;
use warnings;

use constant RE_MONTH_YEAR => qr/(0[1-9]|1[0-2])\/(\d{4})/;

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

use Date::Calc qw( Add_Delta_YM Days_in_Month);

sub _fieldMap {
    my $self = shift;

    my %map = (
        # SERVICE 210; VERSION #2
        2 => {
            dateBegin        => 'A',
            dateEnd          => 'A',
            rAuthor          => 'E',
            rTitle           => 'D',
            rIsbn13          => 'F',
            serviceProductID => 'C',
            rUnits           => 'G',
            rListPrice       => 'H',
            rRevenue         => 'I'
        },
        # SERVICE 210; VERSION #3
        3 => {
            dateBegin        => 'A',
            dateEnd          => 'A',
            rAuthor          => 'C',
            rTitle           => 'B',
            rIsbn13          => 'D',
            rUnits           => 'E',
            rListPrice       => 'F',
            rCOGS            => 'J',
            rRevenue         => 'M',
        },
        # SERVICE 210; VERSION #4
        4 => {
            dateBegin        => 'A',
            dateEnd          => 'A',
            rAuthor          => 'C',
            rTitle           => 'B',
            rIsbn13          => 'D',
            rUnits           => 'E',
            rListPrice       => 'F',
            rCOGS            => 'J',
            rRevenue         => 'K',
        },
    );

    return $map{ $self->_version };
}

sub _headerIdentifier { ( rIsbn13 => 'ISBN' ) }
sub _useIsSaleRec { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub currencyCode       { 'USD' }
sub rListPriceCurrency { 'USD' }
sub countryCode        { 'US' }
sub productType        { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType          { 'rrp' }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

my %months = (
   jan => 1,
   feb => 2,
   mar => 3,
   apr => 4,
   may => 5,
   jun => 6,
   jul => 7,
   aug => 8,
   sep => 9,
   oct => 10,
   nov => 11,
   dec => 12,
);

sub dateBegin {
    my $self = shift;

    # used cached version, if it exists
    return $self->{_dateBegin} if $self->{_dateBegin};

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

    if ( my ($month, $year) = $dateBegin =~ RE_MONTH_YEAR ) {
        $self->{_dateBegin} = sprintf "%04d-%02d-%02d", $year, $month, 1;
    }
    elsif ($dateBegin =~ /^(\w{3}) (\d{1,2}), (\d{4})$/) {
        $self->{_dateBegin} = sprintf "%04d-%02d-%02d", $3, $months{lc($1)}, 1;
    }
    return $self->{_dateBegin} if $self->{_dateBegin};

    $self->fail("Unexpected string format, can't determine start date: $dateBegin");
}

sub dateEnd {
    my $self = shift;

    # used cached version, if it exists
    return $self->{_dateEnd} if $self->{_dateEnd};

    my $dateEnd = $self->_getByFieldName('dateEnd');

    if ( my ($month, $year) = $dateEnd =~ RE_MONTH_YEAR ) {
        my $day = Date::Simple::days_in_month( $year, $month );
        $self->{_dateEnd} = sprintf "%04d-%02d-%02d", $year, $month, $day;
    }
    elsif ($dateEnd =~ /^(\w{3}) (\d{1,2}), (\d{4})$/) {
        $self->{_dateEnd} = sprintf "%04d-%02d-%02d", $3, $months{lc($1)}, Days_in_Month($3, $months{lc($1)} );
    }
    return $self->{_dateEnd} if $self->{_dateEnd};

    $self->fail("Unexpected string format, can't determine end date: $dateEnd");
}

sub _isSaleRec {
    my $self = shift;

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

    # We're going to skip the subtotal line
    if ($revenue && !$date) {
        return 0;
    }

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

###
1;    #
###
