package MultiDance;

sub new {
  my $proto=shift;
#  my $class=ref($proto) || $proto;
  my $class="MultiDance";
#  print "new: proto=$proto, class=$class\n";
  my $self={};
  my ($dance1, $dance2, $dance3, $dance4, $dance5, $numDances)=@_;

  $self->{DANCE1}=$dance1;
  $self->{DANCE2}=$dance2;
  $self->{DANCE3}=$dance3;
  $self->{DANCE4}=$dance4;
  $self->{DANCE5}=$dance5;
  $self->{LEVEL}=$dance1->getLevel();
  $self->{CONTINENT}=$dance1->getContinent();
  $self->{NUM_DANCES}=$numDances;

  if ($numDances==2) {

    $self->{NAME}=$dance1->getName().Constants::get(DELIM).
      $dance2->getName();
    $self->{CONSTANTS_NAME}=$dance1->getConstantsName().",".
      $dance2->getConstantsName();
    $self->{DANCE_ABBREV}=$dance1->getDanceAbbrev().$dance2->getDanceAbbrev();
    $self->{ABBREV}=$dance1->getAbbrev().$dance2->getDanceAbbrev();

  } elsif ($numDances==3) {

    $self->{NAME}=$dance1->getName().Constants::get(DELIM).
      $dance2->getName().Constants::get(DELIM).$dance3->getName();
    $self->{CONSTANTS_NAME}=$dance1->getConstantsName().",".
      $dance2->getConstantsName().",".$dance3->getConstantsName();
    $self->{DANCE_ABBREV}=$dance1->getDanceAbbrev().$dance2->getDanceAbbrev().
      $dance3->getDanceAbbrev();
    $self->{ABBREV}=$dance1->getAbbrev().$dance2->getAbbrev().
      $dance3->getAbbrev();

  } elsif ($numDances==4) {

    $self->{NAME}=$dance1->getName().Constants::get(DELIM).
      $dance2->getName().Constants::get(DELIM).
	$dance3->getName().Constants::get(DELIM).
	  $dance4->getName();
    $self->{CONSTANTS_NAME}=$dance1->getConstantsName().",".
      $dance2->getConstantsName().",".$dance3->getConstantsName().",".
	$dance4->getConstantsName();
    $self->{DANCE_ABBREV}=$dance1->getDanceAbbrev().$dance2->getDanceAbbrev().
      $dance3->getDanceAbbrev().$dance4->getDanceAbbrev();
    $self->{ABBREV}=$dance1->getAbbrev().$dance2->getAbbrev().
      $dance3->getAbbrev().$dance4->getAbbrev();

  } elsif ($numDances==5) {

    $self->{NAME}=$dance1->getName().Constants::get(DELIM).
      $dance2->getName().Constants::get(DELIM).
	$dance3->getName().Constants::get(DELIM).
	  $dance4->getName().Constants::get(DELIM).
	    $dance5->getName();
    $self->{CONSTANTS_NAME}=$dance1->getConstantsName().",".
      $dance2->getConstantsName().",".$dance3->getConstantsName().",".
	$dance4->getConstantsName().",".$dance5->getConstantsName();
    $self->{DANCE_ABBREV}=$dance1->getDanceAbbrev().$dance2->getDanceAbbrev().
      $dance3->getDanceAbbrev().$dance4->getDanceAbbrev().
	$dance5->getDanceAbbrev();
    $self->{ABBREV}=$dance1->getAbbrev().$dance2->getAbbrev().
      $dance3->getAbbrev().$dance4->getAbbrev().$dance5->getAbbrev();
  }

  if ($self->{LEVEL} eq Constants::get(TEAMMATCH)) {
    $self->{STYLE}=Constants::get(FUN_STYLE);
  } else {
    $self->{STYLE}=$dance1->getStyle();
  }

#  $self->{FAST_COMPARE}=$self->{ABBREV};
  bless($self, $class);
  return($self);
}

# Used by Constants.
sub new2 {
  my $proto=shift;
  my $class=ref($proto) || $proto;
  my $self={};
  my ($level, $continent, $name1, $name2)=@_;
  $self=MultiDance->new(SingleDance->new($level, $continent, $name1),
	     SingleDance->new($level, $continent, $name2),
	     null, null, null, 2);

  bless($self, $class);
#  print "new2; blessing $class\n";
  return($self);
}

