package BookPub::Import::Importer::Kobo::Version18;

use strict;

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

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

# We have to do some odd things with the field map here because this importer has
# different column definitions of seperate tabs.  The map is defined by tab name below.
# The summary tab is ignored (returns and empty field set).
sub _fieldMap {
    my $self = shift;

    my $map = {
        'us taxable' => {
            currencyCode => 'L',
            countryCode  => 'B',
            rState       => 'C',
            rPostalCode  => 'D',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'H',
            rAuthor      => 'G',
            rUnits       => 'E',
            rListPrice   => 'I',
            rCOGS        => 'J',
            rRevenue     => 'K',
        },
        'us non-taxable' => {
            currencyCode => 'L',
            countryCode  => 'B',
            rState       => 'C',
            rPostalCode  => 'D',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'H',
            rAuthor      => 'G',
            rUnits       => 'E',
            rListPrice   => 'I',
            rCOGS        => 'J',
            rRevenue     => 'K',
        },
        'world' => {
            currencyCode => 'L',
            countryCode  => 'B',
            rState       => 'C',
            rPostalCode  => 'D',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'H',
            rAuthor      => 'G',
            rUnits       => 'E',
            rListPrice   => 'I',
            rCOGS        => 'J',
            rRevenue     => 'K',
        },
        'canada' => {
            currencyCode => 'L',
            countryCode  => 'B',
            rState       => 'C',
            rPostalCode  => 'D',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'H',
            rAuthor      => 'G',
            rUnits       => 'E',
            rListPrice   => 'I',
            rCOGS        => 'J',
            rRevenue     => 'K',
        },
        'refunds' => {
            currencyCode => 'L',
            countryCode  => 'B',
            rState       => 'C',
            rPostalCode  => 'D',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'H',
            rAuthor      => 'G',
            rUnits       => 'E',
            rListPrice   => 'I',
            rCOGS        => 'J',
            rRevenue     => 'K',
        },
        'bundles' => {
            currencyCode => 'K',
            countryCode  => 'B',
            isbn         => 'D',
            rTitle       => 'F',
            rAuthor      => 'E',
            rUnits       => 'C',
            rListPrice   => 'G',
            rCOGS        => 'H',
            rRevenue     => 'J',
        },

    };

    my $sheet = lc( $self->sheetname ) || return {};
    return {} if ( $sheet =~ /(invoice|summary)/i );

    die "Unrecognized tab name '" . $self->sheetname . "'" unless ( $map->{$sheet} );

    return $map->{$sheet};
}

sub priceType    { 'agency' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _dateFormat { [ 'iso', 'excel', 'text', 'eur' ] }

sub _processHeader {
    my $self = shift;

    unless ( $self->{_dateBegin} ) {
        unless ( $self->{_dateBegin} = $self->_parseDate() ) {
            my $date = $self->line->[1];
            $self->{_dateBegin} = $self->_parseDateFromString($date);
        }
    }
}

sub _parseDate {
    my $self     = shift;
    my $filename = $self->filename;

    return unless ( $filename =~ /(\w{3})\s(\d{4})\./ );

    return $1 && $1 ? "$1 $2" : undef;
}

sub _parseDateFromString {
    my $self = shift;
    my $string = shift || return;

    $string =~ s/Sales summary from //;
    Log->info("Parse Date String: $string");

    my ( $dateStart, $dateEnd ) = split( /\s*-\s/, $string );
    return unless ( $dateStart && $dateEnd );

    if ( $dateEnd =~ /,/ ) {
        $self->{_dateEnd} = $dateEnd,;
    } else {
        return;
    }

    if ( $dateStart =~ /,/ ) {
        $self->{_dateBegin} = $dateStart;
    } else {
        my ( undef, $year ) = split( /, /, $dateEnd );
        return unless ($year);
        $self->{_dateBegin} = $dateStart . ", $year";
    }
}

sub _dateNormalization {
    my $self = shift;
    if ( $self->sheetname =~ /bundle/i ) {
        return $self->{_dateEnd} ? undef : 'month';
    }

    return;
}

sub _getDateBegin {
    my $self = shift;
    my $date = $self->SUPER::_getDateBegin();

    if ( !$date && $self->sheetname =~ /bundle/i ) {
        $self->fail("unable to determine date for bundles") unless ( $self->{_dateBegin} );
        return $self->{_dateBegin};
    }

    return $date;
}

sub _getDateEnd {
    my $self = shift;

    if ( $self->sheetname =~ /bundle/i ) {
        return $self->{_dateEnd};
    }

    return;
}

sub units {
    my $self = shift;

    if ( lc( $self->sheetname ) =~ /(refund|gifts)/i ) {
        return $self->SUPER::units() * -1;
    }

    my $units = $self->SUPER::units();

    return $units ? $units : 1;
}

sub revenue {
    my $self    = shift;
    my $units   = $self->units();
    my $revenue = $self->SUPER::revenue();

    if ( $units && $units < 0 && $revenue && $revenue > 0 ) {
        return $revenue * -1;
    }

    return $revenue;
}
1;
