package com.hyperling.apps.games;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * Created by ling on 1/3/17.
 * Get Unique Device ID: http://stackoverflow.com/questions/16869482/how-to-get-unique-device-hardware-id-in-android#16869491
 */

public class LeaderboardHelper {

    private final String baseURL = "https://hyperling.com:1337/hyper_games/";
    private String getVariables = "";



    public void addVariable(String key, String value){
        if (getVariables.length() == 0) {
            getVariables += "?";
        }
        else{
            getVariables += "&";
        }

        getVariables += key + "=" + value;
    }

    public void clearVariables(){
        getVariables = "";
    }

    public String getTable(String id){
        String newURL = baseURL + "?leaderboard_id=" + id;
        String html = "";

        URL url;
        InputStream is = null;
        BufferedReader br;
        String line;

        try {
            url = new URL(newURL);
            is = url.openStream();  // throws an IOException
            br = new BufferedReader(new InputStreamReader(is));

            while ((line = br.readLine()) != null) {
                html = html.concat(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException ioe) {
                // nothing to see here
            }
        }

        return html;

    }

}