sub new3 {
  my $proto=shift;
  my $class=ref($proto) || $proto;
  my $self={};
  my ($level, $continent, $name1, $name2, $name3)=@_;
  $self=MultiDance->new(SingleDance->new($level, $continent, $name1),
	     SingleDance->new($level, $continent, $name2),
	     SingleDance->new($level, $continent, $name3),
	     null, null, 3);

  bless($self, $class);
#  print "new3; blessing $class\n";
  return($self);
}

sub new4 {
  my $proto=shift;
  my $class=ref($proto) || $proto;
  my $self={};
  my ($level, $continent, $name1, $name2, $name3, $name4)=@_;
  my ($sd1, $sd2, $sd3, $sd4);
  $self=MultiDance->new(SingleDance->new($level, $continent, $name1),
	     SingleDance->new($level, $continent, $name2),
	     SingleDance->new($level, $continent, $name3),
	     SingleDance->new($level, $continent, $name4), null, 4);

  bless($self, $class);
#  print "new4; blessing $class\n";
  return($self);
}

sub new5 {
  my $proto=shift;
  my $class=ref($proto) || $proto;
  my $self={};
  my ($level, $continent, $name1, $name2, $name3, $name4, $name5)=@_;
  $self=MultiDance->new(SingleDance->new($level, $continent, $name1),
	     SingleDance->new($level, $continent, $name2),
	     SingleDance->new($level, $continent, $name3),
	     SingleDance->new($level, $continent, $name4),
	     SingleDance->new($level, $continent, $name5), 5);

  bless($self, $class);
#  print "new5; blessing $class\n";
  return($self);
}

sub newFromDB {
  my $proto=shift;
  my $class=ref($proto) || $proto;
  my ($IN, $numDances)=@_;
  my $self={};
  my ($dance1, $dance2, $dance3, $dance4, $dance5,@data, $i);
  
  @data=();
  for ($i=0;$i<3;$i++) {
#    $_=shift(@$IN);  chop;  
    $_=<$IN>;  chop;  
    push(@data, $_);
  }
  $dance1=SingleDance->new(@data);

  @data=();
  for ($i=0;$i<3;$i++) {
#    $_=shift(@$IN);  chop;  push(@data, $_);
    $_=<$IN>;  chop;  push(@data, $_);
  }
  $dance2=SingleDance->new(@data);

  if ($numDances>2) {
    @data=();
    for ($i=0;$i<3;$i++) {
#	$_=shift(@$IN);  chop;  push(@data, $_);
      $_=<$IN>;  chop;  push(@data, $_);
    }
    $dance3=SingleDance->new(@data);
  }
  if ($numDances>3) {
    if ($numDances>3) {
      @data=();
      for ($i=0;$i<3;$i++) {
#	  $_=shift(@$IN);  chop;  push(@data, $_);
	$_=<$IN>;  chop;  push(@data, $_);
      }
      $dance4=SingleDance->new(@data);
    }
    if ($numDances>4) {
      if ($numDances>4) {
	@data=();
	for ($i=0;$i<3;$i++) {
#	    $_=shift(@$IN);  chop;  push(@data, $_);
	  $_=<$IN>;  chop;  push(@data, $_);
	}
	$dance5=SingleDance->new(@data);
      }
    }
  }

  $self=MultiDance->new($dance1, $dance2, $dance3, $dance4, $dance5, $numDances);
  bless($self, $class);
#  print "newFromDB; blessing $class\n";
  return($self);
}

# Methods

# Dance interface methods
sub equals {
  my ($self, $doppel)=@_;

#General::log("md eq:".
#	     $self->{LEVEL}."\t".$doppel->{LEVEL}."\n".
#	     $self->{CONTINENT}."\t".$doppel->{CONTINENT}."\n".
#	     $self->{CONSTANTS_NAME}."\t".$doppel->{CONSTANTS_NAME});

  if ($self->{LEVEL} eq $doppel->{LEVEL}) {
    if ($self->{CONTINENT} eq $doppel->{CONTINENT}) {
      if ($self->{CONSTANTS_NAME} eq $doppel->{CONSTANTS_NAME}) {
	return(1);
      }
    }
  }
  return(0);
}

sub getAbbrev {
  my $self=shift;
  return($self->{ABBREV});
}

