package Yaala::Html;

use strict;
use warnings;

use Exporter;
use Yaala::Config qw#get_config#;
use Yaala::Data::Setup qw#$SELECTS#;

@Yaala::Html::EXPORT_OK = qw(escape head foot navbar get_filename get_title);
@Yaala::Html::ISA = ('Exporter');

=head1 Html.pm

A set of utilities used by report modules.

=cut

my $VERSION = '$Id: Html.pm,v 1.8 2003/12/07 14:52:02 octo Exp octo $';
print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);

return (1);

# NB: preserves all &foo; to allow inclusion of strange characters
#     returns list
sub escape
{
	my @esc = map
	{
		s/</&lt;/g;
		s/>/&gt;/g;
		s/"/&quot;/g;
		s/\s{2,}/&nbsp;/g;
		$_;
	} (@_);

	if (wantarray ())
	{
		return (@esc);
	}
	else
	{
		return (join ('', @esc));
	}
}

# generates only common header - with title and head.
sub head
{
	my ($title, $header) = @_;
	my $text;
	my $charset = get_config ('html_charset');
	my $stylesheet = get_config ('html_stylesheet');

	if (!defined ($charset) or !$charset) { $charset = 'iso-8859-1'; }
	if (!defined ($stylesheet) or !$stylesheet) { $stylesheet = 'style.css'; }
	
	$text = qq#<?xml version="1.0" encoding="$charset"?>\n#
	.	qq#<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"\n#
	.	qq#\t"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n#
	.	qq#<html>\n<head>\n#;
	
	if ($stylesheet)
	{
		$text .= qq#  <link rel="stylesheet" type="text/css" #
		.	qq#href="$stylesheet" />\n#;
	}
	
	$text .= "  <title>$title</title>\n"
	.	qq#  <meta name="robots" value="noindex, nofollow" />\n#
	.	"</head>\n\n"
	.	"<body>\n";
	
	$text .= qq#<h1><img src="logo.png" /> $header</h1>\n# if $header;
	return $text;
}

sub foot
{
	my ($a, $e);
	my $text = "<hr />\n"
	.	qq#<p id="footer">Generated by <a href="$::HOMEPAGE">$::NAME $::VERSION</a>, #
	.	scalar (localtime ())
	.	"</p>\n";

	$text .= "\n</body>\n</html>\n";
	return $text;
}

sub navbar
{
	my $sel = shift;
	my $text = qq#<p class="navbar">\n#;

	if (defined ($sel) and ref ($sel))
	{
		$text .= qq#  <span>[ <a href="index.html">General</a> ]</span>\n#;
	}
	else
	{
		$text .= qq#  <span>[ General ]</span>\n#;
		$sel = '';
	}

	for (@$SELECTS)
	{
		my $this_sel = $_;
		my $title = get_title ($this_sel);

		if ("$this_sel" eq "$sel")
		{
			$text .= qq#  <span>[ $title ]</span>\n#;
		}
		else
		{
			my $filename = get_filename ($this_sel);
			$text .= qq#  <span>[ <a href="$filename">$title</a> ]</span>\n#;
		}
	}

	$text .= "</p>\n\n";
	return ($text);
}

sub get_filename
{
	my $sel = shift;

	my $aggs = join ('-', @{$sel->[0]});
	my $flds = join ('-', @{$sel->[1]});

	my $filename = $aggs . '_BY_' . $flds;
	
	my %sign_names =
	(
		'=='	=> 'eq',
		'eq'	=> 'eq',
		'>='	=> 'ge',
		'<='	=> 'le',
		'!='	=> 'ne',
		'=~'	=> 're',
		'!~'	=> 'nre',
		'<'	=> 'lt',
		'>'	=> 'gt'
	);
	
	if (scalar (@{$sel->[2]}))
	{
		my @where = ();
		for (@{$sel->[2]})
		{
			my ($key, $op, $val) = @$_;
			$val =~ s/\W//g;
			
			$op = $sign_names{$op} if (defined ($sign_names{$op}));
			push (@where, join ('-', ($key, $op, $val)));
		}

		$filename .= '_WHERE_' . join ('_AND_', @where);
	}
	
	$filename .= '.html';

	return ($filename);
}

sub get_title
{
	my $sel = shift;

	my @aggs = map { ucfirst ($_) } (@{$sel->[0]});
	my @flds = map { ucfirst ($_) } (@{$sel->[1]});

	my $title = my_join (@aggs) . ' by ' . my_join (@flds);

	if (scalar (@{$sel->[2]}))
	{
		$title .= ' where ';
		my @wheres = map
		{
			ucfirst ($_->[0]) . ' '
			. $_->[1]
			. ' "' . $_->[2] . '"'
		} (@{$sel->[2]});

		$title .= my_join (@wheres);
	}

	($title) = escape ($title);
	return ($title);
}

sub my_join
{
	my @all = @_;
	my $last = pop (@all);

	return ($last) unless (@all);
	
	my $retval = join (', ', @all) . " and $last";

	return ($retval);
}
