package org.roshambo;

import java.util.HashMap;

public class Match {
    private int numRounds;
    private int currentRound;

    private Competitor player1;
    private Competitor player2;

    private HashMap<Integer,HashMap<String, String>> results;
    private HashMap<String, Integer> totals = new HashMap<String, Integer>();

    public Match(Competitor player1, Competitor player2, int numRounds) {
        this.player1      = player1;
        this.player2      = player2;
        this.numRounds    = numRounds;
        this.currentRound = 1;
        this.results      = new HashMap<Integer,HashMap<String, String>>();

        this.totals.put("Ties", 0);
        this.totals.put(player1.name(), 0);
        this.totals.put(player2.name(), 0);
    }

    public void fightUntilFinished() {
        while (currentRound <= numRounds) {
            fight(currentRound++);
        }
    }

    public void fightNextRound() {
        fight(currentRound++);
    }

    public Competitor getPlayer1() {
        return player1;
    }

    public Competitor getPlayer2() {
        return player2;
    }

    public HashMap<Integer,HashMap<String, String>> getResults() {
        return results;
    }

    public HashMap<String, Integer> getTotals() {
        return totals;
    }

    private void fight(int round) {
        Competitor.Throw throwP1 = player1.engage();
        Competitor.Throw throwP2 = player2.engage();

        Competitor winner = decideWinner(throwP1, throwP2);

        updateResults(round, winner, throwP1, throwP2);
        notifyCompetitors(round, winner, throwP1, throwP2);
    }

    private Competitor decideWinner(Competitor.Throw throwP1, Competitor.Throw throwP2) {
        switch (throwP1) {
            case PAPER: {
                if (throwP2 == Competitor.Throw.PAPER)         return null;
                else if (throwP2 == Competitor.Throw.SCISSORS) return player2;
                else if (throwP2 == Competitor.Throw.ROCK)     return player1;
            }
            case ROCK: {
                if (throwP2 == Competitor.Throw.PAPER)         return player2;
                else if (throwP2 == Competitor.Throw.SCISSORS) return player1;
                else if (throwP2 == Competitor.Throw.ROCK)     return null;
            }
            case SCISSORS: {
                if (throwP2 == Competitor.Throw.PAPER)         return player1;
                else if (throwP2 == Competitor.Throw.SCISSORS) return null;
                else if (throwP2 == Competitor.Throw.ROCK)     return player2;
            }
            default: {
                return null;
            }
        }
    }

    private void notifyCompetitors(int round, Competitor winner, Competitor.Throw throwP1, Competitor.Throw throwP2) {
        player1.feedback(round, winner == player1, throwP1, throwP2);
        player2.feedback(round, winner == player2, throwP2, throwP1);
    }

    private void updateResults(int round, Competitor winner, Competitor.Throw throwP1, Competitor.Throw throwP2) {
        HashMap<String, String> result = new HashMap<String, String>();

        result.put(player1.name(), throwP1.toString());
        result.put(player2.name(), throwP2.toString());

        if (winner == null) {
            totals.put("Ties", totals.get("Ties") + 1);
            result.put("WINNER", "[Tie]");
        } else {
            result.put("WINNER", winner.name());
            totals.put(winner.name(), totals.get(winner.name()) + 1);
        }

        results.put(round, result);
    }
}
@@ -543,17 +543,17 @@
 Round = 
-1
+0
 ;%0A      
@@ -648,20 +648,20 @@
 ls.put(%22
-Ties
+NONE
 %22, 0);%0A 
@@ -683,30 +683,26 @@
 als.put(
-player1.name()
+%22PLAYER_1%22
 , 0);%0A  
@@ -723,30 +723,26 @@
 als.put(
-player2.name()
+%22PLAYER_2%22
 , 0);%0A  
@@ -839,32 +839,34 @@
           fight(
+++
 currentRound++);
@@ -857,26 +857,24 @@
 currentRound
-++
 );%0A        %7D
@@ -930,16 +930,18 @@
   fight(
+++
 currentR
@@ -944,18 +944,16 @@
 entRound
-++
 );%0A    %7D
@@ -3096,22 +3096,18 @@
 put(
-player1.name()
+%22PLAYER_1%22
 , th
@@ -3148,22 +3148,18 @@
 put(
-player2.name()
+%22PLAYER_2%22
 , th
@@ -3204,16 +3204,65 @@
  == 
-null
+player1
 ) %7B%0A
+            result.put(%22WINNER%22, %22PLAYER_1%22);%0A
     
@@ -3281,20 +3281,24 @@
 ls.put(%22
-Ties
+PLAYER_1
 %22, total
@@ -3308,12 +3308,16 @@
 et(%22
-Ties
+PLAYER_1
 %22) +
@@ -3313,32 +3313,72 @@
 LAYER_1%22) + 1);%0A
+        %7D else if (winner == player2) %7B%0A
             resu
@@ -3399,13 +3399,16 @@
 %22, %22
-%5BTie%5D
+PLAYER_2
 %22);%0A
@@ -3419,71 +3419,69 @@
     
-%7D else %7B%0A            result.put(%22WINNER%22, winner.name()
+    totals.put(%22PLAYER_2%22, totals.get(%22PLAYER_2%22) + 1
 );%0A     
     
@@ -3468,39 +3468,56 @@
 _2%22) + 1);%0A     
+  
+ %7D else %7B%0A
        
+     
 totals.put(winne
@@ -3515,52 +3515,80 @@
 put(
-winner.name(), totals.get(winner.name()) + 1
+%22NONE%22, totals.get(%22NONE%22) + 1);%0A            result.put(%22WINNER%22, %22NONE%22
 );%0A 

update keys in the results hash