package Yaala::Report;

use strict;
use warnings;

use Exporter;
use Yaala::Html qw#head foot escape navbar get_filename get_title#;
use Yaala::Data::Core qw#receive get_values#;
use Yaala::Data::Setup qw#$SELECTS#;
use Yaala::Data::Convert qw#convert#;
use Yaala::Config qw#get_config#;
use Yaala::Report::Core qw#$OUTPUTDIR#;
use Yaala::Report::GDGraph qw#generate_graph $GRAPH_WIDTH $GRAPH_HEIGHT#;

@Yaala::Report::EXPORT_OK = qw#generate#;
@Yaala::Report::ISA = ('Exporter');

my $VERSION = '$Id: Classic.pm,v 1.10 2003/12/07 14:53:30 octo Exp $';
print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);

our $skip_empty = 1;
if (get_config ('classic_skip_empty'))
{
	my $conf = lc (get_config ('classic_skip_empty'));
	if ($conf eq 'no' or $conf eq 'false') { $skip_empty = 0; }
}
	
return (1);

sub generate
{
	for (@$SELECTS)
	{
		my $sel = $_;
		my @keys = @{$sel->[1]};

		generate_sub_index ($sel);

		for (@keys)
		{
			my $key = $_;
			generate_sub_page ($sel, $key);
		}
	}

	generate_index_page ();

	return (1);
}

sub generate_sub_page
{
	my $sel = shift;
	my $key = shift;

	my @vals = get_values ($sel, $key);

	my $filename = get_filename ($sel);
	$filename =~ s/\.html$/__$key.html/;
	my $title = get_title ($sel);

	open (FH, '> ' . $OUTPUTDIR . $filename) or die ('open: ' . $!);
	
	print FH head ($title, $title);
	print FH '<h2>', ucfirst ($key), "</h2>\n";
	print FH qq#<a id="top_of_page"></a>\n#;
	print FH navbar ($sel);
	print FH own_navbar ($sel, $key);
	
	my $graph_file = generate_graph ($sel, $key);
	if ($graph_file)
	{
		print FH qq#<p><img src="$graph_file" width="$GRAPH_WIDTH" #,
		qq#height="$GRAPH_HEIGHT" alt="[graph]" /></p>\n#;
	}
	
	for (sort (@vals))
	{
		my $val = $_;
		print FH generate_table ($sel, $key, $val);
	}

	print FH foot ();
	close (FH);
}

sub generate_sub_index
{
	my $sel = shift;

	my $filename = get_filename ($sel);
	my $title = get_title ($sel);

	open (FH, '> ' . $OUTPUTDIR . $filename) or die ('open: ' . $!);

	print FH head ($title, $title);
	print FH navbar ($sel);
	print FH own_navbar ($sel);

	print FH "<table>\n";
	for (@{$sel->[1]})
	{
		my $key = $_;
		my @vals = get_values ($sel, $key);
		my $num_vals = scalar (@vals);

		print FH "  <tr>\n    <th>", ucfirst ($key), "</th>\n",
		"    <td>$num_vals entr", ($num_vals == 1 ? 'y' : 'ies'),
		"</td>\n  </tr>\n";
	}
	print FH "</table>\n\n";
	
	print FH foot ();
	close (FH);
}

sub generate_index_page
{
	open (FH, '> ' . $OUTPUTDIR . 'index.html') or die ('open: ' . $!);

	print FH head ("yaala $::VERSION", "yaala $::VERSION - Index");
	print FH navbar ();

	if (scalar (keys (%$::EXTRA)))
	{
		print FH "\n<hr>\n<table>\n";
		for (keys (%$::EXTRA))
		{
			my $key = $_;
			my $val = $::EXTRA->{$key};

			print FH qq#  <tr>\n    <th class="top">$key</th>\n    <td>$val</td>\n  </tr>\n#;
		}
		print FH "</table>\n";
	}
	else
	{
		print FH "\n<!-- no \%::EXTRA -->\n";
	}
	
	print FH foot ();
	close (FH);
}

