#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>

#define MAXCOMMANDS 20

/* STRINGS */
char g_sDataFile[128];
char g_sVIPCommands[MAXCOMMANDS][2][128];

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name        = "UNLOZE_VIP_Menu",
	author      = "Neon",
	description = "",
	version     = "1.0",
	url         = "https://steamcommunity.com/id/n3ontm"
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	BuildPath(Path_SM, g_sDataFile, sizeof(g_sDataFile), "configs/vip_menu.cfg");
	RegAdminCmd("sm_vip", Command_VIP, ADMFLAG_CUSTOM1, "Open VIP Menu.");
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnMapStart()
{
	for (int i = 0; i < MAXCOMMANDS; i++)
	{
		for (int j = 0; j < 2; j++)
			g_sVIPCommands[i][j] = "";
	}

	int k = 0;

	if(!FileExists(g_sDataFile))
	{
		SetFailState("Config file missing!");
		return;
	}

	KeyValues hKvConfig = new KeyValues("vip_menu");

	if (!(hKvConfig.ImportFromFile(g_sDataFile)))
	{
		SetFailState("ImportFromFile() failed!");
		return;
	}

	hKvConfig.Rewind();

	if(!hKvConfig.GotoFirstSubKey())
	{
		SetFailState("GotoFirstSubKey() failed!");
		return;
	}

	do
	{
		char sSection[64];
		hKvConfig.GetSectionName(sSection, sizeof(sSection));

		char sCommand[64];
		hKvConfig.GetString("command", sCommand, sizeof(sCommand));

		if(!sCommand[0])
		{
			SetFailState("Could not find \"command\" in \"%s\"", sSection);
			return;
		}

		char sDescription[64];
		hKvConfig.GetString("description", sDescription, sizeof(sDescription));

		if(!sDescription[0])
		{
			SetFailState("Could not find \"description\" in \"%s\"", sSection);
			return;
		}

		g_sVIPCommands[k][0] = sCommand;
		g_sVIPCommands[k][1] = sDescription;
		k++;

	} while(hKvConfig.GotoNextKey(false));

	delete hKvConfig;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_VIP(int client, int iArgs)
{
	Menu menu = new Menu(MenuHandler_VIPMenu);
	menu.SetTitle("VIP Menu");

	for (int i = 0; i < MAXCOMMANDS; i++)
	{
		if(!g_sVIPCommands[i][0][0])
			break;

		menu.AddItem(g_sVIPCommands[i][0], g_sVIPCommands[i][1]);
	}

	menu.Display(client, MENU_TIME_FOREVER);

	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public int MenuHandler_VIPMenu(Menu menu, MenuAction action, int param1, int param2)
{
	switch(action)
	{
		case MenuAction_Select:
		{
			char sInfo[128];
			menu.GetItem(param2, sInfo, sizeof(sInfo));
			FakeClientCommandEx(param1, sInfo);
		}

		case MenuAction_End:
		{
			delete menu;
		}
	}
	return 0;
}