package WebPAC::Input::ISI;

use warnings;
use strict;

use WebPAC::Input;
use base qw/WebPAC::Common/;

use Data::Dump qw/dump/;

=head1 NAME

WebPAC::Input::ISI - support for ISI Export Format

=cut

our $VERSION = '0.03';

=head1 SYNOPSIS

Open file in ISI export fromat

 my $input = new WebPAC::Input::ISI(
	path => '/path/to/ISI/records.txt',
 );

=head1 FUNCTIONS

=head2 new

Returns new low-level input API object

  my $input = new WebPAC::Input::ISI(
  	path => '/path/to/ISI/records.txt'
	filter => sub {
		my ($l,$field_nr) = @_;
		# do something with $l which is line of input file
		return $l;
	},
  }

Options:

=over 4

=item path

path to ISI export file

=back

=cut

my $subfields = {
	'CR' => sub {
		my $full_cr = shift;
		my @v = split(/, /, $full_cr);
		my $f = { full => $full_cr };
		foreach ( qw/author year reference volume page doi/ ) {
			if ( my $tmp = shift @v ) {
				$f->{$_} = $tmp;
			}
		}
		if ( $f->{author} =~ /^\*(.+)/ ) {
			delete $f->{author};
			$f->{institution} = $1;
		}
		$f->{doi} =~ s{DOI\s+}{} if $f->{doi}; # strip DOI prefix
		return $f;
	},
};

sub new {
	my $class = shift;
	my $self = {@_};
	bless($self, $class);

	my $arg = {@_};

	my $log = $self->_get_logger();

	open( my $fh, '<', $arg->{path} ) || $log->logconfess("can't open $arg->{path}: $!");

	my ( $format, $version );

	my $line = <$fh>;
	chomp($line);
	if ( $line =~ /^FN\s(.+)$/) {
		$format = $1;
	} else {
		$log->logdie("first line of $arg->{path} has to be FN, but is: $line");
	}

	$line = <$fh>;
	chomp($line);
	if ( $line =~ /^VR\s(.+)$/) {
		$version = $1;
	} else {
		$log->logdie("second line of $arg->{path} has to be VN, but is: $line");
	}

	$log->info("opening $format $version database '$arg->{path}'");

	my $tag;
	my $rec;

	$self->{size} = 0;

	while( $line = <$fh> ) {
		chomp($line);

		my $v;

		if ( $line =~ /^(\S\S)\s(.+)$/ ) {
				$tag = $1;
				$v = $2;
		} elsif ( $line =~ /^\s{3}(.+)$/ ) {
				$v = $1;
				if ( $tag eq 'CR' && $v =~ m{DOI$} ) {
					my $doi = <$fh>;
					chomp($doi);
					$doi =~ s{^\s{3}}{ } || die "can't find DOI in: $doi";
					$v .= $doi;
				}
		} elsif ( $line eq 'ER' ) {
			# join tags
			foreach ( qw/AB DE ID TI SO RP SC/ ) {
				$rec->{$_} = join(' ', @{ $rec->{$_} }) if defined $rec->{$_};
			}
			$rec->{'000'} = [ ++$self->{size} ];
			push @{ $self->{_rec} }, $rec;
			$rec = {};
			$line = <$fh>;
			chomp $line;
			$log->logdie("expected blank like in ",$arg->{path}, " +$.: $line") unless ( $line eq '' );
		} elsif ( $line eq 'EF' ) {
			last;
		} else {
			$log->logdie("can't parse +$. $arg->{path} : $line");
		}

		if ( defined $v ) {
			$v = $subfields->{$tag}->($v) if defined $subfields->{$tag};

			$log->debug("$tag: ", sub { dump( $v ) });
			push @{ $rec->{$tag} }, $v;
		}

	}

	$log->debug("loaded ", $self->size, " records");

	$self ? return $self : return undef;
}

=head2 fetch_rec

Return record with ID C<$mfn> from database

  my $rec = $input->fetch_rec( $mfn, $filter_coderef );

=cut

sub fetch_rec {
	my $self = shift;

	my ( $mfn, $filter_coderef ) = @_;

	return $self->{_rec}->[$mfn-1];
}


=head2 size

Return number of records in database

  my $size = $input->size;

=cut

sub size {
	my $self = shift;
	return $self->{size};
}

=head1 SEE ALSO

L<http://isibasic.com/help/helpprn.html> is only sane source of document format which Google could find...
 
=head1 AUTHOR

Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

=head1 COPYRIGHT & LICENSE

Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of WebPAC::Input::ISI
