package BookPub::Import::Importer::Speechify::v1;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin          => 'O',
        rFormat            => 'AD',
        priceType          => 'AN',
        rAuthor            => 'Y',
        rTitle             => 'X',
        rIsbn13            => 'U',
        rImprint           => 'AC',
        countryCode        => 'AL',
        rReturns           => 'AG',
        rSales             => 'AF',
        rUnits             => 'AH',
        rUnitPrice         => 'AM',
        rListPrice         => 'BQ',
        rListPriceCurrency => 'AO',
        rDiscount          => 'AP',
        rRevenue           => 'BC',
        currencyCode       => 'AO',
    };
}

sub _unverifiedFieldMap {{
        altDateBegin => 'G',
        altDateEnd   => 'H',
        altIsbn13    => 'W',
    }}
sub _headerIdentifier { rTitle => 'Product title' }

sub rServiceName       { 'Speechify' }
sub _useIsSaleRec      { 1 }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rPurchaseType      { $_[0]->purchaseType() }
sub rProductType       { $_[0]->productType() }
sub rPriceType         { $_[0]->priceType() }

sub productType {
    my $self = shift;

    my $type = $self->filename() || "";
    return BookPub::DB::Item::Sale::kProductTypeEBook if $type =~ /ebook/i;
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if $type =~ /audiobook/i;

    $self->fail("Unbale to determine product type from '$type'");
}

sub dateBegin {
    my $self = shift;

    my %monthNames = (
        "jan" => "01",
        "feb" => "02",
        "mar" => "03",
        "apr" => "04",
        "may" => "05",
        "jun" => "06",
        "jul" => "07",
        "aug" => "08",
        "sep" => "09",
        "oct" => "10",
        "nov" => "11",
        "dec" => "12",
    );

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

    if ($date && $date =~ /^(\w{4}).(\w{2}).(\w{2})/) {
        return sprintf( "%04d-%02d-%02d", $1, $2, $3);
    }
    elsif ( $self->filename =~ /macmillan.(\w{3}).(\d{4})\.?/i ) {
        if (exists($monthNames{lc($1)})) {
            my ($y, $m) = ($2, $monthNames{lc($1)});
            return sprintf( "%04d-%02d-%02d", $y, $m, 1);
        }
    }

    $self->fail("Couldn't find date from '$date' or filename: '".$self->filename."'");
}
sub dateEnd {
    my $self = shift;

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

    if ($date && $date =~ /^(\w{4}).(\w{2}).(\w{2})/) {
        return sprintf( "%04d-%02d-%02d", $1, $2, $3);
    }
    else {
        return $self->dateBegin();
    }
}

sub rListPrice {
    my $self = shift;

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

    return ($listPrice ? $listPrice : $unitPrice);
}

sub priceType {
    my $self = shift;

    my $type = $self->_getByFieldName('priceType') || '';
    return 'rrp' if $type eq "43";
    return 'agency' if $type eq "41";

    $self->fail("Unbale to determine price type from '$type'");
}

sub currencyCode {
    my $self = shift;

    my $code = $self->_getByFieldName('currencyCode') || '';
    return $code if $code =~ /^\w{3}$/;

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

sub rIsbn13 {
    my $self = shift;

    my $rIsbn13   = $self->_getByFieldName('rIsbn13') || $self->_getByFieldName('altIsbn13');
    return $rIsbn13 if $rIsbn13;

    $self->fail("ISBN is missing.");
}

sub isFree {
    my $self = shift;

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


1;
