package BookPub::Import::Importer::Mackin::Version1;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;
use Data::Dumper;

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

sub _headerIdentifier { ( rIsbn13 => 'ISBN' ) }

sub _fieldMap {
    {
        # detail
        rIsbn13       => 'F',
        rTitle        => 'A',
        rUnits        => 'G',
        rUnitPrice    => 'I',
        rListPrice    => 'H',
        rDiscount     => 'K',
        rRevenue      => 'J',
        rPurchaseType => 'E',
        rProductType  => 'A3',
    };
}

sub priceType            { 'rrp' }
sub countryCode          { 'US' }
sub currencyCode         { 'USD' }
sub rListPriceCurrency   { 'USD' }
sub productType          { shift->rProductType }
sub priceTypeQualifierID { BookPub::DB::Item::PriceTypeQualifier::kCorporate }

sub _useIsSaleRec { 1 }

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

sub rProductType  {
    my $self = shift;

    my $rProductType = $self->_getByFieldName('rProductType');
    return BookPub::DB::Item::Sale::kProductTypeEBook     if $rProductType =~ /EBOOKS$/i;
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if $rProductType =~ /DIGITAL AUDIO$/i;
}

sub purchaseType {
    my $self         = shift;
    my $purchaseType = $self->_getByFieldName('rPurchaseType');

    if ( lc($purchaseType) eq 'm' || lc($purchaseType) eq 's' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    }
    if ( lc($purchaseType) eq 'm/s' || lc($purchaseType) eq 's/s' || lc($purchaseType) eq 'c/9' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    }
    if ( lc($purchaseType) eq 'm/p' || lc($purchaseType) eq 's/p' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } elsif ($purchaseType) {
        $self->fail("Unknown purchase type '$purchaseType'");
    }
}

# accept records only with an title and an ISBN, but ignore the header where the
# isbn value is 'ISBN'
sub _isSaleRec {
    my $self = shift;
    return $self->rTitle && $self->rIsbn13 && $self->rIsbn13 ne 'ISBN';
}

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

    $self->SUPER::_preProcess(%args);

    my $sheetnum = 0;
    foreach my $sheet ( @{ $args{sheets} } ) {
        my $sheetname = $args{sheet_names}->[$sheetnum];
        $sheetnum++;

        foreach my $line (@$sheet) {
            $self->{_dateBegin} = $line->[5];
            $self->{_dateEnd}   = $line->[7];

            unless ( $self->{_dateBegin} && $self->{_dateEnd} ) {
                die "Unable to find dates in header";
            }
            last;
        }
    }
}

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

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    return $date;
}

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

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    return $date;
}

sub isFree {
    my $self = shift;

    return (((!$self->rRevenue || $self->rRevenue =~ /^0.0+/) && $self->rUnits) ? 'Y' : 'N');
}

1;
