using System;
using System.Collections;
using System.Collections.Generic;
using PRequest = Playtomic.PRequest;


[Serializable]
public class HighscoreTable
{
    public string[] tables;
    public bool success;
    public int errorcode;
}

public class PLeaderboardsTable
{
    private const string SECTION = "leaderboards";
    private const string LIST = "tables";

    public void GetTableName(Action<string, PResponse> callback)
    {
        Playtomic.Api.StartCoroutine(SendGetTableRequest(SECTION, LIST, null, callback));
    }

    private IEnumerator SendGetTableRequest(string section, string action, Dictionary<string, object> postdata,
        Action<string, PResponse> callback)
    {
        var www = PRequest.Prepare(section, action, postdata);

        yield return www;
        var response = PRequest.Process(www);

        Dictionary<string, object> data = response.json;

        string tableName = string.Empty;
        if (response.success)
        {
            if (data.ContainsKey("tables"))
            {
                var tableData = Newtonsoft.Json.JsonConvert.DeserializeObject<HighscoreTable>(www.text);
                if (tableData.tables.Length != 0)
                    tableName = tableData.tables[0];
            }
        }
        callback(tableName, response);
    }
}