#!/usr/bin/perl

# Copyright (C) 2005 John J. Chew, III <jjchew@math.utoronto.ca>
# All Rights Reserved

package TSH::Command::TotalTeamStandings;

use strict;
use warnings;

use TSH::Log;
# use TSH::Utility qw(Debug DebugOn);

# DebugOn('SP');

our (@ISA) = qw(TSH::Command);

=pod

=head1 NAME

TSH::Command::TotalTeamStandings - implement the C<tsh> TotalTeamStandings command

=head1 SYNOPSIS

  my $command = new TSH::Command::TotalTeamStandings;
  my $argsp = $command->ArgumentTypes();
  my $helptext = $command->Help();
  my (@names) = $command->Names();
  $command->Run($tournament, @parsed_arguments);
  
=head1 ABSTRACT

TSH::Command::TotalTeamStandings is a subclass of TSH::Command.

=cut

=head1 DESCRIPTION

=over 4

=cut

sub initialise ($$$$);
sub new ($);
sub Run ($$@);

=item $parserp->initialise()

Used internally to (re)initialise the object.

=cut

sub initialise ($$$$) {
  my $this = shift;
  my $path = shift;
  my $namesp = shift;
  my $argtypesp = shift;

  $this->{'help'} = <<'EOF';
Use this command to display total team standings.
EOF
  $this->{'names'} = [qw(tts totalteamstandings)];
  $this->{'argtypes'} = [qw()];
# print "names=@$namesp argtypes=@$argtypesp\n";

  return $this;
  }

sub new ($) { return TSH::Utility::new(@_); }

=item $command->Run($tournament, @parsed_args)

Should run the command in the context of the given
tournament with the specified parsed arguments.

=cut

sub Run ($$@) { 
  my $this = shift;
  my $tournament = shift;

  my $rankround0 = -1;
  my $round = 0;
  for my $dp ($tournament->Divisions()) {
    my $dround = $dp->MostScores();
    $round = $dround if $dround > $round;
    $dround = $dp->LeastScores()-1;
    $dp->ComputeRanks($dround);
    $rankround0 = $dround if $dround > $rankround0;
    }
  my $logp = new TSH::Log($tournament, undef, 'total_teams', $round);
  $logp->ColumnClasses([qw(rank wl spread wl spread wl spread name ranks)]);
  $logp->ColumnTitles(
    {
      'text' => ['Rnk', '  Won-Lost', 'Spread', ' Mean-WL',  qw(Mean-Spr W%VsO Team Mean-Rk Ranks)],
      'html' => [qw(Rank Won-Lost Spread Mean-WL Mean<br>Spread), 'Winning %<br>vs. Others', qw(Team Mean-Rank Ranks)],
    }
    );

  my %teams;
  for my $dp ($tournament->Divisions()) {
    $dp->CountTeamRecords($round-1, $rankround0, \%teams);
    }

  my $lastw = -1; my $lasts = 0; my $rank = 0; my $i = 0; my $lastl = -1;
  for my $teamname (sort { 
    my $ac = $teams{$a}{'count'};
    my $axc = $teams{$a}{'xcount'};
    my $bc = $teams{$b}{'count'};
    my $bxc = $teams{$b}{'xcount'};
    ($bxc ? $teams{$b}->{'xwins'}/$bxc : 0) <=> ($axc ? $teams{$a}->{'xwins'}/$axc : 0)
    || $teams{$b}->{'wins'}/$bc <=> $teams{$a}->{'wins'}/$ac
    || $teams{$a}->{'losses'}/$ac <=> $teams{$b}->{'losses'}/$bc
    || $teams{$b}->{'spread'}/$bc <=> $teams{$a}->{'spread'}/$ac
    } keys %teams) {
    my $teamdata = $teams{$teamname};
    $teamdata->{'xcount'} ||= 0;
    my $wins = $teamdata->{'wins'};
    my $losses = $teamdata->{'losses'};
    my $spread = $teamdata->{'spread'};
    my $count = $teamdata->{'count'};
    my $ranks = $teamdata->{'ranks'};
    $i++;
    if ($wins != $lastw || $spread != $lasts || $losses != $lastl) {
      $lastl = $losses;
      $lastw = $wins; $lasts = $spread;
      $rank = $i;
      }
    $logp->WriteRow([
      $rank,
      sprintf("%5.1f-%5.1f", $wins, $losses),
      sprintf("%+7.1f", $spread),
      sprintf("%5.2f-%5.2f", $count && $wins/$count, $count && $losses/$count),
      sprintf("%+6.1f", $count && $spread/$count),
#     sprintf("%5.1f-%5.1f", $teamdata->{'xwins'}, $teamdata->{'xlosses'}),
      sprintf("%6.2f", $teamdata->{'xcount'} && 100*$teamdata->{'xwins'}/$teamdata->{'xcount'}),
      $teamname,
      sprintf("%6.2f", $count && $teamdata->{'ranksum'}/$count),
      join(' ', sort { $a <=> $b } @$ranks),
      ]
    );
    }
  $logp->Close();
  }

=back

=cut

=head1 BUGS

None known

=cut

1;
