package BookPub::Import::Importer::DirectEbooks;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( rListPrice => 'List Price' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            countryCode    => 'F',
            rState         => 'E',
            rPostalCode    => 'G',
            dateBegin      => 'I',
            rFormat        => 'R',
            rIsbn13        => 'Z',
            rTitle         => 'W',
            rAuthor        => 'X',
            rListPrice     => 'M',
            rPurchasePrice => 'O',
            rDiscount      => 'N',
            rRevenue       => 'O',

        },
        2 => {
            dateBegin      => 'A',
            rProductType   => 'E',
            rAuthor        => 'X',
            rTitle         => 'D',
            rIsbn13        => 'C',
            countryCode    => 'L',
            rListPrice     => 'F',
            rPurchasePrice => 'K',
            rDiscount      => 'H',
            rRevenue       => 'K',
            rTaxUnitPrice  => 'J',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub priceType                  { 'agency' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub currencyCode               { 'AUD' }
sub rListPriceCurrency         { 'AUD' }
sub rPurchasePriceCurrency     { 'AUD' }
sub rServiceName               { 'Direct Ebooks' }

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin') || "";

    # Example: 26-Nov-2013 12:28:39

    my %textMonthVals = (
        "jan" => 1,
        "feb" => 2,
        "mar" => 3,
        "apr" => 4,
        "may" => 5,
        "jun" => 6,
        "jul" => 7,
        "aug" => 8,
        "sep" => 9,
        "oct" => 10,
        "nov" => 11,
        "dec" => 12
    );

    if ( $date =~ /^(\d{1,2})-(\w{3})-(\d{4})/ ) {
        my $day   = $1;
        my $month = $textMonthVals{ lc($2) };
        my $year  = $3;
        if ($month) {
            return sprintf( "%04d-%02d-%02d", $year, $month, $day );
        }
    }
    elsif ( $date =~ /^(\d{2})\/(\d{2})\/(\d{4})/ ) {
        return sprintf( "%04d-%02d-%02d", $3, $1, $2 );
    }
    elsif ( $date =~ /^(\d{4})-(\d{2})-(\d{2})$/) {
        return sprintf( "%04d-%02d-%02d", $1, $3, $2 );
    }
    else {
        return $date;
    }
}

sub units {
    my $self    = shift;
    my $revenue = $self->rRevenue();
    my $units   = 1;

    if ( $revenue < 0 ) {
        $units *= -1;
    }

    return $units;
}
sub rUnits { shift->units() }

sub isFree {
    my $self    = shift;
    my $revenue = $self->revenue();

    if ( $revenue == 0 ) {
        return 'Y';
    } else {
        return 'N';
    }
}

sub productType {
    my $self = shift;

    if ( defined( $self->_getByFieldName('rProductType') ) ) {
        my $type = $self->_getByFieldName('rProductType') || "";;

        return BookPub::DB::Item::Sale::kProductTypeAudioBook if ($type =~ /^Audiobook$/i);

        my $typeCode = $self->_parseProductType($type);
        $typeCode ? return $typeCode : $self->fail("Unrecognized product type '$type'");
    } else {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }
}

sub rListPrice {
    my $self = shift;

    my $list_price = $self->_getByFieldName('rListPrice') || "";
    $list_price =~ s/\$//;

    return $list_price;
}

sub rPurchasePrice {
    my $self = shift;

    my $purchase_price = $self->_getByFieldName('rPurchasePrice') || "";
    $purchase_price =~ s/\$//;

    return $purchase_price;
}

sub rDiscount {
    my $self = shift;

    my $discount = $self->_getByFieldName('rDiscount') || "";
    $discount =~ s/\$//;

    return $discount;
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || "";
    $revenue =~ s/\$//;

    return $revenue;
}

sub rTaxUnitPrice {
    my $self = shift;

    my $tax_unit_price = $self->_getByFieldName('rTaxUnitPrice') || "";
    $tax_unit_price =~ s/\$//;

    return $tax_unit_price;
}

sub _isSaleRec {
    my $self = shift;

    return unless (
           $self->rTitle
        && $self->rIsbn13
    );

    return $self->BookPub::Import::Importer::_isSaleRec();
}

1;
