package BookPub::Import::Importer::Sony::Version11;

use strict;

use lib '/app/tools/common/lib/';
use Common::Country;

use lib '/app/tools/sale_import/lib/';
use Import::ValidationError;

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

use constant { LABEL_COLUMN => 0, DATA_COLUMN => 2 };

sub _headerIdentifier { ( rImprint => 'Imprint' ) }

sub _fieldMap {
    {
        # Detail
        countryCode        => 'I',
        rIsbn13            => 'B',
        rTitle             => 'D',
        rAuthor            => 'E',
        rImprint           => 'C',
        rUnits             => 'H',
        currencyCode       => 'L',
        rListPrice         => 'J',
        rListPriceCurrency => 'L',
        rUnitPrice         => 'Q',
        rRevenue           => 'S',
    };
}

sub _unverifiedFieldMap {
    { productIdType => 'A', };
}

sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType     { 'rrp' }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub dateBegin     { shift->{_dateBegin} }
sub dateEnd       { shift->{_dateEnd} }
sub _useIsSaleRec { 1 }

sub _isSaleRec {
    my $self = shift;
    return $self->_isValidSheet && $self->rTitle && $self->dateBegin;
}

sub _isValidSheet {
    my $self = shift;

    # Log->info("Sheet: ".$self->sheetnum." Ingored: ".join ', ', $self->_ignoreSheetIndex);
    foreach my $check_sheetnum ( $self->_ignoreSheetIndex ) {
        return 0 if ( ( $self->sheetnum - 1 ) == $check_sheetnum );
    }
    return 1;
}

sub _preProcess {
    my $self = shift;
    my %args = @_;

    Log->info("Starting _preProcess");
    $self->SUPER::_preProcess(%args);

    my @sheets          = @{ $args{sheets} };
    my @sheet_names     = @{ $args{sheet_names} };
    my @sheets_enabled  = ();
    my @sheets_disabled = ();

    # For case 17742: http://tools.royaltyshare.com/fogbugz/default.php?17742
    # If there's more than one sheet, only use the sheet with "Overall" in it.
    # If there is only one, just use that sheet.  Populate _ignoreSheetIndex
    # with all of the other sheets via @sheets_disabled
    if ( scalar(@sheets) == 1 ) {
        @sheets_enabled  = (0);
        @sheets_disabled = ();
    } else {
        foreach my $idx ( 0 .. $#sheets ) {
            if ( $sheet_names[$idx] =~ m/ OVERALL$/ ) {
                push @sheets_enabled, $idx;
            } else {
                push @sheets_disabled, $idx;
            }
        }
    }
    $self->{_ignoreSheetIndex} = \@sheets_disabled;

    if ( scalar(@sheets_enabled) != 1 ) {
        $self->fail( "Unexpected multiple sheet with parseable contents:  " . join( ", ", map { "'$sheet_names[$_]'" } @sheets_enabled ) );
    }

    my $enabled_sheet_idx = $sheets_enabled[0];
    my @sheet             = @{ $sheets[$enabled_sheet_idx] };
    foreach my $line (@sheet) {
        next unless ( $line->[LABEL_COLUMN] =~ m/Report period/i );
        $line->[DATA_COLUMN] =~ m/From (\d{8}) to (\d{8})/;
        $self->{_dateBegin} = Common::Util::normalize_date($1);
        $self->{_dateEnd}   = Common::Util::normalize_date($2);
        last;
    }

    unless ( $self->{_dateBegin} && $self->{_dateEnd} ) {
        $self->fail("Unable to determine begin [$self->{_dateBegin}] and/or end [$self->{_dateEnd}] date");
    }

}

sub _ignoreSheetIndex { @{ shift->{_ignoreSheetIndex} } }

1;
