#!/opt/perl5/bin/perl
############################################################
#
# $Header: E:\\RCS\\E\\Tt\\Ittc\\tools\\Record.pm,v 1.5 2001-06-09 13:10:54+01 gael Exp $
#
# $Source: E:\\RCS\\E\\Tt\\Ittc\\tools\\Record.pm,v $
# $Revision: 1.5 $
# $Locker:  $
#
# Module describing RECORD class
#
############################################################

package RECORD;

use strict;
use ITTC;
use File::stat;

use Exporter;
use vars qw(@ISA @EXPORT $VERSION);

@ISA = qw (Exporter);

@EXPORT = qw ();

# RCS versioning
$VERSION = '$Revision: 1.5 $';
$VERSION =~ s/\$(Revision): ([0-9\.]*) \$$/$2/;

my %hash_month = ('January' => '01',
                  'February' => '02',
                  'March' => '03',
                  'April' => '04',
                  'May' => '05',
                  'June' => '06',
                  'July' => '07',
                  'August' => '08',
                  'September' => '09',
                  'October' => '10',
                  'November' => '11',
                  'December' => '12');

#
# ->new
#
# Attributes:
#   tournament: reference to a TOURNAMENT object
#   event: reference to an EVENT object
#   medal: reference to a MEDAL object
#
sub new {
    my ($self) = bless {}, shift;
    my ($tournament, $event, $medal) = @_;
    $self->{'tournament'} = $tournament;
    $self->{'event'} = $event;
    $self->{'medal'} = $medal;
    return $self;
}

#
# Attribute access routines
#
sub tournament	 { $_[0]->{'tournament'} }
sub event        { $_[0]->{'event'} }
sub medal        { $_[0]->{'medal'} }

#
# sort records by date and town, put major tournaments in first
#
sub sort_records {
  $b->tournament->year <=> $a->tournament->year
    or
      $b->tournament->is_major <=> $a->tournament->is_major
	or
	  $hash_month{$b->tournament->month} <=> $hash_month{$a->tournament->month}
	    or
	      $b->tournament->day cmp $a->tournament->day
		or
		  $a->tournament->town cmp $b->tournament->town
		    or
		      MEDAL->color_name($a->medal->color) <=> MEDAL->color_name($b->medal->color)
		    }


#
# print in HTML an array of RECORD objects
# with the specified caption to the specified FileHandle
#
sub print_html {
    shift;
    my ($INDEX, $event_file, $caption, @records) = @_;
    print $INDEX '<TABLE BORDER=1 WIDTH="100%" CELLSPACING="0" CELLPADDING="2" NOWRAP ';
    print $INDEX 'bgcolor="'.$ittc_bgcolor.'"><CAPTION><STRONG>';
    print $INDEX $caption.'</STRONG></CAPTION><TR VALIGN="Top" bgcolor="'.$ittc_th_bgcolor.'">';
    print $INDEX '<TH><font color="'.$ittc_th_font_color.'">Year</font></TH>'."\n";
    print $INDEX '<TH><font color="'.$ittc_th_font_color.'">Event</font></TH>'."\n";
    print $INDEX '<TH><font color="'.$ittc_th_font_color.'">Town</font></TH>'."\n";
    print $INDEX '<TH><font color="'.$ittc_th_font_color.'">Results</font></TH></TR>'."\n";

    my ($record, $tournament, $event, $medal);
    my $year = '';
    my $tour_dirname = '';
    my %year_rowspan;
    my %event_rowspan;

    foreach $record (@records) {
	$tournament = $record->tournament;
	$year_rowspan{$tournament->year}++;
	$event_rowspan{$tournament->dirname}++;
    }

    foreach $record (sort {sort_records($a, $b)} @records) {
	$tournament = $record->tournament;
	$event = $record->event;
	$medal = $record->medal;
	if ($tournament->year ne $year) {
	    print $INDEX '<TR><TD ROWSPAN='.$year_rowspan{$tournament->year}.'>'.$tournament->year.'</TD>'."\n";
	    $year = $tournament->year;
	} else {
	    print $INDEX '<TR>';
	}
	if ($tournament->dirname ne $tour_dirname) {
	    # Here we start printing medals from a new tournament
	    $tour_dirname = $tournament->dirname;
	    print $INDEX '<TD ROWSPAN='.$event_rowspan{$tour_dirname}.'>';
	    if ($tournament->is_major) {
		# As this is a major tournament, print it in bold
		print $INDEX '<B>'.$tournament->name.'</B></TD>'."\n";
	    } else {
		print $INDEX $tournament->name.'</TD>'."\n";
	    }
	    print $INDEX '<TD ROWSPAN='.$event_rowspan{$tournament->dirname}.'>';
	    print $INDEX '<A HREF="../../../'.$tour_dirname.'/index.htm">';
	    print $INDEX $tournament->town.'</A> ('.COUNTRY->full_name($tournament->country).')</TD>'."\n";
	}
	print $INDEX '<TD>'.$medal->print_color.'&nbsp;';
	print $INDEX '<A HREF="../../../'.$tour_dirname.$event_file;
	# use non breakable spaces for event names so that they are not splitted
	# if there is not enough place in column
	my ($ev_name) = $event->full_name;
	$ev_name =~ s/ /&nbsp;/g;
	print $INDEX $event->anchor.'">'.$ev_name.'</A></TD></TR>'."\n";
    }
    print $INDEX '</TABLE>'."\n";
}

1;
