package BookPub::Import::Importer::Legible::v1;

use strict;
use warnings;

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


sub _fieldMap {
    {
        dateBegin          => 'B',
        dateEnd            => 'C',
        rProductType       => 'L',
        rAuthor            => 'H',
        rTitle             => 'G',
        rIsbn13            => 'F',
        rImprint           => 'J',
        countryCode        => 'D',
        rUnits             => 'P',
        rUnitPrice         => 'V',
        rListPrice         => 'M',
        rListPriceCurrency => 'O',
        rCOGS              => 'U',
        rDiscount          => 'T',
        rRevenue           => 'V',
        currencyCode       => 'O',
    }
}

sub _unverifiedFieldMap {
    {
        "_release_price_usd" => 'M',
        "_release_price_cad" => 'N',
    }
}

sub _headerIdentifier { rIsbn13 => 'ISBN' }

sub rServiceName  { 'Legible' }
sub priceType     { 'agency' }
sub _useIsSaleRec { 1 }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rPurchaseType { 'Downloads' }

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

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    my ($d, $m, $y);

    # dd-Mon-YY
    if ( ($d, $m, $y) = $date =~ /(\d{1,2}).(\w{3}).(\d{2})$/) {
        return sprintf "20%02d-%02d-%02d", $y, $months{lc($m)}, $d;
    }
    # MM/DD/YY
    elsif ( ($m, $d, $y) = $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{2})$/) {
        return sprintf "20%02d-%02d-%02d", $y, $m, $d;
    }
    # DD.MM.YYYY
    elsif ( ($d, $m, $y) = $date =~ /(\d{1,2}).(\d{1,2}).(\d{4})$/) {
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }
    if ( $date =~ /^\d{5}$/) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }
}

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';
    my ($d, $m, $y);

    # dd-Mon-YY
    if ( ($d, $m, $y) = $date =~ /(\d{1,2}).(\w{3}).(\d{2})$/) {
        return sprintf "20%02d-%02d-%02d", $y, $months{lc($m)}, $d;
    }
    # MM/DD/YY
    elsif ( ($m, $d, $y) = $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{2})$/) {
        return sprintf "20%02d-%02d-%02d", $y, $m, $d;
    }
    # DD.MM.YYYY
    elsif ( ($d, $m, $y) = $date =~ /(\d{1,2}).(\d{1,2}).(\d{4})$/) {
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }
    if ( $date =~ /^\d{5}$/) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }
}


sub productType {
    my $self = shift;

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

    $self->fail("Wrong product type: $type") if $type =~ /Audiobook/i;
    return BookPub::DB::Item::Sale::kProductTypeEBook if $type =~ /E-?book/i;

    return $type;
}

sub rListPrice {
    my $self = shift;

    my $currency = $self->_getByFieldName('currencyCode') || "";
    my $price_usd = $self->_getByFieldName('_release_price_usd') || 0;
    my $price_cad = $self->_getByFieldName('_release_price_cad') || 0;

    $self->fail("Wrong currency: $currency") if $currency !~ /^(USD|CAD)$/;

    return $currency eq 'CAD' ? $price_cad : $price_usd;
}

sub rUnitPrice {
    my $self = shift;

    return $self->rRevenue / $self->rUnits;
}

sub discount {
    my $self = shift;

    return if $self->isFree eq 'Y';

    return 1 - ($self->rUnitPrice / $self->rListPrice);
}

sub isFree {
    my $self = shift;

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

1;
