package WebPAC::Output::TT;

use warnings;
use strict;

use base qw/WebPAC::Common/;

use Template;
use List::Util qw/first/;
use Data::Dumper;

=head1 NAME

WebPAC::Output::TT - use Template Toolkit to produce output

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

Produce output using Template Toolkit.

=head1 FUNCTIONS

=head2 new

Create new instance.

 my $tt = new WebPAC::Output::TT(
 	include_path => '/path/to/conf/output/tt',
	filters => {
		filter_1 => sub { uc(shift) },
	},
 );

By default, Template Toolkit will C<EVAL_PERL> if included in templates.

=cut

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

	my $log = $self->_get_logger;

	# create Template toolkit instance
	$self->{'tt'} = Template->new(
		INCLUDE_PATH => $self->{'include_path'},
		FILTERS => $self->{'filter'},
		EVAL_PERL => 1,
	);
	
	$log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'});

	$log->debug("filters defined: ",Dumper($self->{'filter'}));

	$self ? return $self : return undef;
}


=head2 apply

Create output from in-memory data structure using Template Toolkit template.

 my $text = $tt->apply(
 	template => 'text.tt',
	data => \@ds
 );

=cut

sub apply {
	my $self = shift;

	my $args = {@_};

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

	foreach my $a (qw/template data/) {
		$log->logconfess("need $a") unless ($args->{$a});
	}

	# filter to return value from @ds

	sub tt_filter_d {

		my ($name,$join) = @_;

print "### name = ",Dumper($name);

		my $item = first { $_->{'name'} eq $name } @{ $args->{'data'} };

		return unless($item);

print "### item = ",Dumper($item);

		my $v = $item->{'display'} || return;

		if (ref($v) eq 'ARRAY') {
			if ($#{$v} == 0) {
				$v = $v->[0];
			} else {
				$join = '&#182;<br/>' unless defined($join);
				$v = join($join, @{$v});
			}
		}
print "### v = $v\n";
		return $v;
	}
	$args->{'d'} = \&tt_filter_d;


	my $out;

	$self->{'tt'}->process(
		$args->{'template'},
		$args,
		\$out
	) || $log->logconfess( "apply can't process template: ", $self->{'tt'}->error() );

	return $out;
}

=head2 to_file

Create output from in-memory data structure using Template Toolkit template
to a file.

 $tt->to_file(
        file => 'out.txt',
 	template => 'text.tt',
	data => \@ds
 );

=cut

sub to_file {
	my $self = shift;

	my $args = {@_};

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

	my $file = $args->{'file'} || $log->logconfess("need file name");

	$log->debug("creating file ",$file);

	open(my $fh, ">", $file) || $log->logdie("can't open output file '$file': $!");
	print $fh $self->output(
		template => $args->{'template'},
		data => $args->{'data'},
	) || $log->logdie("print: $!");
	close($fh) || $log->logdie("close: $!");

	return 1;
}


=head1 AUTHOR

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

=head1 COPYRIGHT & LICENSE

Copyright 2005 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::Output::TT
