package BookPub::Import::Importer::Kobo::Version13;

use strict;

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

sub _headerIdentifier { ( rTitle => 'Title' ) }

# 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 = {
        'partners' => {
            currencyCode => 'J',
            countryCode  => 'B',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'D',
            rAuthor      => 'E',
            rUnits       => 'C',
            rListPrice   => 'G',
            rCOGS        => 'H',
            rRevenue     => 'I',
        },
        'kobo' => {
            currencyCode => 'J',
            countryCode  => 'B',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'D',
            rAuthor      => 'E',
            rUnits       => 'C',
            rListPrice   => 'G',
            rCOGS        => 'H',
            rRevenue     => 'I',
        },
        'kobo ebook sales' => {
            currencyCode => 'J',
            countryCode  => 'B',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'D',
            rAuthor      => 'E',
            rUnits       => 'C',
            rListPrice   => 'G',
            rCOGS        => 'H',
            rRevenue     => 'I',
        },
        'bundle sales' => {
            currencyCode => 'I',
            isbn         => 'J',
            rTitle       => 'C',
            rAuthor      => 'D',
            rUnits       => 'B',
            rListPrice   => 'E',
            rCOGS        => 'F',
            rRevenue     => 'H',
        },
        'promotions' => {
            currencyCode => 'J',
            countryCode  => 'B',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'D',
            rAuthor      => 'E',
            rUnits       => 'C',
            rListPrice   => 'G',
            rCOGS        => 'H',
            rRevenue     => 'I',
        },
        'refunds' => {
            currencyCode => 'H',
            countryCode  => 'B',
            dateBegin    => 'M',
            isbn         => 'E',
            rTitle       => 'F',
            rAuthor      => 'G',
            rListPrice   => 'I',
            rCOGS        => 'J',
            rRevenue     => 'K',
        },
        'gifts' => {
            currencyCode => 'J',
            countryCode  => 'B',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'D',
            rAuthor      => 'E',
            rReturns     => 'C',
            rListPrice   => 'G',
            rCOGS        => 'H',
            rRevenue     => 'I',
        },
    };

    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    { 'rrp' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

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

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');
    $date =~ s/\.\d+//;

    # "Bundle Sales" sheets doesn't have dates in the data rows, so we try to pull
    # it from the filename.
    if ( !defined($date) && lc( $self->sheetname ) eq 'bundle sales' ) {
        if ( $self->filename =~ /(\w{3})_(\d{4})\./ ) {
            return lc("$1-$2");
        } else {
            $self->fail( "Unable to determine date from filename: " . $self->filename );
        }
    }

    return $date;
}

sub units {
    my $self = shift;

    if ( lc( $self->sheetname ) eq 'refunds' ) {
        return -1;
    }

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

sub _dateNormalization {
    my $self = shift;
    if ( lc( $self->sheetname ) eq 'bundle sales' ) {
        return 'month';
    }

    return;
}

sub countryCode {
    my $self = shift;

    if ( lc( $self->sheetname ) eq 'bundle sales' ) {
        return 'CA';
    }

    return $self->SUPER::countryCode;
}

sub isFree {
    if ( lc( shift->sheetname ) eq 'promotions' ) {
        return 'Y';
    }

    return;
}

sub rSales {
    if ( shift->rReturns ) {
        return 0;
    }

    return;
}

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;
