package BookPub::Import::Importer::HummingbirdDigital;

use strict;
use warnings;

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

sub _headerIdentifier {
    my $self = shift;

    my $title = $self->_version == 3 ? 'Title' : 'Product Title';

    return rTitle => $title;
}

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin          => 'B',
            dateEnd            => 'B',
            rFormat            => 'AD',
            rPriceType         => 'BR',
            rAuthor            => 'Y',
            rTitle             => 'X',
            rIsbn13            => 'U',
            rImprint           => 'AC',
            countryCode        => 'AL',
            serviceProductID   => 'W',
            rReturns           => 'AG',
            rSales             => 'AF',
            rUnits             => 'AH',
            rUnitPrice         => 'AM',
            rListPrice         => 'BQ',
            rListPriceCurrency => 'AO',
            rDiscount          => 'AP',
            rRevenue           => 'BC',
            currencyCode       => 'I',
            rChannel           => 'N',
        },
        2 => {
            rServiceName       => 'BO',
            dateBegin          => 'O',
            dateEnd            => 'O',
            rProductType       => 'AD',
            rPriceType         => 'ZZ',     # a stub
            rAuthor            => 'Y',
            rTitle             => 'X',
            rIsbn13            => 'U',
            rImprint           => 'AC',
            countryCode        => 'AL',
            serviceProductID   => 'W',
            rReturns           => 'AG',
            rSales             => 'AF',
            rUnits             => 'AH',
            rListPrice         => 'AM',
            rListPriceCurrency => 'AO',
            rDiscount          => 'AP',
            rRevenue           => 'BC',
            currencyCode       => 'H',
            isFree             => 'AM',
            rChannel           => 'N',
        },
        3 => {
            rServiceName       => '1A',
            dateBegin          => 'B',
            dateEnd            => 'B',
            rAuthor            => 'G',
            rTitle             => 'F',
            rIsbn13            => 'E',
            rUnits             => 'H',
            rListPrice         => 'J',
            rListPriceCurrency => 'I',
            rDiscount          => 'K',
            rRevenue           => 'L',
            currencyCode       => 'I',
            rDistributor       => 'D',
        },
        4 => {
            rServiceName       => 'BO',
            dateBegin          => 'O',
            dateEnd            => 'O',
            rProductType       => 'AD',
            rPriceType         => 'ZZ',    # a stub
            rAuthor            => 'Y',
            rTitle             => 'X',
            rIsbn13            => 'U',
            rImprint           => 'AC',
            countryCode        => 'AL',
            serviceProductID   => 'W',
            rReturns           => 'AG',
            rSales             => 'AF',
            rUnits             => 'AH',
            rUnitPrice         => 'AM',
            rListPrice         => 'BQ',
            rListPriceCurrency => 'AO',
            rDiscount          => 'AP',
            rRevenue           => 'BC',
            currencyCode       => 'F',
            isFree             => 'AM',
            rChannel           => 'N',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            secondaryPriceType => 'AN',
            periodBegin        => 'E',
            periodEnd          => 'F',
        },
        2 => {
            secondaryPriceType => 'AN',
            periodBegin        => 'E',
            periodEnd          => 'F',
        },
        3 => {
            periodBegin        => 'B',  # just a fake
            periodEnd          => 'B',  # just a fake
        },
        4 => {
            secondaryPriceType => 'AN',
            periodBegin        => 'G',
            periodEnd          => 'H',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { shift->rProductType }

sub rProductType {
    my $self = shift;

    if ( $self->_version =~ /^(?:2|4)$/ ) {
        my $rProductType  = $self->_getByFieldName('rProductType') || '';
        return BookPub::DB::Item::Sale::kProductTypeEBook     if $rProductType =~ /^E101$/i;
        return BookPub::DB::Item::Sale::kProductTypeAudioBook if $rProductType =~ /^audio$/i;
    }

    return BookPub::DB::Item::Sale::kProductTypeEBook;
}

sub rServiceName {
    my $self = shift;

    if ( $self->_version == 3 ) {
        return 'Hummingbird Digital Media';
    }

    if ( $self->_version =~ /^(?:2|4)$/ ) {
        my $serviceName = $self->_getByFieldName('rServiceName') || '';
        return $serviceName || 'Hummingbird Digital Media';
    }

    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => $self->serviceID );
}

sub dateBegin {
    my $self = shift;

    my $dateStr  = $self->_getByFieldName('dateBegin');
    my $dateStr2 = $self->_getByFieldName('periodBegin');
    my $date;

    # date is in format YYYYMMDD
    if ( $dateStr && $dateStr =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    } elsif ( $dateStr2 && $dateStr2 =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    } else {
        $date = undef;
    }

    return $date;
}

sub dateEnd {
    my $self = shift;

    my $dateStr  = $self->_getByFieldName('dateEnd');
    my $dateStr2 = $self->_getByFieldName('periodEnd');
    my $date;

    # date is in format YYYYMMDD
    if ( $dateStr && $dateStr =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    } elsif ( $dateStr2 && $dateStr2 =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    } else {
        $date = undef;
    }

    return $date;
}

sub priceType {
    my $self = shift;

    return 'rrp' if $self->_version == 3;

    my $type  = $self->_getByFieldName('rPriceType')         || '';
    my $type2 = $self->_getByFieldName('secondaryPriceType') || '';

    if ($type) {
        return $type;
    } elsif ( $type2 && $type2 == 41 || $type2 == 43 ) {
        return 'agency';
    }
}

sub rListPrice {
    my $self = shift;

    my $listPrice = $self->_getByFieldName('rListPrice') || 0;
    my $unitPrice = $self->_getByFieldName('rUnitPrice') || 0;

    if (  $self->_version == 2 ) {
        return $listPrice;
    }

    if ( $self->_version == 4 ) {
        return $unitPrice;
    }

    # if list price is blank, use unit price
    if ( $listPrice ) {
        return $listPrice;
    } elsif ( $unitPrice && $self->_version =~ /^(?:1)/ ) {
        return $unitPrice;
    }

    return;
}

sub rListPriceCurrency {
    my $self = shift;

    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency') || '';

    return uc($listPriceCurrency);
}

sub countryCode {
    my $self = shift;

    return 'US' if $self->_version == 3;

    my $code = $self->_getByFieldName('countryCode') || '';
    return $code;
}

sub currencyCode {
    my $self = shift;

    my $code = $self->_getByFieldName('currencyCode') || '';
    return uc($code);
}

sub isFree {
    my $self = shift;

    if ( $self->_version == 2 ) {
        my $listPrice = $self->rListPrice + 0;
        return $listPrice ? 'N' : 'Y';
    }

    if ( $self->_version == 4 ) {
        my $unitPrice = $self->rUnitPrice + 0;
        return $unitPrice ? 'N' : 'Y';
    }

    return 'N';
}


1;
