package BookPub::Import::Importer::CampusEbookstore::v3;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Date::Calc qw(Days_in_Month);

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

sub _fieldMap {
    my $self = shift;

    my %map = (
        3 => {
            dateBegin          => 'A',
            dateEnd            => 'A',
            rPurchaseType      => 'G',
            rTitle             => 'G',
            rIsbn13            => 'F',
            rInstitution       => 'E',
            countryCode        => 'M',
            serviceProductID   => 'F',
            rUnits             => 'H',
            rUnitPrice         => 'K',
            rListPrice         => 'I',
            rListPriceCurrency => 'M',
            rDiscount          => 'J',
            rRevenue           => 'L',
            currencyCode       => 'M',
        },
        4 => {
            dateBegin          => 'A',
            dateEnd            => 'A',
            rTitle             => 'H',
            rIsbn13            => 'G',
            rInstitution       => 'E',
            countryCode        => 'N',
            serviceProductID   => 'G',
            rUnits             => 'I',
            rUnitPrice         => 'L',
            rListPrice         => 'J',
            rListPriceCurrency => 'N',
            rDiscount          => 'K',
            rRevenue           => 'M',
            currencyCode       => 'N',
        }
    );

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

sub _headerIdentifier { rIsbn13 => 'ISBN' }

sub _useIsSaleRec { 1 }
sub rServiceName  { 'Campus eBookstore' }
sub priceType     { 'rrp' }
sub purchaseType  { $_[0]->rPurchaseType() }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }

sub dateBegin {
    my $self = shift;

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

    return $date if ( $date =~ /^\d{4}-\d{2}-\d{2}$/ );
    # YYYY-MM
    if ( $date =~ /^(\d{4})-(\d{1,2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, 1;
    }
    # YYYY-MM-DD
    if ( $date =~ /^(\d{4})-(\d{1,2})-(\d{1,2})/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }
    # DD.MM.YYYY
    if ( $date =~ /^(\d{2}).(\d{2}).(\d{4})/ ) {
        return sprintf "%04d-%02d-%02d", $3, $2, $1;
    }
    # MM.DD.YYYY
    if ( $date =~ /^(\d{1,2}).(\d{1,2}).(\d{4})/ ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }

    $self->fail("Unable to determine dateBegin from: '$date'");
}

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';

    return $date if ( $date =~ /^\d{4}-\d{2}-\d{2}$/ );

    # YYYY-MM
    if ( $date =~ /^(\d{4})-(\d{1,2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, Days_in_Month( $1, $2 );
    }
    # YYYY-MM-DD
    if ( $date =~ /^(\d{4})-(\d{1,2})-(\d{1,2})/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }
    # DD.MM.YYYY
    if ( $date =~ /^(\d{2}).(\d{2}).(\d{4})/ ) {
        return sprintf "%04d-%02d-%02d", $3, $2, Days_in_Month($3, $2);
    }
    # MM.DD.YYYY
    if ( $date =~ /^(\d{1,2}).(\d{1,2}).(\d{4})/ ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }

    $self->fail("Unable to determine dateEnd from: '$date'");
}

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName('countryCode') || '';
    return 'US' if $code =~ /^USD$/i;
    return 'CA' if $code =~ /^CAD$/i;

    $self->fail("Unbale to determine countryCode from: '$code'");
}

sub rTitle {
    my $self = shift;

    my $title = $self->_getByFieldName('rTitle') || "";
    $title =~ s/^.+?: ?//;

    return $title;
}

sub serviceProductID {
    my $self = shift;

    my $id = $self->_getByFieldName('serviceProductID') || "";
    $id =~ s/^\[|\]$//g;

    return $id;
}

sub isFree {
    my $self = shift;

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

sub rDuration {
    my $self = shift;

    my $title = $self->_getByFieldName("rTitle") || "";
    return $title =~ /\((\d+ day access?)\)/i;
}

sub rPurchaseType {
    my $self = shift;

    if ( $self->_version == 4 ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }

    my $rDuration = $self->_getByFieldName("rTitle") || "";
    return $rDuration =~ /\((\d+ day access?)\)/i
        ? BookPub::DB::Item::Sale::kPurchaseTypeLoan
        : BookPub::DB::Item::Sale::kPurchaseTypeDownload;
}

1;
