package BookPub::Import::Importer::AdtalemChamberlain::v1;

use strict;
use warnings;

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

sub _fieldMap {
    my $self = shift;

    my %map = (
        1 => {
            rRevenue => 'H',
        },
        2 => {
            rRevenue => 'H',
        },
    );

    return $map{ $self->version };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %map = (
        1 => {
            ebook_rPurchaseType => 'C',
            ebook_purchaseType  => 'C',
            ebook_rProductType  => 'C',
            ebook_rIsbn13       => 'B',
            ebook_rSales        => 'C',
            ebook_rUnits        => 'C',
            ebook_rUnitPrice    => 'D',
            ebook_rListPrice    => 'D',

            pod_rPurchaseType   => 'E',
            pod_purchaseType    => 'E',
            pod_rProductType    => 'E',
            pod_rIsbn13         => 'A',
            pod_rSales          => 'E',
            pod_rUnits          => 'E',
            pod_rUnitPrice      => 'F',
            pod_rListPrice      => 'F',
        },
        2 => {
            ebook_rPurchaseType => 'E',
            ebook_purchaseType  => 'E',
            ebook_rProductType  => 'E',
            ebook_rIsbn13       => 'B',
            ebook_rSales        => 'E',
            ebook_rUnits        => 'E',
            ebook_rUnitPrice    => 'D',
            ebook_rListPrice    => 'D',

            pod_rPurchaseType   => 'G',
            pod_purchaseType    => 'G',
            pod_rProductType    => 'G',
            pod_rIsbn13         => 'A',
            pod_rSales          => 'G',
            pod_rUnits          => 'G',
            pod_rUnitPrice      => 'F',
            pod_rListPrice      => 'F',
        },
    );

    return $map{ $self->version };
}

sub _headerIdentifier { rRevenue => 'PUBLISHER TOTAL' }

sub _useIsSaleRec      { 1 }
sub discount           { 0 }
sub priceType          { 'rrp' }
sub rInstitution       { 'Chamberlain' }
sub countryCode        { 'US' }
sub currencyCode       { 'USD' }
sub rListPriceCurrency { 'USD' }
sub dateBegin          { shift->{_dateBegin} }
sub dateEnd            { shift->{_dateBegin} }
sub productType        { shift->rProductType }
sub purchaseType       { shift->rPurchaseType }

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

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

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

    my ($year, $month, $day);
    if ( $self->filename =~ m/^(\d{4})(\d{2})\s([a-zA-Z]{3})/ ) {
        my %monthes = (
            jan => 1, feb => 2, mar => 3, apr => 4,  may => 5,  jun => 6,
            jul => 7, aug => 8, sep => 9, oct => 10, nov => 11, dec => 12,
        );
        $month = $monthes{ lc($3) } if exists $monthes{ lc($3) };
        ($year, $day) = ($1, $2);
    }

    $self->errstr('Unable to find date in filename') unless ( $year && $month && $day );

    return $self->{_dateBegin} = sprintf "%04d-%02d-%02d", $year, $month, $day;
}

sub _processLine {
    my $self = shift;

    unless ( $self->_isSaleRec ) {
        Common::Log::Print( " -- Ignore line sheet: " . $self->sheetname . " line: " . $self->linenum );
        $self->{_linesIgnored}++;
        return;
    }

    $self->{_current_flag} = '';
    foreach my $flag (qw/ebook pod/) {
        next unless $self->{ '_' . $flag };
        $self->{_current_flag} = $flag;
        $self->_saveLine();
        $self->{_linesImported}++;
    }

}

sub _isSaleRec {
    my $self = shift;

    my @controlFields = qw/rProductType rIsbn13 rUnitPrice/;
    foreach my $flag (qw/ebook pod/) {
        $self->{ '_' . $flag } = 0;
        my $matches = grep { $self->_getByFieldName( $flag . '_' . $_ ) } @controlFields;
        $self->{ '_' . $flag } = $matches if $matches == @controlFields;
    }

    return unless ( $self->{_ebook} || $self->{_pod} );

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

sub rIsbn13 {
    my $self = shift;

    my $podRIsbn13   = $self->_getByFieldName('pod_rIsbn13');
    my $ebookRIsbn13 = $self->_getByFieldName('ebook_rIsbn13');

    if ( $self->rProductType eq 'EBOK' ) {
        return $ebookRIsbn13;
    }
    elsif ( $self->rProductType eq 'PBOK' ) {
        return $podRIsbn13
    }

    return;
}

sub rPurchaseType {
    my $self = shift;

    my $flag = $self->{_current_flag};
    if ( $flag eq 'ebook' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    }
    elsif ( $flag eq 'pod' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypePrintOnDemand;
    }

    return;
}

sub rProductType {
    my $self = shift;

    my $flag = $self->{_current_flag};
    if ( $flag eq 'ebook' ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }
    elsif ( $flag eq 'pod' ) {
        return BookPub::DB::Item::Sale::kProductTypePhysicalBook;
    }

    return;
}

sub rSales {
    my $self = shift;

    return $self->{_current_flag}
        ? $self->_getByFieldName( $self->{_current_flag} . '_rSales' )
        : undef;
}

sub rUnits {
    my $self = shift;

    return $self->{_current_flag}
        ? $self->_getByFieldName( $self->{_current_flag} . '_rUnits' )
        : undef;
}

sub rUnitPrice {
    my $self = shift;

    return $self->{_current_flag}
        ? $self->_getByFieldName( $self->{_current_flag} . '_rUnitPrice' )
        : undef;
}

sub rListPrice {
    my $self = shift;

    return $self->{_current_flag}
        ? $self->_getByFieldName( $self->{_current_flag} . '_rListPrice' )
        : undef;
}

sub rRevenue {
    my $self = shift;

    if ( defined $self->rUnits && defined $self->rUnitPrice ) {
        return $self->rUnits * $self->rUnitPrice;
    }

    return;
}


1;
