package BookPub::Import::Importer::Ambassador;

use strict;

use lib '/app/tools/common/lib';
use Date::Calc qw(Days_in_Month);
use Data::Dumper;
use Date::Simple;

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

sub _fieldMap {
    my $self     = shift;
    my %fieldMap = (
        1 => {
            dateBegin      => 'A2',
            dateEnd        => 'A2',
            rTitle         => 'C',
            rIsbn13        => 'B',
            rInstitution   => 'D',
            rUnits         => 'H',
            rUnitPrice     => 'G',
            rListPrice     => 'E',
            rPurchasePrice => 'E',
            rRevenue       => 'I',
        },
        2 => {
            dateBegin      => 'B',
            dateEnd        => 'B',
            rTitle         => 'D',
            rIsbn13        => 'C',
            rInstitution   => 'E',
            rUnits         => 'I',
            rUnitPrice     => 'H',
            rListPrice     => 'F',
            rPurchasePrice => 'F',
            rRevenue       => 'J',
        },
        3 => {
            dateBegin      => 'B',
            dateEnd        => 'B',
            rAuthor        => 'E',
            rTitle         => 'D',
            rIsbn13        => 'C',
            rInstitution   => 'E',
            rState         => 'F',
            rUnits         => 'L',
            rUnitPrice     => 'K',
            rListPrice     => 'I',
            rPurchasePrice => 'I',
            rRevenue       => 'M',
            rDuration      => 'H',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _headerIdentifier          { rIsbn13 => 'POD ISBN' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypePrintOnDemand }
sub priceType                  { 'rrp' }
sub discount                   { 0 }
sub countryCode                { 'US' }
sub currencyCode               { 'USD' }
sub rListPriceCurrency         { 'USD' }
sub rPurchasePriceCurrency     { 'USD' }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName(
        service_id => shift->serviceID
    );
}

sub productType {
    my $self = shift;

    if ( $self->_version == 3 ) {
        return  BookPub::DB::Item::Sale::kProductTypeEBook
    }

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

sub rProductType {
    my $self = shift;

    if ( $self->_version == 3 ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

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

sub _processLine {
    my $self = shift;

    if (   !$self->rInstitution()
        && !$self->rTitle()
        && !$self->rIsbn13()
        && !$self->rUnitPrice()
        && !$self->rListPrice ) {

        $self->{_endOfData} = 1;
        return;
    }

    return $self->SUPER::_processLine(@_);
}

sub _isSaleRec {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

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

sub dateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');

    if ( $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{4}) To: (\d{1,2})\/(\d{1,2})\/(\d{4})/i ) {
        my $startDate = sprintf( "%04d-%02d-%02d", $3, $1, $2 );
        return $startDate;
    } elsif ( $date =~ /(\d{4})\/(\d{2})/i ) {
        my $startDate = sprintf( "%04d-%02d-%02d", $1, $2, 1 );
        return $startDate;
    } else {
        $self->fail("Can't determine dateBegin: $date");
    }
}

sub dateEnd {
    my $self = shift;
    my $date = $self->_getByFieldName('dateEnd');

    if ( $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{4}) To: (\d{1,2})\/(\d{1,2})\/(\d{4})/i ) {
        my $endDate = sprintf( "%04d-%02d-%02d", $6, $4, $5 );
        return $endDate;
    } elsif ( $date =~ /(\d{4})\/(\d{2})/i ) {
        my $daysInMonth = Days_in_Month( $1, $2 );
        my $endDate = sprintf( "%04d-%02d-%02d", $1, $2, $daysInMonth );
        return $endDate;
    } else {
        $self->fail("Can't determine dateEnd $date");
    }
}

1;
