package BookPub::Import::Importer::Snapplify::v1;

use strict;
use Data::Dumper;

use lib '/app/tools/common/lib/';

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

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin          => 'B',
            priceType          => 'F4',
            rTitle             => 'B',
            rIsbn13            => 'A',
            countryCode        => 'D',
            rReturns           => 'C',
            rSales             => 'C',
            rListPrice         => 'F',
            rListPriceCurrency => 'E',
            rDiscount          => 'H',
            rRevenue           => 'I',
            currencyCode       => 'E',
        },
    );

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

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

sub _useIsSaleRec { 1 }

my %monthNames = (
    "january"   => "01",
    "jan"       => "01",
    "february"  => "02",
    "feb"       => "02",
    "march"     => "03",
    "mar"       => "03",
    "april"     => "04",
    "apr"       => "04",
    "may"       => "05",
    "june"      => "06",
    "jun"       => "06",
    "july"      => "07",
    "jul"       => "07",
    "august"    => "08",
    "aug"       => "08",
    "september" => "09",
    "sep"       => "09",
    "october"   => "10",
    "oct"       => "10",
    "november"  => "11",
    "nov"       => "11",
    "december"  => "12",
    "dec"       => "12",
);

sub _preProcess {
    my $self        = shift;
    my %args        = @_;
    my $sheet_names = $args{sheet_names};
    my $sheet_count = 0;
    my ( $beginMonth, $endMonth );

    $self->SUPER::_preProcess(%args);

    foreach my $sheet ( @{ $args{sheets} } ) {
        if ( $sheet_names->[$sheet_count] =~ /Summary/i ) {
            foreach my $line (@$sheet) {
                $self->{line} = $line;

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

                # Date cell format: 01 April 2016 - 30 April 2016
                if ( $range =~ /(\d{2}) (\w{3,9}) (\d{4}) - (\d{2}) (\w{3,9}) (\d{4})/i ) {
                    my $beginMonthName = lc($2);
                    my $endMonthName   = lc($5);

                    if ( $monthNames{$beginMonthName} ) {
                        $beginMonth = $monthNames{$beginMonthName};
                    } else {
                        $self->fail("Bad begin month value: $beginMonthName");
                    }

                    if ( $monthNames{$endMonthName} ) {
                        $endMonth = $monthNames{$endMonthName};
                    } else {
                        $self->fail("Bad end month value: $endMonthName");
                    }

                    $self->{_dateBegin} = sprintf( "%04d-%02d-%02d", $3, $beginMonth, $1 );
                    $self->{_dateEnd}   = sprintf( "%04d-%02d-%02d", $6, $endMonth,   $4 );

                    return;
                }
            }
        }
        $sheet_count++;
    }
}

sub dateBegin {
    my $self = shift;

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

sub dateEnd {
    my $self = shift;

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

sub rSales {
    my $self  = shift;
    my $sales = $self->_getByFieldName('rSales');

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

sub rReturns {
    my $self    = shift;
    my $returns = $self->_getByFieldName('rReturns');

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

sub _isSaleRec {
    my $self = shift;

    my $title             = $self->_getByFieldName('rTitle');
    my $isbn              = $self->_getByFieldName('rIsbn13');
    my $country           = $self->_getByFieldName('countryCode');
    my $sales             = $self->_getByFieldName('rSales');
    my $listPrice         = $self->_getByFieldName('rListPrice');
    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');

    # We're going to skip the subtotal lines, which are blank for these fields
    if ( !$title && !$isbn && !$country && !$sales && !$listPrice && !$listPriceCurrency ) {
        return 0;
    }

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

1;
