From 10e15be6778dacaa7448433e4e548d80d07b4a30 Mon Sep 17 00:00:00 2001
From: Haakon Gunleiksrud <haakogun@stud.ntnu.no>
Date: Wed, 18 Mar 2020 12:20:52 +0100
Subject: [PATCH] #33 Added functionality for fetching games, creating buttons
 and added empty event listeners for now.

---
 .../com/gameware/game/states/MenuState.java   | 10 ++-
 .../game/states/ViewHighScoreState.java       | 84 +++++++++++--------
 2 files changed, 59 insertions(+), 35 deletions(-)

diff --git a/frontend/core/src/com/gameware/game/states/MenuState.java b/frontend/core/src/com/gameware/game/states/MenuState.java
index abf75d9..b188d4a 100644
--- a/frontend/core/src/com/gameware/game/states/MenuState.java
+++ b/frontend/core/src/com/gameware/game/states/MenuState.java
@@ -75,7 +75,13 @@ public class MenuState extends State{
         TextButton highScoreBtn = new TextButton(highScoreBtnText, skin);
         highScoreBtn.addListener(new ClickListener() {
             @Override
-            public void clicked(InputEvent e, float x, float y){ handleHighscoreBtnClick(); }
+            public void clicked(InputEvent e, float x, float y){
+                try {
+                    handleHighscoreBtnClick();
+                } catch (IOException ex) {
+                    ex.printStackTrace();
+                }
+            }
         });
         return highScoreBtn;
     }
@@ -131,7 +137,7 @@ public class MenuState extends State{
         gsm.set(new SinglePlayerSelectGameState(gsm));
     }
 
-    private void handleHighscoreBtnClick(){
+    private void handleHighscoreBtnClick() throws IOException {
         gsm.set(new ViewHighScoreState(gsm));
     }
 
diff --git a/frontend/core/src/com/gameware/game/states/ViewHighScoreState.java b/frontend/core/src/com/gameware/game/states/ViewHighScoreState.java
index 7d17cf6..7ce02c0 100644
--- a/frontend/core/src/com/gameware/game/states/ViewHighScoreState.java
+++ b/frontend/core/src/com/gameware/game/states/ViewHighScoreState.java
@@ -2,59 +2,48 @@ package com.gameware.game.states;
 
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.scenes.scene2d.InputEvent;
+import java.util.List;
 import com.badlogic.gdx.scenes.scene2d.ui.Skin;
 import com.badlogic.gdx.scenes.scene2d.ui.Table;
 import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
 import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
+import com.gameware.game.QueryIntermediate;
+import com.gameware.game.GameWare;
+import com.gameware.game.models.Game;
+import java.io.IOException;
+
+
+
+import java.io.IOException;
 
 public class ViewHighScoreState extends State {
 
     private Table table;
+    private List<Game> games;
 
     private String backBtnText = "Back";
-    private String colorRushHighScoreBtnText = "Color Rush";
-    private String bubbleWrapHighScoreBtnText = "Bubble Wrap";
 
-
-    public ViewHighScoreState(GameStateManager gsm) {
+    public ViewHighScoreState(GameStateManager gsm) throws IOException {
         super(gsm);
-        System.out.println("Du er i view high score state :0");
+
+        games = getGames();
 
         table = new Table();
         table.setFillParent(true);
 
         table.row();
-        table.add(makeBackBtn());
 
-        //TODO: autogenerer fra QI get
-        table.row();
-        TextButton colorRushBtn = new TextButton(colorRushHighScoreBtnText, skin);
-        colorRushBtn.addListener(new ClickListener() {
-            @Override
-            public void clicked(InputEvent e, float x, float y){ handleColorRushBtnClick(); }
-        });
-        table.add(colorRushBtn);
-
-        table.row();
-        TextButton bubbleWrapBtn = new TextButton(bubbleWrapHighScoreBtnText, skin);
-        bubbleWrapBtn.addListener(new ClickListener() {
-            @Override
-            public void clicked(InputEvent e, float x, float y){ handleBubbleWrapBtnClick(); }
-        });
-        table.add(bubbleWrapBtn);
+        for (Game g : games){
+            TextButton gameBtn = new TextButton(g.getName(), skin);
+            gameBtn.addListener(new ViewHighScoreState.MyClickListener(g));
+            table.add(gameBtn).spaceBottom(spacingOnBottom);
+            table.row();
+        }
 
+        table.add(makeBackBtn()).spaceBottom(spacingOnBottom);
         stage.addActor(table);
     }
 
-    private TextButton makeBackBtn(){
-        TextButton backBtn = new TextButton(backBtnText, skin);
-        backBtn.addListener(new ClickListener() {
-            @Override
-            public void clicked(InputEvent e, float x, float y){ handleBackBtnClick(); }
-        });
-        return backBtn;
-    }
-
     @Override
     protected void handleInput() {
 
@@ -85,9 +74,38 @@ public class ViewHighScoreState extends State {
         return this;
     }
 
+    private TextButton makeBackBtn(){
+        TextButton backBtn = new TextButton(backBtnText, skin);
+        backBtn.addListener(new ClickListener() {
+            @Override
+            public void clicked(InputEvent e, float x, float y){ handleBackBtnClick(); }
+        });
+        return backBtn;
+    }
+
     private void handleBackBtnClick(){
         gsm.set(new MenuState(gsm));
     }
-    private void handleColorRushBtnClick(){}
-    private void handleBubbleWrapBtnClick(){}
+
+    private void handleGameBtnClick(){
+        //TODO: This needs to take in the game object and go to viewHighScoreForGameState. For the next issue.
+    }
+
+    private static List<Game> getGames() throws IOException {
+        List<Game> games = QueryIntermediate.getGames();
+        return games;
+    }
+
+    public class MyClickListener extends ClickListener{
+        private Game game;
+
+        public MyClickListener(Game game){
+            this.game = game;
+        }
+
+        public void clicked(InputEvent event, float x, float y) {
+            //TODO: Add a new hashmap to GameWare with the gameobjects from the game id from here.
+            handleGameBtnClick();
+        };
+    }
 }
\ No newline at end of file
-- 
GitLab