/*
   * @see betting_center.IBettingCenter_Spectator#goCollectTheGains(int)
   */
  @Override
  public double goCollectTheGains(int spectatorID) {
    lock.lock();
    fileLogger.log(
        CLASS_TAG, "goCollectTheGains", FunctionState.START, "spectatorID:" + spectatorID);

    while (!betManagerIsHonouringBets) waitForCondition(betManagerIsHonouringBetsCondition);
    betManagerIsHonouringBets = false;

    spectatorCollectingGain = true;
    spectatorToHonourBetID = spectatorID;
    spectatorCollectingGainConditon.signal();

    while (!betManagerHasHonourBet) waitForCondition(betManagerHasHonourBetCondition);
    betManagerHasHonourBet = false;
    fileLogger.log(CLASS_TAG, "goCollectTheGains", FunctionState.END, "spectatorID:" + spectatorID);
    lock.unlock();
    return honourBet;
  }
  /*
   * @see betting_center.IBettingCenter_Spectator#placeABet(int, int, int)
   */
  @Override
  public void placeABet(int spectatorID, int betValue, int horseToBetID) {
    lock.lock();

    fileLogger.log(
        CLASS_TAG,
        "placeABet",
        FunctionState.START,
        "Spectator:" + spectatorID + " betValue:" + betValue + " horseToBetID:" + horseToBetID);
    while (!betManagerAcceptingBets) waitForCondition(betManagerAcceptingBetsCondition);

    bet = new Bet(spectatorID, betValue, horseToBetID);
    spectatorsToBet--;
    spectatorPlaceABet = true;
    spectatorPlaceABetCondition.signal();

    fileLogger.log(
        CLASS_TAG,
        "placeABet",
        FunctionState.END,
        "Spectator:" + spectatorID + " betValue:" + betValue + " horseToBetID:" + horseToBetID);
    lock.unlock();
  }
  /*
   * @see betting_center.IBettingCenter_Manager#honourTheBets(int, java.util.TreeMap)
   */
  @Override
  public void honourTheBets(int numWinnerHorses, TreeMap<Integer, Bet> winnerSpectators) {
    lock.lock();
    fileLogger.log(
        CLASS_TAG,
        "honourTheBets",
        FunctionState.START,
        "numWinnerHorses:" + numWinnerHorses + " winnerSpectators:" + winnerSpectators);

    while (winnerSpectators.size() != 0) {

      betManagerIsHonouringBets = true;
      betManagerIsHonouringBetsCondition.signal();

      while (!spectatorCollectingGain) waitForCondition(spectatorCollectingGainConditon);
      spectatorCollectingGain = false;

      Bet bet = winnerSpectators.remove(spectatorToHonourBetID);

      // TODO: Multiply with horse odd value */
      honourBet = bet.getHorseWinOdd() * bet.getBetValue();

      betManagerHasHonourBet = true;
      betManagerHasHonourBetCondition.signal();
      fileLogger.log(
          CLASS_TAG,
          "honourTheBets",
          FunctionState.MIDDLE,
          " winnerSpectators:" + winnerSpectators);
    }
    fileLogger.log(
        CLASS_TAG,
        "honourTheBets",
        FunctionState.END,
        "numWinnerHorses:" + numWinnerHorses + " winnerSpectators:" + winnerSpectators);
    lock.unlock();
  }