sub generate_table
{
	my $sel = shift;
	my $key = shift;
	my $val = shift;
	
	my @aggs = @{$sel->[0]};
	my $num_aggs = scalar (@aggs);

	my @keys = grep { $_ ne $key } (@{$sel->[1]});
	@keys = sort (@keys);


	my $link_val = $val;
	$link_val =~ s/\W//g;
	
	my $text = qq#\n<hr />\n<table id="$link_val">\n  <tr>\n#;
	$text .= '    <th colspan="' . (2 + (2 * $num_aggs)) . '">' . ucfirst ($val) . "</th>\n  </tr>\n";
	$text .= qq#  <tr>\n    <th class="subhdr">Field</th>\n#
	.	qq#    <th class="subhdr">Value</th>\n#;
	
	my %grand_total = ();
	for (@aggs)
	{
		$text .= qq#    <th class="subhdr"># . ucfirst ($_) . "</th>\n"
		.	qq#    <th class="subhdr">Percent</th>\n#;
		
		$grand_total{$_} = receive ($sel, $_, {$key => $val});
	}
	$text .= "  </tr>\n";

	for (@keys)
	{
		my $second_key = $_;
		my @second_vals = get_values ($sel, $second_key);

		my $tmp_text = '';
		my $first_line = 1;
		my %sub_total = ();
		
		my $num_vals = scalar (@second_vals);

		for (sort (@second_vals))
		{
			my $this_val = $_;
			my $skipped_cells = 0;
			my $tmp_text2 = '';

			if (!$first_line) { $tmp_text2 = "  <tr>\n"; }
			$tmp_text2 .= qq#    <td>$this_val</td>\n#;
			
			for (@aggs)
			{
				my $agg = $_;
				my $sum = 0;
				
				if (!defined ($sub_total{$agg})
						or ($sub_total{$agg} != $grand_total{$agg}))
				{
					$sum = receive ($sel, $agg, {$key => $val, $second_key => $this_val});
					$sub_total{$agg} += $sum;
				}

				if (!$sum and $skip_empty)
				{
					$skipped_cells++;
				}

				my $print_sum = convert ($agg, $sum);

				my $percent = ($sum ? sprintf ("%.1f%%", 100 * $sum / $grand_total{$agg}) : '&nbsp;');
			
				$tmp_text2 .= "    <td>"
				.	($print_sum ? $print_sum : '&nbsp;' )
				.	qq#</td>\n#
				.	qq#    <td>$percent</td>\n#;
			}

			$tmp_text2 .= "  </tr>\n";
			
			if ($skipped_cells == $num_aggs)
			{
				$num_vals--;
			}
			else
			{
				$first_line = 0;
				$tmp_text .= $tmp_text2;
			}
		}
		
		$text .= qq#  <tr>\n    <th class="subhdr"#
		. ($num_vals > 1 ? qq# rowspan="$num_vals"# : '')
		. '>' . ucfirst ($second_key) . "</th>\n"
		. $tmp_text;
	}

	$text .= qq#  <tr>\n    <th class="subhdr">Total</th>\n#
	.	qq#    <td class="total">&nbsp;</td>\n#;
	for (@aggs)
	{
		my $print_sum = convert ($_, $grand_total{$_});
		
		$text .= qq#    <td class="total">$print_sum</td>\n#
		.	qq#    <td class="total">100.0%</td>\n#;
	}
	$text .= qq#  </tr>\n</table>\n#
	.	qq(<p>[&nbsp;<a href="#top_of_page">top</a>&nbsp;]</p>\n);

	return ($text);
}

sub own_navbar
{
	my $sel = shift;
	my $key = shift;

	if (!defined ($key)) { $key = ''; }

	my $base_filename = get_filename ($sel);
	my $text = qq#<p class="navbar">\n#;

	for (@{$sel->[1]})
	{
		my $this_key = $_;
		my $this_filename = $base_filename;
		$this_filename =~ s/\.html$/__$this_key.html/;

		if ($this_key eq $key)
		{
			$text .= '  <span>[ ' . ucfirst ($key) . " ]</span>\n";
		}
		else
		{
			$text .= qq#  <span>[ <a href="$this_filename"># . ucfirst ($this_key) . "</a> ]</span>\n";
		}
	}

	$text .= "</p>\n";

	if ($key)
	{
		my @vals = get_values ($sel, $key);

		$text .= qq#<p class="navbar">\n#;
		for (sort (@vals))
		{
			my $link_val = $_;
			my $print_val = convert ($key, $_);
			$link_val =~ s/\W//g;
		
			$text .= qq(  <span>[ <a href="#$link_val">$print_val</a> ]</span>\n);
		}
		$text .= "</p>\n";
	}
	
	return ($text);
}
