package BookPub::Import::Importer::RallyReader::v1;

use strict;
use warnings;

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

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

sub _unverifiedFieldMap {
    {
        periodBegin => 'E',
        periodEnd   => 'F',
    };
}

sub _headerIdentifier { rTitle => 'Product title' }

sub priceType                  { shift->rPriceType }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub _autoParseTitleAndSubtitle { 1 }
sub _useIsSaleRec              { 1 }

sub rServiceName { 'Rally Reader' }

sub _getDateBegin {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin') || '';
    my $periodBegin = $self->_getByFieldName('periodBegin') || '';

    foreach ( $dateBegin, $periodBegin ) {
        # YYYY-MM-DD
        if ( $_ =~ /^(\d{4})[-\/](\d{1,2})[-\/](\d{1,2})/ ) {
            return sprintf "%04d-%02d-%02d", $1, $2, $3;
        }
        # MM/DD/YYYY
        elsif ( $_ =~ /^(\d{1,2})[-\/](\d{1,2})[-\/](?:20)?(\d{2})/ ) {
            return sprintf "20%02d-%02d-%02d", $3, $1, $2;
        }
    }

    $self->fail("Unable to determine date begin.");
}

sub _getDateEnd {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin') || '';
    my $periodEnd = $self->_getByFieldName('periodEnd') || '';

    foreach ( $dateBegin, $periodEnd ) {
        # YYYY-MM-DD
        if ( $_ =~ /^(\d{4})[-\/](\d{1,2})[-\/](\d{1,2})/ ) {
            return sprintf "%04d-%02d-%02d", $1, $2, $3;
        }
        # MM/DD/YYYY
        elsif ( $_ =~ /^(\d{1,2})[-\/](\d{1,2})[-\/](?:20)?(\d{2})/ ) {
            return sprintf "20%02d-%02d-%02d", $3, $1, $2;
        }
    }

    $self->fail("Unable to determine date end.");
}

sub rPriceType {
    my $self = shift;

    my $type = $self->_getByFieldName('rPriceType') || '';
    if ( Common::RSApp::GetClientID == 318 ) {
        return 'agency';
    } elsif ( $type =~ /^agency$/i ) {
        return 'agency';
    } elsif ( $type =~ /^wholesale$/i ) {
        return 'rrp';
    }

    $self->fail("Unexpected priceType found: $type ");
}

sub rListPrice {
    my $self = shift;

    return $self->_getByFieldName('rPriceType') || $self->_getByFieldName('rUnitPrice');
}


1;