#include <sourcemod>
#include <SteamWorks>
#include <json>
#include <cstrike>

public Plugin myinfo =
{
	name			= "Toplvl",
	author			= "jenz",
	description		= "show toplvl for racetimer",
	version			= "1.0",
	url				= ""
};

char g_cTimeRecords[100][128];

public void OnPluginStart()
{
    RegConsoleCmd("sm_toplvl", Command_topLVL, "Displays top 100 players with highest level");
    OnMapStart();
}

public int OnTransferResponse(char[] sData)
{
    JSON_Object obj = json_decode(sData); //https://github.com/clugg/sm-json
    
    int counter = 0;
    while (counter < 100)
    {
        char s_counter[3];
        IntToString(counter, s_counter, sizeof(s_counter))
        JSON_Object position_obj = obj.GetObject(s_counter);
        int I_player_points = position_obj.GetInt("PlayerPoints");

        char player_name[64];
        position_obj.GetString("name", player_name, sizeof(player_name));

        Format(g_cTimeRecords[counter], sizeof(g_cTimeRecords[]), "%s LVL: %i", player_name, I_player_points/1000);
        counter++;
    }
    //just cleaning the outmost object should be enough.
    json_cleanup_and_delete(obj);
    return 0;
}

public int OnTransferComplete(Handle hRequest, bool bFailure, bool bSuccessful, EHTTPStatusCode eStatusCode)
{
    if (bFailure || !bSuccessful || eStatusCode != k_EHTTPStatusCode200OK)
    {
        delete hRequest;
        LogError("Request-Error: %d", eStatusCode);
        return 0;
    }

    SteamWorks_GetHTTPResponseBodyCallback(hRequest, OnTransferResponse);
    return 0;
}

public void OnMapStart()
{
    char sRequest[256];
    //fucking bitch ass nigger shit was not saying anything about the http response being too large for heap in sourcepawn. instead of 
    //saying something or throwing an error message the shit just kept failing sillently and it took me fucking 1-2 days before i realized
    //its because of the fucking json response being too large for sourcemod heap or something shit. fucking shit. 
    FormatEx(sRequest, sizeof(sRequest), "https://racebackend.unloze.com/racetimer_endpoints-1.0/api/timers/leaderboard/minified/0");
    Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, sRequest);
    if (!hRequest ||
        !SteamWorks_SetHTTPCallbacks(hRequest, OnTransferComplete) ||
        !SteamWorks_SetHTTPRequestContextValue(hRequest, 5) ||
        !SteamWorks_SendHTTPRequest(hRequest))
    {
        delete hRequest;
    }
}

public int MenuHandler1(Menu menu, MenuAction action, int param1, int param2)
{
    switch(action)
    {
        case MenuAction_End:
        {
          delete menu;
        }
    }
    return 0;
}

public Action Command_topLVL(int client, int args)
{
    char sTitle[64];
    Format(sTitle, sizeof(sTitle), "[UNLOZE RaceTimer] Top 100 highest levels");
    Menu menu = new Menu(MenuHandler1);
    menu.SetTitle(sTitle);
    for (int i = 0; i < sizeof(g_cTimeRecords); i++)
    {
        menu.AddItem("-1", g_cTimeRecords[i], ITEMDRAW_DISABLED);
    }
    menu.ExitButton = true;
    menu.Display(client, 0);
    return Plugin_Handled;
}