package Number;

my @taken_numbers=(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); # 10 false's
my ($hundreds, $tens, $ones);
  
sub automatically_assign_numbers {

  my ($i, $school, $firsttime, $data_ref, $looped, $j, @sortvec,
      $schools_ref, $comps_ref, $vec_ref, $comp);
  my $debug=1;

  if (Constants::get(REGISTRATION_OPEN)) {
    Error::RegistrationStillOpen();
  }
  General::log("Number:automatically_assign_numbers\n");

  
  # OK, here's the theory.  The biggest school gets the 0's.  The next
  # biggest gets the 5's.  The next gets the 1's, etc.
  # If there are more than 10 schools, then we'll need to double up.
  
  # Now, in reality, we're going to start with the smallest school and
  # double them up as we can.
  
  # We need to figure out how many people are in each school and sort on that.
  $schools_ref=Database::getSchools();
  General::log("sizeof schools: ".$#$schools_ref);
  for ($i=0;$i<=$#$schools_ref;$i++) {
    $school=$$schools_ref[$i];
    $comps_ref=Database::getCompetitorsBySchool($school);
    General::log("school: $school\tnumber=$#$comps_ref (1)") if $debug;

    # we have to my it here, so we keep getting a new one.
    my (@data, $tmp_ref);

    $tmp_ref=[];
    @data=($school, $comps_ref);
    if ($sortvec[$#$comps_ref]!=0) {
      $tmp_ref=$sortvec[$#$comps_ref];
    }
    push(@$tmp_ref, \@data);
    $sortvec[$#$comps_ref]=$tmp_ref;
  }

  # Get a number sorted array of couples.
  @data=();
  
  $firsttime=1;
  for ($key=0;$key<=$#sortvec;$key++) {
    next if ($sortvec[$key]==0);

    $tmp_ref=$sortvec[$key];
    General::log("\n**** key: $key\tnumber of schools at this key: ".$#$tmp_ref.
		 "\n") if $debug;
    for ($i=0;$i<=$#$tmp_ref;$i++) {
      my $data_ref=$$tmp_ref[$i];
      $school=$$data_ref[0];
      my $comps_ref=$$data_ref[1];
      General::log("     school: $school\tnumber=$#$comps_ref (2)") if $debug;
# print "     school: $school\tnumber=$#$comps_ref (2)\n";

      if ($firsttime) {
	$hundreds=Constants::get(STARTING_NUMBER)/100; # will int math work?
	$tens=0;
	$ones=0;
	$taken_numbers[$ones]=1;
	$firsttime=0;
      } else {
	if ($#comps > ((Constants::get(LAST_NUMBER)-
			Constants::get(STARTING_NUMBER)) / 10 - 
		       ($hundreds*10+$tens))) {
	  # OK, if we can squeeze in the next Vector of people into the
	  # remaining slots, go us.
	  # Unfortunately, we can't, so we'll zero
	  # out our numbers and try again.

	  $hundreds=Constants::get(STARTING_NUMBER)/100;
	  $tens=0;
	  $ones+=5;
	  if ($ones>9) {
	    $ones-=10;
	  }
	  $looped=0;
	  while ($taken_numbers[$ones]) {
	    $ones++;
	    if ($ones>9) {
	      if ($looped) {
		Error::NotEnoughNumbers();
	      } else { # i'm pretty sure we'll still screwed, but hey
		$looped=1;
		$ones=0;
	      }
	    }
	  }
	  $taken_numbers[$ones]=1;
	} # if
      } # else

      for ($j=0;$j<=$#$comps_ref;$j++) {
	$comp=$$comps_ref[$j];
	if ($comp->getNumber()==-1) {
	  if (Database::isLeader($comp)) {
	    $nextnumber=&getNextNumber();
	    $comp->assignNumber($nextnumber);
	    General::log("assigned $nextnumber to ".$comp->getName()) if
	      $debug;
# print "assigned $nextnumber to ".$comp->getName()."\n";
	  } else {
# print "not a leader: ".$comp->getName()."\n";
	  }
	}
      } # for ($j=0...

    } # for ($i=0;$i<=$#tmp...
  } # foreach $key
  return(1);
} # generateNumbers()

sub getNextNumber {
  my ($retval, $looped);

  $retval=$hundreds*100+$tens*10+$ones;
  if ($tens==9) {
    $tens=0;
    $hundreds++;
  } else {
    $tens++;
  }
  if (($hundreds*100)>Constants::get(LAST_NUMBER)) {
    $hundreds=Constants::get(STARTING_NUMBER)/100;
    $tens=0;
    $ones+=5;
    if ($ones>9) {
      $ones-=10;
    }
    $looped=0;
    while ($taken_numbers[$ones]) {
      $ones++;
      if ($ones>9) {
	if ($looped) {
	  Error::NotEnoughNumbers();
	} else { # i'm pretty sure we'll still screwed, but hey
	  $looped=1;
	  $ones=0;
	}
      }
    }
    $taken_numbers[$ones]=1;
  }
  return($retval);
} # getNextNumber()

sub manually_assign_numbers {
}

1;
