package BookPub::Import::Importer::SimplyAudio::Version6;

use strict;
use warnings;

use parent 'BookPub::Import::Importer::SimplyAudio';

sub _fieldMap {
    {
        dateBegin    => 'A',
        rTitle       => 'D',
        rIsbn13      => 'C',
        countryCode  => 'B',
    };
}

sub _unverifiedFieldMap {
    {
        clubDownloads     => 'E',
        alaCarteDownloads => 'I',
        clubListPrice     => 'F',
        alaCarteListPrice => 'J',
        clubDiscount      => 'G',
        alaCarteDiscount  => 'K',
        clubRevenue       => 'H',
        alaCarteRevenue   => 'L',
    };
}

sub _headerIdentifier { rIsbn13 => 'ISBN' }

sub _processHeader { }
sub countryCode    { $_[0]->_getByFieldName('countryCode') || '' }
sub currencyCode   { $_[0]->rListPriceCurrency }
sub priceType      { 'rrp' }
sub rPurchaseType  {
    my $self = shift;

    my $club = $self->_getByFieldName('clubDownloads') || 0;
    my $alacarte = $self->_getByFieldName('alaCarteDownloads') || 0;

    return 'A la Carte Downloads' if $alacarte;
    return 'Club Downloads' if $club;
}

sub purchaseType   {
    my $self = shift;

    my $club = $self->_getByFieldName('clubDownloads') || 0;
    my $alacarte = $self->_getByFieldName('alaCarteDownloads') || 0;

    return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL if $club;
    return BookPub::DB::Item::Sale::kPurchaseTypeDownload if $alacarte;
}

sub rUnits {
    my $self = shift;

    my $club = $self->_getByFieldName('clubDownloads') || 0;
    my $alacarte = $self->_getByFieldName('alaCarteDownloads') || 0;

    return $club + $alacarte;
}

sub rListPriceCurrency {
    my $self = shift;

    my $clientID = Common::RSApp::GetClientID();
    return 'GBP' if $clientID =~ /^(?:359|355)$/;
    return 'USD' if $clientID =~ /^(?:335|318)$/;
}

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

sub _getDateEnd {
    my $self = shift;
    return $self->{_dateEnd};
}

sub _importLines {
    my ($self, %args) = @_;

    my $sheetNumber = 0;
    foreach my $sheet ( @{ $args{sheets} } ) {
        if ( $args{sheet_names}->[$sheetNumber] =~ /Monthly Report/i ) {
            $self->_saveDate($sheet);
        }
        $sheetNumber++;
    }

    $self->fail("Failed to read dates from 'Monthly Report' tab") unless $self->{_dateBegin};

    return $self->SUPER::_importLines(%args);
}

sub _saveDate {
    my $self = shift;
    my $sheet = shift || return;

    foreach my $line (@$sheet) {
        $self->{line} = $line;
        my $date = $self->_getByFieldName('dateBegin');
        if ( $date && $date =~ /^(\d{2})\/(\d{2})\/(?:20)?(\d{2}) - (\d{2})\/(\d{2})\/(?:20)?(\d{2})/ ) {
            $self->{_dateBegin} = sprintf "%04d-%02d-%02d", "20$3", $1, $2;
            $self->{_dateEnd}   = sprintf "%04d-%02d-%02d", "20$6", $4, $5;
            return;
        }
    }
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

sub _isSaleRec {
    my $self = shift;

    return $self->rTitle || $self->rIsbn13;
}


1;
