/**
  * Gets the next opponent after the current opponent is defeated. Updates Leaderboard and calls
  * RewardScreen if victorious.
  */
 public void generateNextOpponent() {
   RelativeLayout rl = (RelativeLayout) findViewById(R.id.fightLayout);
   ImageButton frfd = (ImageButton) findViewById(R.id.firstRowFirstDice);
   frfd.setTag(null);
   ImageButton frsd = (ImageButton) findViewById(R.id.firstRowSecondDice);
   frsd.setTag(null);
   ImageButton srfd = (ImageButton) findViewById(R.id.secondRowFirstDice);
   srfd.setTag(null);
   ImageButton srsd = (ImageButton) findViewById(R.id.secondRowSecondDice);
   srsd.setTag(null);
   if (opponents.size() == 0) {
     if (this.player.getUserId() != 0) {
       updateLeaderboard();
     }
     rewardsScreen();
   } else {
     if (this.player.getUserId() != 0) {
       updateLeaderboard();
     }
     Opponent next = opponents.get(0);
     currOpponet = next;
     int resId = getResources().getIdentifier(next.getLayout(), "drawable", getPackageName());
     rl.setBackgroundColor(Color.WHITE);
     rl.setBackground(ContextCompat.getDrawable(this, resId));
     resetDicePics();
     generateOperations(currOpponet.getOp());
     generateAns(level);
     opProgressBar.setProgress(currOpponet.getPercentHealthLeft());
     opHealth.setText(currOpponet.getName() + " HP: " + currOpponet.getCurrentHealth());
   }
 }
  /**
   * Method to actually do the calculations of the attack. First grab the dice that were used, do
   * the computations against the operations presented and then deal out damage. Then if an opponent
   * dies, generate a new opponent or if there is none go to the reward screen.
   *
   * @param view view of the application
   */
  public void attackClicked(View view) {
    currentElement = "Light";
    // Generate all of the stuff we need to calculate damage
    // Create the list of the dice in the first row
    ArrayList<Die> firstRow = new ArrayList<Die>();
    // Create the list of dice in the second row
    ArrayList<Die> secondRow = new ArrayList<Die>();
    // Find which dice was used in the first row first position
    ImageButton firstRowFirstDice = (ImageButton) findViewById(R.id.firstRowFirstDice);
    // Find which dice was used in the first row second position
    ImageButton firstRowSecondDice = (ImageButton) findViewById(R.id.firstRowSecondDice);
    // Find which dice was used in the second row first slot
    ImageButton secondRowFirstDice = (ImageButton) findViewById(R.id.secondRowFirstDice);
    // Find which dice was used in the second row second slot
    ImageButton secondRowSecondDice = (ImageButton) findViewById(R.id.secondRowSecondDice);
    if ((firstRowFirstDice.getTag() == null)
        || (firstRowSecondDice.getTag() == null)
        || (secondRowFirstDice.getTag() == null)
        || (secondRowSecondDice.getTag() == null)) {
      Intent healthLow = new Intent(this, HealthTooLowActivity.class);
      healthLow.putExtra("player", player);
      healthLow.putExtra("dice", true);
      startActivityForResult(healthLow, 200);
    } else {

      // Get the value of the dice as a string
      String frfd = firstRowFirstDice.getTag().toString();
      String frsd = firstRowSecondDice.getTag().toString();
      String srfd = secondRowFirstDice.getTag().toString();
      String srsd = secondRowSecondDice.getTag().toString();
      // Find the text view for the first answer
      TextView firstRowAns = (TextView) findViewById(R.id.firstRowFirstAns);
      // Find the text view for the second answer
      TextView secondRowAns = (TextView) findViewById(R.id.secondRowAns);
      TextView firstRowSecondOp = (TextView) findViewById(R.id.firstRowSecondOp);
      TextView secondRowSecondOp = (TextView) findViewById(R.id.secondRowSecondOp);
      boolean isDead = false;
      diceUsed = player.getDiceUsed();
      playerProgressBar = (ProgressBar) findViewById(R.id.playerProgressBar);
      opProgressBar = (ProgressBar) findViewById(R.id.oppProgressBar);
      generateOps = false;
      TextView firstRowFirstOp = (TextView) findViewById(R.id.firstRowFirstOp);
      TextView secondRowFirstOp = (TextView) findViewById(R.id.secondRowFirstOp);
      int firstRowFirstDiceRoll = 0;
      int firstRowSecondDiceRoll = 0;
      int secondRowFirstDiceRoll = 0;
      int secondRowSecondDiceRoll = 0;

      // Loop through the dice used array list to figure out which dice was used in which row
      for (int i = 0; i < diceUsed.size(); i++) {
        // If the dice selected equals the string value of the first row first position dice and the
        // first row array list is less than 2, add it to the first row list
        if (diceUsed.get(i).getDiceType().equals(frfd) && firstRow.size() < 2) {
          firstRow.add(diceUsed.get(i));
          // Else if the dice value is equal to the first row second position and the first row
          // array list is less than 2, add it to the first row list
        } else if (diceUsed.get(i).getDiceType().equals(frsd) && firstRow.size() < 2) {
          firstRow.add(diceUsed.get(i));
          // Else if the dice value is equal to the second row first dice and that list is less than
          // two, add it to the second row list
        } else if (diceUsed.get(i).getDiceType().equals(srfd) && secondRow.size() < 2) {
          secondRow.add(diceUsed.get(i));
          // Else if the dice value is equal to the second row second dice and that list is less
          // than two, add it to the second row list
        } else if (diceUsed.get(i).getDiceType().equals(srsd) && secondRow.size() < 2) {
          secondRow.add(diceUsed.get(i));
        }
      }
      int firstRowTotal = 0;
      int secondRowTotal = 0;

      // For every dice in the first row list, have them roll dice and add it to the total
      for (Die d : firstRow) {
        if (firstRow.indexOf(d) == 0) {
          firstRowFirstDiceRoll = d.rollDice();
          firstRowTotal += firstRowFirstDiceRoll;
        } else {
          if (firstRowFirstOp.getText().equals("+")) {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal += firstRowSecondDiceRoll;
          } else if (firstRowFirstOp.getText().equals("-")) {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal = firstRowTotal - firstRowSecondDiceRoll;

          } else if (firstRowFirstOp.getText().equals("*")) {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal = firstRowTotal * firstRowSecondDiceRoll;
          } else {
            firstRowSecondDiceRoll = d.rollDice();
            firstRowTotal = firstRowTotal / firstRowSecondDiceRoll;
          }
        }
      }
      // For every dice in the second row list, have them roll dice and add it to the total
      for (Die d : secondRow) {
        if (secondRow.indexOf(d) == 0) {
          secondRowFirstDiceRoll = d.rollDice();
          secondRowTotal += secondRowFirstDiceRoll;
        } else {
          if (secondRowFirstOp.getText().equals("+")) {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal += secondRowSecondDiceRoll;
          } else if (secondRowFirstOp.getText().equals("-")) {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal = secondRowTotal - secondRowSecondDiceRoll;

          } else if (secondRowFirstOp.getText().equals("*")) {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal = secondRowTotal * secondRowSecondDiceRoll;
          } else {
            secondRowSecondDiceRoll = d.rollDice();
            secondRowTotal = secondRowTotal / secondRowSecondDiceRoll;
          }
        }
      }
      // If the selected element is fire, add + 1 to each row
      if (currentElement.equals("Fire")) {
        firstRowTotal++;
        secondRowTotal++;
      }
      Log.i("firstrowdm", "first row damage : " + firstRowTotal);
      Log.i("firstrowdmg", "second rwo damage : " + secondRowTotal);
      if (firstRowTotal < 0) {
        firstRowTotal = 0;
      }
      if (secondRowTotal < 0) {
        secondRowTotal = 0;
      }
      this.player.updateMaxSingleDamage(firstRowTotal);
      this.player.updateMaxSingleDamage(secondRowTotal);
      this.player.updateMaxTotalDamage(firstRowTotal + secondRowTotal);
      int firstRowAttack = 0;
      int secondRowAttack = 0;
      String firstRowUser = "";
      String secondRowUser = "";
      double totalRolls = 0;
      double totalHits = 0;
      boolean isDeadOnFirstRoll = false;
      // Now check damage calulations
      // If the first row total from the dice is greater or equal to the answer
      if (firstRowTotal >= Integer.parseInt(firstRowAns.getText().toString())) {
        // do attack,
        firstRowAttack = firstRowTotal - Integer.parseInt(firstRowAns.getText().toString());
        currOpponet.takeDamage((int) (firstRowAttack * player.getDamageRate()));
        firstRowUser = "******";
        // recalc health,
        int gj2 = currOpponet.getCurrentHealth();
        opHealth.setText(currOpponet.getName() + " HP: " + currOpponet.getCurrentHealth());
        opProgressBar.setProgress(currOpponet.getPercentHealthLeft());
        this.player.updateTotalHits();
        this.player.updateTotalRolls();
        totalRolls++;
        totalHits++;
        // Check op health if dead rewards screen,
        if (currOpponet.getCurrentHealth() <= 0) {
          player.swapDiceBackToInv();
          opponents.remove(0);
          generateOps = true;
          isDeadOnFirstRoll = true;
          isDead = true;
          this.player.updateHighestAcc(totalHits / totalRolls * 100);
          generateResults(
              firstRowFirstDiceRoll,
              firstRowSecondDiceRoll,
              secondRowFirstDiceRoll,
              secondRowSecondDiceRoll,
              firstRowFirstOp.getText().toString(),
              secondRowFirstOp.getText().toString(),
              firstRowSecondOp.getText().toString(),
              secondRowSecondOp.getText().toString(),
              firstRowAns.getText().toString(),
              secondRowAns.getText().toString(),
              firstRowAttack,
              firstRowUser,
              secondRowAttack,
              secondRowUser,
              isDeadOnFirstRoll,
              isDead,
              currOpponet.getAttack());
          generateNextOpponent();
        }
        // Else if it is less than the answer
      } else {
        // do enemy attack,
        firstRowAttack = Integer.parseInt(firstRowAns.getText().toString()) - firstRowTotal;
        player.takeDamage(firstRowAttack + currOpponet.getAttack());
        firstRowUser = "******";
        // recalc health,
        playerHealth.setText("Player Health: " + player.getCurrentHealth());
        int d = player.getPercentHealthLeft();
        playerProgressBar.setProgress(player.getPercentHealthLeft());
        this.player.updateTotalRolls();
        totalRolls++;
      }

      // IF the second row total from the dice is greater or equal to the answer
      if ((secondRowTotal >= Integer.parseInt(secondRowAns.getText().toString()))
          && !(currOpponet.getCurrentHealth() < 0)
          && !(isDead)) {
        // do attack,
        secondRowAttack = secondRowTotal - Integer.parseInt(secondRowAns.getText().toString());
        currOpponet.takeDamage((int) (secondRowAttack * player.getDamageRate()));
        secondRowUser = "******";
        // recalc health,
        int o1 = currOpponet.getCurrentHealth();
        opHealth.setText(currOpponet.getName() + " HP: " + currOpponet.getCurrentHealth());
        opProgressBar.setProgress(currOpponet.getPercentHealthLeft());
        this.player.updateTotalHits();
        this.player.updateTotalRolls();
        totalRolls++;
        totalHits++;
        // Check op health if dead rewards screen,
        if (currOpponet.getCurrentHealth() <= 0) {
          player.swapDiceBackToInv();
          if (opponents.size() > 0) opponents.remove(0);
          generateOps = true;
          isDead = true;
          player.swapDiceBackToInv();
          generateResults(
              firstRowFirstDiceRoll,
              firstRowSecondDiceRoll,
              secondRowFirstDiceRoll,
              secondRowSecondDiceRoll,
              firstRowFirstOp.getText().toString(),
              secondRowFirstOp.getText().toString(),
              firstRowSecondOp.getText().toString(),
              secondRowSecondOp.getText().toString(),
              firstRowAns.getText().toString(),
              secondRowAns.getText().toString(),
              firstRowAttack,
              firstRowUser,
              secondRowAttack,
              secondRowUser,
              false,
              isDead,
              currOpponet.getAttack());
          generateNextOpponent();
        }
        // Else if it is less than the answer
      } else if (!(currOpponet.getCurrentHealth() < 0) && !(isDead)) {
        // do enemy attack,
        secondRowAttack = Integer.parseInt(secondRowAns.getText().toString()) - secondRowTotal;
        player.takeDamage(secondRowAttack + currOpponet.getAttack());
        secondRowUser = "******";
        // recalc health,
        playerHealth.setText("Player Health: " + player.getCurrentHealth());
        playerProgressBar.setProgress(player.getPercentHealthLeft());
        this.player.updateTotalRolls();
        totalRolls++;
      }

      // If at this point we have not gotten pushed to the rewards screen, lets see if the player is
      // dead
      if (player.getCurrentHealth() <= 0) {
        player.swapDiceBackToInv();
        generateResults(
            firstRowFirstDiceRoll,
            firstRowSecondDiceRoll,
            secondRowFirstDiceRoll,
            secondRowSecondDiceRoll,
            firstRowFirstOp.getText().toString(),
            secondRowFirstOp.getText().toString(),
            firstRowSecondOp.getText().toString(),
            secondRowSecondOp.getText().toString(),
            firstRowAns.getText().toString(),
            secondRowAns.getText().toString(),
            firstRowAttack,
            firstRowUser,
            secondRowAttack,
            secondRowUser,
            false,
            false,
            currOpponet.getAttack());
        generateDeathScreen();
      }
      // Else the player is alive and so is the monster, so reset the dice locations to null
      // Generate new answers
      // Generate new operations
      // Swap the dice from the players usedDice array to the inventory and keep fighting
      else if (generateOps == false) {
        // reset operations and dice
        player.swapDiceBackToInv();
        generateResults(
            firstRowFirstDiceRoll,
            firstRowSecondDiceRoll,
            secondRowFirstDiceRoll,
            secondRowSecondDiceRoll,
            firstRowFirstOp.getText().toString(),
            secondRowFirstOp.getText().toString(),
            firstRowSecondOp.getText().toString(),
            secondRowSecondOp.getText().toString(),
            firstRowAns.getText().toString(),
            secondRowAns.getText().toString(),
            firstRowAttack,
            firstRowUser,
            secondRowAttack,
            secondRowUser,
            false,
            false,
            currOpponet.getAttack());
        resetDicePics();
        generateOperations(currOpponet.getOp());
        generateAns(level);

        ImageButton frfdTag = (ImageButton) findViewById(R.id.firstRowFirstDice);
        frfdTag.setTag(null);
        ImageButton frsdTag = (ImageButton) findViewById(R.id.firstRowSecondDice);
        frsdTag.setTag(null);
        ImageButton srfdTag = (ImageButton) findViewById(R.id.secondRowFirstDice);
        srfdTag.setTag(null);
        ImageButton srsdTag = (ImageButton) findViewById(R.id.secondRowSecondDice);
        srsdTag.setTag(null);
      }
    }
  }