package eic.beike.projectx.network.projectXServer;

import android.util.Log;
import com.google.gson.Gson;
import eic.beike.projectx.network.RetrieveReader;
import eic.beike.projectx.util.Constants;
import eic.beike.projectx.util.ScoreEntry;

import java.io.BufferedReader;

/**
 * Used to interact with our database.
 * Created by alex on 10/1/15.
 */
public class Database implements IDatabase {

    /**
     * Register a name for the given id.
     *
     * @return True if registration succeded, false if it failed.
     */
    @Override
    public boolean register(String id, String name) {
        BufferedReader in = RetrieveReader.get(
                Constants.SERVER_URL+"Register.php?id="+ id + "&name=" + name
        );
        if (in != null) {
            Gson gson = new Gson();
            InsertResponse response = gson.fromJson(in, InsertResponse.class);
            Log.d(this.getClass().getSimpleName(),"Got answer from server, message:" + response.getMessage());
            return (response.getSuccess() == 1);
        }
        Log.w(this.getClass().getSimpleName(),"Failed to contact the database.");
        return false;
    }

    /**
     * @param playerId The id, not the name, of the player
     * @param bus
     * @return True if score was recorded correctly, false if it failed.
     */
    @Override
    public boolean recordScore(String playerId, int score, long time, String bus) {
        return false;
    }

    /**
     * @return The best ten scores for all players on all buses.
     */
    @Override
    public ScoreEntry[] getTopTen() {
        return new ScoreEntry[0];
    }

    /**
     * @param playerId
     * @return The best ten scores for the given player.
     */
    @Override
    public ScoreEntry[] getPlayerTopTen(String playerId) {
        return new ScoreEntry[0];
    }
}

@@ -265,16 +265,67 @@
 dReader;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -636,24 +636,48 @@
 ing name) %7B%0A
+        //Make request.%0A
         Buff
@@ -801,16 +801,94 @@
      );%0A
+        //If server connection succeeded see if the registration did as well.%0A
         
@@ -1316,30 +1316,115 @@
 %0A     * 
-@param playerI
+Record a score in the database, yet unclear when a score should be recorded.%0A     *%0A     * @param i
 d The id
@@ -1470,16 +1470,43 @@
 aram bus
+ the vin-number of the bus.
 %0A     * 
@@ -1631,23 +1631,17 @@
 (String 
-playerI
+i
 d, int s
@@ -1667,24 +1667,920 @@
 ring bus) %7B%0A
+        //We need a player id and a vin number.%0A        if(id == null %7C%7C bus == null)%7B%0A            return false;%0A        %7D%0A        //Make request.%0A        BufferedReader in = RetrieveReader.get(%0A                Constants.SERVER_URL+%22AddScore.php?%22 +%0A                        %22id=%22+ id +%0A                        %22&score=%22 + score +%0A                        %22&time=%22 + Long.toString(time) +%0A                        %22&bus=%22 + bus%0A        );%0A        //If connection established see if the score was recorded.%0A        if (in != null) %7B%0A            Gson gson = new Gson();%0A            InsertResponse response = gson.fromJson(in, InsertResponse.class);%0A            Log.d(this.getClass().getSimpleName(),%22Got answer from server, message:%22 + response.getMessage());%0A            return (response.getSuccess() == 1);%0A        %7D%0A        Log.w(this.getClass().getSimpleName(),%22Failed to contact the database.%22);%0A
         retu
@@ -2694,32 +2694,37 @@
 ride%0A    public 
+List%3C
 ScoreEntry%5B%5D get
@@ -2713,26 +2713,25 @@
 t%3CScoreEntry
-%5B%5D
+%3E
  getTopTen()
@@ -2744,32 +2744,42 @@
      return new 
+ArrayList%3C
 ScoreEntry%5B0%5D;%0A 
@@ -2764,35 +2764,35 @@
 yList%3CScoreEntry
-%5B0%5D
+%3E()
 ;%0A    %7D%0A%0A    /**
@@ -2905,16 +2905,21 @@
  public 
+List%3C
 ScoreEnt
@@ -2924,10 +2924,9 @@
 ntry
-%5B%5D
+%3E
  get
@@ -2976,16 +2976,26 @@
 urn new 
+ArrayList%3C
 ScoreEnt
@@ -3000,11 +3000,11 @@
 ntry
-%5B0%5D
+%3E()
 ;%0A  

Implement recordScore() method.