/** awardChallengeWin. Generates and displays win case for challenge event. */
  private void awardChallengeWin() {
    final long questRewardCredits = ((QuestEventChallenge) qEvent).getCreditsReward();

    final StringBuilder sb = new StringBuilder();
    sb.append("Challenge completed.\n\n");
    sb.append("Challenge bounty: ").append(questRewardCredits).append(" credits.");

    qData.getAssets().addCredits(questRewardCredits);

    view.showMessage(
        sb.toString(),
        "Challenge Rewards for \"" + qEvent.getTitle() + "\"",
        FSkinProp.ICO_QUEST_BOX);

    awardSpecialReward(null);
  }
  /** awardEventCredits. Generates and displays standard rewards for gameplay and skill level. */
  private void awardEventCredits() {
    // TODO use q.qdPrefs to write bonus credits in prefs file
    final StringBuilder sb = new StringBuilder();

    int credTotal;
    int credBase;
    int credGameplay = 0;
    int credUndefeated = 0;
    int credEstates;

    // Basic win bonus
    final int base = FModel.getQuestPreferences().getPrefInt(QPref.REWARDS_BASE);
    final double multiplier = qEvent.getDifficulty().getMultiplier();

    credBase = (int) (base * multiplier);

    sb.append(StringUtils.capitalize(qEvent.getDifficulty().getTitle()));
    sb.append(" opponent: ").append(credBase).append(" credits.\n");

    final int creditsForPreviousWins =
        (int)
            ((Double.parseDouble(
                    FModel.getQuestPreferences().getPref(QPref.REWARDS_WINS_MULTIPLIER))
                * qData.getAchievements().getWin()));
    credBase += creditsForPreviousWins;

    sb.append("Bonus for previous wins: ")
        .append(creditsForPreviousWins)
        .append(creditsForPreviousWins != 1 ? " credits.\n" : " credit.\n");

    // Gameplay bonuses (for each game win)
    boolean hasNeverLost = true;
    int lifeDifferenceCredits = 0;

    final LobbyPlayer localHuman = GamePlayerUtil.getQuestPlayer();
    for (final GameOutcome game : lastGame.getOutcomesOfMatch()) {
      if (!game.isWinner(localHuman)) {
        hasNeverLost = false;
        continue; // no rewards for losing a game
      }
      // Alternate win

      // final PlayerStatistics aiRating = game.getStatistics(computer.getName());
      PlayerStatistics humanRating = null;
      for (final Entry<LobbyPlayer, PlayerStatistics> kvRating : game) {
        if (kvRating.getKey().equals(localHuman)) {
          humanRating = kvRating.getValue();
          continue;
        }

        final PlayerOutcome outcome = kvRating.getValue().getOutcome();
        final GameLossReason whyAiLost = outcome.lossState;
        int altReward = getCreditsRewardForAltWin(whyAiLost);

        String winConditionName = "Unknown (bug)";
        if (game.getWinCondition() == GameEndReason.WinsGameSpellEffect) {
          winConditionName = game.getWinSpellEffect();
          altReward = getCreditsRewardForAltWin(null);
        } else {
          switch (whyAiLost) {
            case Poisoned:
              winConditionName = "Poison";
              break;
            case Milled:
              winConditionName = "Milled";
              break;
            case SpellEffect:
              winConditionName = outcome.loseConditionSpell;
              break;
            default:
              break;
          }
        }

        if (altReward > 0) {
          credGameplay += altReward;
          sb.append(
              String.format(
                  "Alternate win condition: <u>%s</u>! Bonus: %d credits.\n",
                  winConditionName, altReward));
        }
      }
      // Mulligan to zero
      final int cntCardsHumanStartedWith = humanRating.getOpeningHandSize();
      final int mulliganReward = FModel.getQuestPreferences().getPrefInt(QPref.REWARDS_MULLIGAN0);

      if (0 == cntCardsHumanStartedWith) {
        credGameplay += mulliganReward;
        sb.append(
            String.format(
                "Mulliganed to zero and still won! Bonus: %d credits.\n", mulliganReward));
      }

      // Early turn bonus
      final int winTurn = game.getLastTurnNumber();
      final int turnCredits = getCreditsRewardForWinByTurn(winTurn);

      if (winTurn == 0) {
        sb.append("Won on turn zero!");
      } else if (winTurn == 1) {
        sb.append("Won in one turn!");
      } else if (winTurn <= 5) {
        sb.append("Won by turn 5!");
      } else if (winTurn <= 10) {
        sb.append("Won by turn 10!");
      } else if (winTurn <= 15) {
        sb.append("Won by turn 15!");
      }

      if (turnCredits > 0) {
        credGameplay += turnCredits;
        sb.append(String.format(" Bonus: %d credits.\n", turnCredits));
      }

      if (game.getLifeDelta() >= 50) {
        lifeDifferenceCredits += Math.max(Math.min((game.getLifeDelta() - 46) / 4, 750), 0);
      }
    } // End for(game)

    if (lifeDifferenceCredits > 0) {
      sb.append(String.format("Life total difference: %d credits.\n", lifeDifferenceCredits));
    }

    // Undefeated bonus
    if (hasNeverLost) {
      credUndefeated += FModel.getQuestPreferences().getPrefInt(QPref.REWARDS_UNDEFEATED);
      final int reward = FModel.getQuestPreferences().getPrefInt(QPref.REWARDS_UNDEFEATED);
      sb.append(String.format("You have not lost once! Bonus: %d credits.\n", reward));
    }

    // Estates bonus
    credTotal = credBase + credGameplay + credUndefeated + lifeDifferenceCredits;
    double estateValue = 0;
    switch (qData.getAssets().getItemLevel(QuestItemType.ESTATES)) {
      case 1:
        estateValue = .1;
        break;
      case 2:
        estateValue = .15;
        break;
      case 3:
        estateValue = .2;
        break;
      default:
        break;
    }
    if (estateValue > 0) {
      credEstates = (int) (estateValue * credTotal);
      sb.append("Estates bonus (")
          .append((int) (100 * estateValue))
          .append("%): ")
          .append(credEstates)
          .append(" credits.\n");
      credTotal += credEstates;
    }

    // Final output
    String congrats = "\n";
    if (credTotal < 100) {
      congrats += "You've earned";
    } else if (credTotal < 250) {
      congrats += "Could be worse: ";
    } else if (credTotal < 500) {
      congrats += "A respectable";
    } else if (credTotal < 750) {
      congrats += "An impressive";
    } else {
      congrats += "Spectacular match!";
    }

    sb.append(String.format("%s %d credits in total.", congrats, credTotal));
    qData.getAssets().addCredits(credTotal);

    view.showMessage(sb.toString(), "Gameplay Results", FSkinProp.ICO_QUEST_GOLD);
  }