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;
import java.util.ArrayList;
import java.util.List;

/**
 * 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) {
        //Make request.
        BufferedReader in = RetrieveReader.get(
                Constants.SERVER_URL+"Register.php?id="+ id + "&name=" + name
        );
        //If server connection succeeded see if the registration did as well.
        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;
    }

    /**
     * Record a score in the database, yet unclear when a score should be recorded.
     *
     * @param id The id, not the name, of the player
     * @param bus the vin-number of the bus.
     * @return True if score was recorded correctly, false if it failed.
     */
    @Override
    public boolean recordScore(String id, int score, long time, String bus) {
        //We need a player id and a vin number.
        if(id == null || bus == null){
            return false;
        }
        //Make request.
        BufferedReader in = RetrieveReader.get(
                Constants.SERVER_URL+"AddScore.php?" +
                        "id="+ id +
                        "&score=" + score +
                        "&time=" + Long.toString(time) +
                        "&bus=" + bus
        );
        //If connection established see if the score was recorded.
        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;
    }

    /**
     * @return The best ten scores for all players on all buses.
     */
    @Override
    public List<ScoreEntry> getTopTen() {
        return new ArrayList<ScoreEntry>();
    }

    /**
     * @param playerId
     * @return The best ten scores for the given player.
     */
    @Override
    public List<ScoreEntry> getPlayerTopTen(String playerId) {
        return new ArrayList<ScoreEntry>();
    }
}

@@ -99,16 +99,58 @@
 n.Gson;%0A
+import com.google.gson.reflect.TypeToken;%0A
 import e
@@ -187,24 +187,24 @@
 ieveReader;%0A
-
 import eic.b
@@ -304,24 +304,78 @@
 eredReader;%0A
+import java.io.Reader;%0Aimport java.lang.reflect.Type;%0A
 import java.
@@ -464,35 +464,20 @@
 %0A * 
-Created by alex on 10/1/15.
+@Author alex
 %0A */
@@ -2677,32 +2677,87 @@
 %0A    %7D%0A%0A    /**%0A
+     * Get the ten best scores in the database.%0A     *%0A
      * @return T
@@ -2881,266 +2881,1565 @@
     
-return new ArrayList%3CScoreEntry%3E();%0A    %7D%0A%0A    /**%0A     * @param playerId%0A     * @return The best ten scores for the given player.%0A     */%0A    @Override%0A    public List%3CScoreEntry%3E getPlayerTopTen(String playerId) %7B%0A        return new ArrayList%3CScoreEntry%3E()
+//Make request.%0A        BufferedReader in = RetrieveReader.get(%0A                Constants.SERVER_URL+%22GetHighscore.php%22%0A        );%0A%0A        //Parse the result%0A        return parseResultsFromReader(in);%0A    %7D%0A%0A    /**%0A     * Get the ten best scores for a specific player.%0A     *%0A     * @param playerId the id of the player that we want the high score for.%0A     * @return An list of ScoreEntry:s of the players best scores.%0A     */%0A    @Override%0A    public List%3CScoreEntry%3E getPlayerTopTen(String playerId) %7B%0A        //Make request.%0A        BufferedReader in = RetrieveReader.get(%0A                Constants.SERVER_URL+%22GetHighscore.php?id=%22 + playerId%0A        );%0A%0A        //Parse the result%0A        return parseResultsFromReader(in);%0A    %7D%0A%0A    /**%0A     *  Helper method that converts the json to an array of ScoreEntry:s%0A     *%0A     *  @param in reader hooked to a response from the GetHighscore.php script.%0A     *  @return%0A     */%0A    private List%3CScoreEntry%3E parseResultsFromReader(Reader in)%7B%0A%0A        ArrayList%3CScoreEntry%3E results = new ArrayList%3CScoreEntry%3E();%0A%0A        //If server connection succeeded try to get an array of ScoreEntry:s%0A        if (in != null) %7B%0A            Gson gson = new Gson();%0A            Type listType = new TypeToken%3CArrayList%3CScoreEntry%3E%3E() %7B%7D.getType();%0A            results = gson.fromJson(in, listType);%0A            Log.d(this.getClass().getSimpleName(),%22Got answer from server.%22);%0A            return results;%0A        %7D%0A        Log.w(this.getClass().getSimpleName(),%22Failed to contact the database.%22);%0A        return results
 ;%0A  

Implement getters for database scorebord.