/** 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);
  }
  /**
   * awardSpecialReward. This builds the card reward based on the string data.
   *
   * @param message String, reward text to be displayed, if any
   */
  private void awardSpecialReward(String message) {
    final List<InventoryItem> itemsWon = qEvent.getCardRewardList();

    if (itemsWon == null || itemsWon.isEmpty()) {
      return;
    }

    final List<PaperCard> cardsWon = new ArrayList<>();

    for (final InventoryItem ii : itemsWon) {
      if (ii instanceof PaperCard) {
        cardsWon.add((PaperCard) ii);
      } else if (ii instanceof TournamentPack || ii instanceof BoosterPack) {
        final List<PaperCard> boosterCards = new ArrayList<>();
        SealedProduct booster;
        if (ii instanceof BoosterPack) {
          booster = (BoosterPack) ((BoosterPack) ii).clone();
          boosterCards.addAll(booster.getCards());
        } else {
          booster = (TournamentPack) ((TournamentPack) ii).clone();
          boosterCards.addAll(booster.getCards());
        }
        if (!boosterCards.isEmpty()) {
          qData.getCards().addAllCards(boosterCards);
          view.showCards("Extra " + ii.getName() + "!", boosterCards);
        }
      } else if (ii instanceof IQuestRewardCard) {
        final List<PaperCard> cardChoices = ((IQuestRewardCard) ii).getChoices();
        final PaperCard chosenCard =
            (null == cardChoices ? null : SGuiChoose.one("Choose " + ii.getName(), cardChoices));
        if (null != chosenCard) {
          cardsWon.add(chosenCard);
        }
      }
    }
    if (!cardsWon.isEmpty()) {
      if (message == null) {
        message = "Cards Won";
      }
      view.showCards(message, cardsWon);
      qData.getCards().addAllCards(cardsWon);
    }
  }
  /** 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);
  }