package BookPub::Import::Importer::Sony::Version12;

use strict;

use lib '/app/tools/common/lib/';
use Common::Country;
use Common::CurrencyFormat;

use lib '/app/tools/sale_import/lib/';
use Import::ValidationError;

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

sub _headerIdentifier { ( rIsbn13 => 'Isbn13' ) }

sub _fieldMap {
    {
        countryCode    => 'B',
        dateBegin      => 'S',
        rIsbn13        => 'C',
        rTitle         => 'D',
        rAuthor        => 'E',
        rImprint       => 'C',
        rUnits         => 'G',
        rSales         => 'E',
        rReturns       => 'F',
        rListPrice     => 'I',
        rPurchasePrice => 'J',
        rDiscount      => 'N',
        rRevenue       => 'R',
    };
}

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType    { 'agency' }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub currencyCode {
    my $self        = shift;
    my $countryCode = $self->_getByFieldName('countryCode');
    if ( $countryCode ne "" ) {
        my $currencyFormat = Common::CurrencyFormat->new( countryCode => $countryCode );
        my $currencyCode = $currencyFormat->currencyCode();
        return $currencyCode;
    }
    return undef;
}

sub rTitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $title;
}

sub rSubtitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $subtitle;
}

sub _isValidSaleRecord {
    my $self = shift;

    return $self->rTitle && $self->dateBegin;
}

1;
