package BookPub::Import::Importer::Kortext;

use strict;

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

use Date::Calc qw( Add_Delta_YM );
use Spreadsheet::ParseExcel::Utility;


sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            isbn         => 'C',
            rTitle       => 'D',
            rAuthor      => 'E',
            rUnits       => 'H',
            rListPrice   => 'K',
            rUnitPrice   => 'M',
            rDiscount    => 'L',
            rRevenue     => 'N',
            rInstitution => 'A',
            rChannel     => 'I',
            rModel       => 'J',
        },
        2 => {
            dateBegin    => 'A',
            dateEnd      => 'A',
            rFormat      => 'E',
            rAuthor      => 'F',
            rTitle       => 'D',
            rIsbn13      => 'C',
            rInstitution => 'B',
            rUnits       => 'I',
            rUnitPrice   => 'N',
            rListPrice   => 'L',
            rDiscount    => 'M',
            rRevenue     => 'O',
            rChannel     => 'J',
            rModel       => 'K',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub countryCode                { 'GB' }
sub currencyCode               { 'GBP' }
sub rListPriceCurrency         { 'GBP' }
sub priceType                  { 'rrp' }
sub _dateNormalization         { 'month' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType               { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub _headerIdentifier {
    return $_[0]->_version == 1
        ? (isbn    => 'ISBN')
        : (rIsbn13 => 'ISBN');
}

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

sub _getDateBegin {
    my $self     = shift;

    my %shortMonths = (
        jan => 1,
        feb => 2,
        mar => 3,
        apr => 4,
        may => 5,
        jun => 6,
        jul => 7,
        aug => 8,
        sep => 9,
        oct => 10,
        nov => 11,
        dec => 12,
    );
    my %months = (
        january   => 1,
        february  => 2,
        march     => 3,
        april     => 4,
        may       => 5,
        june      => 6,
        july      => 7,
        august    => 8,
        september => 9,
        october   => 10,
        november  => 11,
        december  => 12,
    );

    my ($date, $month, $year);
    $date = $self->_version == 1
        ? $self->filename()
        : $self->_getByFieldName('dateBegin');

    if ( $date =~ m/\b(\w{3}) (\d{2})\b/ ) {
        $month = $shortMonths{ lc($1) };
        $year  = "20" . $2;
    }
    elsif ( $date =~ m/\b(\w+) (\d{4})\b/ ) {
        $month = $months{ lc($1) };
        $year  = $2;
    }
    elsif ( $date =~ /^\d+$/ ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return Common::Util::normalize_date($date);
    }
    elsif ( $date =~ /^\d{4}-\d{2}-\d{2}$/ ) {    # 2019-07-01
        return $date;
    }
    else {
        $self->fail("Unrecognized date in filename: '$date'");
    }

    return "$year-$month-1";
}

sub _getDateEnd { shift->_getDateBegin() }

sub _isSaleRec {
    my $self = shift;

    my $units     = $self->rUnits();
    my $unitPrice = $self->_getByFieldName('rUnitPrice');
    my $isbn      = $self->_getByFieldName('isbn') || $self->_getByFieldName('rIsbn13');
    my $title     = $self->_getByFieldName('rTitle');

    # We're going to skip the subtotal lines, which are blank for these fields
    return unless ( ($units || $unitPrice) && $isbn && $title );

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

###
1;    #
###
