package BookPub::Import::Importer::OpenRoadMedia::v2;

use strict;
use warnings;

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

sub _fieldMap {
    my $self = shift;
    my %map = (
        "Sales Detail" => {
            dateBegin          => 'A',
            purchaseType       => 'C',
            rProductType       => 'C',
            rAuthor            => 'O',
            rTitle             => 'P',
            rIsbn13            => 'E',
            rImprint           => 'M',
            countryCode        => 'F',
            rUnits             => 'H',
            rListPrice         => 'G',
            rPurchasePrice     => 'J',
            rRevenue           => 'I',
            currencyCode       => 'I1',
            rDeliveryMethod    => 'V',
            rDistributor       => 'B',
            rModel             => 'N',
        },
        "Berrett-Koehler Publishers" => {
            rAuthor            => 'C',
            rTitle             => 'B',
            rIsbn13            => 'A',
            rUnits             => 'F',
            rRevenue           => 'J',
            currencyCode       => 'G1',
        }
    );

    return exists($map{$self->sheetname}) ? $map{$self->sheetname} : {} if $self->sheetname;
}

sub _headerIdentifier { rTitle => 'Title' }

sub rServiceName               { 'Open Road Media' }
sub rPriceType                 { 'rrp' }
sub priceType                  { shift->rPriceType }
sub productType                { shift->rProductType() }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub currencyCode               { $_[0]->{_currency} if $_[0]->{_currency} }
sub rListPriceCurrency         { 'USD' }
sub rPurchasePriceCurrency     { 'USD' }
sub _useIsSaleRec              { 1 }

sub _processSheet {
    my ( $self, $sheet, $sheetnum ) = @_;

    my $clientID = Common::RSApp::GetClientID();
    my $sheetname = $self->sheetname;

    if ($clientID == 335 && $sheetname =~ /^Berrett-Koehler Publishers$/i ||
        $clientID != 335 && $sheetname =~ /^Sales Detail$/i) {
        $self->SUPER::_processSheet( $sheet, $sheetnum );
    }
}

sub _processHeader {
    my $self = shift;

    unless ($self->{_currency}) {
        my $currency = $self->_getByFieldName("currencyCode");
        if ($currency =~ /\((\w{3})\)$/ ) {
            $self->{_currency} = $1;
        }
    }
    return $self->SUPER::_processHeader();
}

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;

    if (Common::RSApp::GetClientID() == 335) {
        my ($mon,$year) = ($self->filename() =~ /(\w{3}).(\d{4})\./);
        return sprintf("%04d-%02d-01", $year, $months{lc($mon)});
    }
    else {
        my $date = $self->_getByFieldName('dateBegin') || '';
        if ($date =~ /^(\d{4}).(\d{2}).(\d{2})$/ ){
            return sprintf("%04d-%02d-%02d", $1, $2, $3);
        }
        if (my ($day, $mon, $year) = ($date =~ /^(\d{1,2}).(\d{1,2}).(\d{2,4})$/ )){
            $year = $year < 100 ? $year + 2000 : $year;
            return sprintf("%04d-%02d-%02d", $year, $mon, $day);
        }
    }
}

sub countryCode {
    my $self = shift;

    return 'US' if Common::RSApp::GetClientID() == 335;
    $self->SUPER::countryCode();
}

sub rProductType {
    my $self = shift;

    my $type = $self->_getByFieldName('rProductType') || '';
    if ( $type =~ /^(?:ebook|subscription)$/i || Common::RSApp::GetClientID() == 335 ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

    $self->fail("Unknown product type: $type");
}

sub rUnits {
    my $self = shift;

    my $units = $self->_getByFieldName('rUnits') || 0;
    return $units;
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || 0;
    return $revenue;
}

sub _isSaleRec {
    my $self = shift;

    return if !$self->rIsbn13;
    return if $self->rIsbn13 =~ /^Grand total$/i;
    return if !$self->rUnits && !$self->rRevenue;

    return $self->SUPER::_isSaleRec();
}

sub isFree {
    my $self = shift;

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

1;
