package BookPub::Import::Importer::Sony::Version7;

use strict;

use lib '/app/tools/common/lib';
use Common::Date;
use Common::Log;

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

sub _fieldMap {
    {
        # Data
        currencyCode => 'H',
        countryCode  => 'G',
        rIsbn10      => 'C',
        rIsbn13      => 'D',
        rTitle       => 'E',
        rAuthor      => 'F',
        rImprint     => 'B',
        rUnits       => 'K',
        rListPrice   => 'I',
        rDiscount    => 'J',
        rRevenue     => 'L',
    };
}

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

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

sub _dateNormalization {
    my $self = shift;
    my $date = $self->_getDateBegin() || return;

    return $date =~ /Q/ ? 'quarter' : 'month';
}

sub _dateFormat { [ 'numerical', 'quarter', 'iso' ] }

sub _getDateBegin {
    my $self = shift;
    return $self->{_dateBegin} if ( $self->{_dateBegin} );

    my $sheetname = $self->sheetname;

    if ( $sheetname && $sheetname =~ /(Q[1234])-(\d{2})/ ) {
        $self->{_dateBegin} = "$1-20$2" if ( $1 && $2 );
    } elsif ( $sheetname && $sheetname =~ /M-(\d{1,2}-\d{4})/ ) {
        $self->{_dateBegin} = $1;
    } elsif ( $sheetname && $sheetname =~ /Q-0([1234])-(\d{4})/ ) {
        $self->{_dateBegin} = "Q$1-$2";
    } elsif ( $self->filename =~ /M-(\d{1,2}-\d{4})/ ) {
        $self->{_dateBegin} = $1;
    }

    return $self->{_dateBegin};
}

sub _getDateEnd { return shift->_getDateBegin(); }

sub _isValidSaleRecord {
    my $self = shift;
    return if ( $self->rDiscount eq 'Total' );

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

1;
