#!/usr/bin/perl

# AI Lab web server runs as user "mail", UID 8

use strict;
use CGI qw(:cgi-lib);
use FileHandle;

my $dbFile = "scores.jdb";
my $q = new CGI;

my $showComments = (defined($q->param("button")) and $q->param("button") eq "Show Comments") ? 1 : 0;

sub mysort
{
    return $a <=> $b if ($a->[0] ne $b->[0]);
    #### START HERE ####
}

sub max
{
    for (my $i=1; $i < @_; ++$i) {
	$_[0] = $_[$i] if ($_[$i] > $_[0]);
    }
    return $_[0];
}

print $q->header;
print $q->start_html("Disc Golf Scores");
print $q->start_form("put", "display.cgi");
my $button = $showComments ? "Hide Comments" : "Show Comments";
print $q->submit(-name=>'button',-value=>$button,-override=>1);
print $q->end_form();
my $ret = readFile();

# Sort courses by number of rounds
sub byRounds { @{$ret->{$b}} <=> @{$ret->{$a}} }
my @courses = keys(%$ret);
@courses = sort byRounds @courses;

foreach my $course (@courses) {
    my $entries = $ret->{$course};
    my $max = 0;
    foreach my $entry (@$entries) {
	my @holes = parseHoles($entry->[3]);
	$max = $holes[$#holes] if ($holes[$#holes] > $max);
    }
    my @tableRows;
    my @totals;
    my @sum;
    my @count;
    my @nineSum;
    foreach my $entry (@$entries) {
	my ($mon, $mday, $year, $holestring, @scores) = @$entry;
	my $comment = pop @scores;
	my @holes = parseHoles($holestring);
	my @row = ("$mon&nbsp;$mday,&nbsp;$year");
	my $tot;
	my @nine;
	my @nineCnt;
	for (my $i=0; $i < @holes; ++$i) {
	    my $hole = $holes[$i];
	    #$row[$hole] = $scores[$i];
	    $row[($hole-1)%9+int(($hole-1)/9)*10+1] = $scores[$i];
	    $sum[$hole] += $scores[$i];
	    $nine[int(($hole-1)/9)] += $scores[$i];#
	    ++$nineCnt[int(($hole-1)/9)];
	    $tot += $scores[$i];
	    ++$count[$hole];
	}
	for (my $i=1; $i <= $max%9+int($max/9)*10; ++$i) {
	    $row[$i] = $nine[int($i/10)-1] if ($i%10==0 and $nineCnt[int($i/10)-1]==9);#
	    $row[$i] = "" if (!defined($row[$i]));
	}
	push @row, $tot;
	push @tableRows, $q->td(\@row);
	push @tableRows, $q->td({colspan=>($max+2),align=>"left"},$comment) if ($showComments);
# <td colspan=$max+2 align="left"><div id="off"><a href="javascript:switchLayers('on');"><img src="plus.png"></a></div><div id="on"><div id="off"><a href="javascript:switchLayers('off');"><img src="minus.png"></a>$comment</div></td>
# 
	push @totals, $tot;
    }
    
    print $q->h2($course), "\n";
    my @topRow = ($q->td(""));
    for (my $i=1; $i <= $max; ++$i) {
	push @topRow, $q->td( $i<10 ? "<font color=\"#ffffff\">-</font>$i" : $i );
	push @topRow, $q->td("Tot9") if ($i % 9 == 0);#
    }
    push @topRow, $q->td("Total");
    unshift @tableRows, join("", @topRow);
    my @bottomRow = ($q->td("Average"));
    my $tot = 0;
    my $nine = 0;
    for (my $i=1; $i <= $max; ++$i) {
	if ($count[$i] > 0) {
	    my $score = int($sum[$i]/$count[$i]*10+0.5)/10;
	    $nine += $score;#
	    $tot += $score;
	    push @bottomRow, $q->td($score);
	} else {
	    push @bottomRow, $q->td("");
	}
	if ($i%9==0) {#
	    push @bottomRow, $q->td($nine);#
	    $nine = 0;#
	}#
    }
    push @bottomRow, $q->td($tot);
    push @tableRows, join("", @bottomRow);
    print $q->table({border=>2},$q->Tr({align=>"right"},\@tableRows));
    print "\n";
}
print $q->end_html;

sub readFile
{
    my $ret;
    my $fh = new FileHandle($dbFile);
    if (!defined($fh)) {
	print $q->h2("Unable to open database file ($dbFile: $!)");
	die;
    }
    while ($_ = <$fh>) {
	my ($course, @foo) = split(/\|/, $_);
	push @{$ret->{$course}}, \@foo;
    }
    return $ret;
}

sub parseHoles
{
    my ($s) = @_;
    my @ret;
    my @tokens = split(/[^\-0-9]/, $s);
    foreach my $tok (@tokens) {
	if (my ($start, $finish) = ($tok =~ m/^(\d{1,2})\-(\d{1,2})$/)) {
	    for (my $i=$start; $i <= $finish; ++$i) {
		push @ret, $i;
	    }
	} elsif (my ($num) = ($tok =~ m/^(\d{1,2})$/)) {
	    push @ret, $num;
	} else {
	    print $q->p.$q->em("Bad custom hole definition")."\n";
	    die;
	}
    }
    return @ret;
}