sub getContinent {
  my $self=shift;
  return($self->{CONTINENT});
}

sub getDance {
  my $self=shift;
  my $i=@_[0];
  if ($i==1) {
    return($self->{DANCE1});
  } elsif ($i==2) {
    return($self->{DANCE2});
  } elsif ($i==3) {
    return($self->{DANCE3});
  } elsif ($i==4) {
    return($self->{DANCE4});
  } elsif ($i==5) {
    return($self->{DANCE5});
  }
}

sub getDanceAbbrev {
  my $self=shift;
  return($self->{DANCE_ABBREV});
}

sub getLevel {
  my $self=shift;
  return($self->{LEVEL});
}

#   // We do it here, because in general, we'll so very rarely need to do this
#   // kind of grovelling.  So pay it each time we have to.
sub getLevelExp {
  my $self=shift;
  my $level=$self->{LEVEL};

  if ($level eq NEWCOMER) {
    return(Constants::get(NEWCOMER_EXP));
  } elsif ($level eq BEGINNER) {
    return(Constants::get(BEGINNER_EXP));
  } elsif ($level eq INTERMEDIATE) {
    return(Constants::get(INTERMEDIATE_EXP));
  } elsif ($level eq ADVANCED) {
    return(Constants::get(ADVANCED_EXP));
  } elsif ($level eq OPEN) {
    return(Constants::get(OPEN_EXP));
  } elsif ($level eq PRE_BRONZE) {
    return(Constants::get(PRE_BRONZE_EXP));
  } elsif ($level eq BRONZE) {
    return(Constants::get(BRONZE_EXP));
  } elsif ($level eq SILVER) {
    return(Constants::get(SILVER_EXP));
  } elsif ($level eq GOLD) {
    return(Constants::get(GOLD_EXP));
  } elsif ($level eq NOVICE) {
    return(Constants::get(NOVICE_EXP));
  } elsif ($level eq PRE_CHAMP) {
    return(Constants::get(PRE_CHAMP_EXP));
  } elsif ($level eq CHAMP) {
    return(Constants::get(CHAMP_EXP));
  } elsif ($level eq TEAMMATCH) {
    return(Constants::get(TEAMMATCH_EXP));
  } else {
    Error::Croak("Unexpected level in MultiDance::getLevelExp():".$level);
  }
}

#sub getConstantsName {
#  my $self=shift;
#  return($self->{CONSTANTS_NAME});
#}

sub getCanonicalFullName {
  my $self=shift;
  if ($self==null) {
    return;
  } else {
    return($self->{LEVEL}." ".$self->{CONTINENT}." ".$self->{NAME});
  }
}

sub getFullName {
  my $self=shift;
  if ($self==null) {
    return;
  } else {
    return(Constants::get($self->{LEVEL})." ".
	   Constants::get($self->{CONTINENT})." ".$self->{NAME});
  }
}

sub getName {
  my $self=shift;
  if ($self==null) {
    return;
  } else {
    return($self->{NAME});
  }
}

sub getNumDances {
  my $self=shift;
  return($self->{NUM_DANCES});
}

sub getStyle {
  my $self=shift;
  return($self->{STYLE});
}

sub getStyle2 {
  my $self=shift;
  my $style=$self->{STYLE};
  my $continent=$self->{CONTINENT};
  if (($style eq ESQUE) && ($continent eq INTERNATIONAL)) {
    return(Constants::get(LATIN));
  } elsif (($style eq ESQUE) && ($continent eq AMERICAN)) {
    return(Constants::get(RHYTHM));
  } elsif (($style eq MODERN) && ($continent eq INTERNATIONAL)) {
    return(Constants::get(STANDARD));
  } elsif (($style eq MODERN) && ($continent eq AMERICAN)) {
    return(Constants::get(SMOOTH));
  } else {
    return(Constants::get(OTHER));
  }
}

sub write {
  my $self=shift;
  my $LOG=@_[0];
  my ($numDances, $i, $tmpdance);

  $numDances=$self->getNumDances();
  print $LOG "$numDances\n";
  for ($i=1;$i<=$numDances;$i++) {
    $tmpdance=$self->getDance($i);
    print $LOG $tmpdance->getLevel()."\n";
    print $LOG $tmpdance->getContinent()."\n";
    print $LOG $tmpdance->getConstantsName()."\n";
  }
}

1;

