#pragma semicolon 1

#include <sourcemod>
#include <sdktools>
#include <cstrike>
#include <clientprefs>
#include <multicolors>

#define MESSAGE_RADIOTEXT 1
#define MESSAGE_SENDAUDIO 2

bool g_bStopRadioSounds[MAXPLAYERS+1] = { false, ... };
bool g_bStopNadeSounds[MAXPLAYERS+1] = { false, ...};
bool g_bStopRadioSoundsHooked = false;

Handle g_hCookieStopRadio = null;
Handle g_hCookieStopNade = null;

public Plugin myinfo =
{
	name = "SelfMuteRadio",
	author = "Dogan + zaCade",
	description = "Make it possible to self mute the radio (aka ignoread via command)",
	version = "1.0.0",
	url = ""
};

public void OnPluginStart()
{
	UserMsg RadioText = GetUserMessageId("RadioText");

	if (RadioText == INVALID_MESSAGE_ID)
		SetFailState("This game does not support the \"RadioText\" UserMessage.");

	UserMsg SendAudio = GetUserMessageId("SendAudio");

	if (SendAudio == INVALID_MESSAGE_ID)
		SetFailState("This game does not support the \"SendAudio\" UserMessage.");

	RegConsoleCmd("sm_smradio", OnToggleSelfMuteRadio, "Toggle Radio Self Mute");
	//RegConsoleCmd("sm_radio", OnToggleSelfMuteRadio, "Toggle Radio Self Mute"); //GFL only
	RegConsoleCmd("sm_smradio_nades", OnToggleSelfMuteNade, "Toggle only Radio 'Fire in the hole' Self Mute");

	g_hCookieStopRadio = RegClientCookie("radio_blocked", "is the radio blocked", CookieAccess_Protected);
	g_hCookieStopNade = RegClientCookie("nades_blocked", "is the 'fire in the hole' radio blocked", CookieAccess_Protected);

	SetCookieMenuItem(MenuHandler_CookieMenu, 0, "Radio Self Mute");

	HookUserMessage(RadioText, Hook_RadioText, true);
	HookUserMessage(SendAudio, Hook_SendAudio, true);
}

public Action Hook_RadioText(UserMsg msg_id, Handle bf, const int[] players, int playersNum, bool reliable, bool init)
{
	if(!g_bStopRadioSoundsHooked)
		return Plugin_Continue;

	int dest = BfReadByte(bf);
	int client = BfReadByte(bf);

	char sSoundType[128];
	BfReadString(bf, sSoundType, sizeof(sSoundType), false);

	char sSoundName[128];
	BfReadString(bf, sSoundName, sizeof(sSoundName), false);

	char sSoundFile[128];
	BfReadString(bf, sSoundFile, sizeof(sSoundFile), false);

	// Check which clients need to be excluded.
	int[] newPlayers = new int[playersNum];
	int newPlayersNum = 0;

	for(int i = 0; i < playersNum; i++)
	{
		int player = players[i];

		if(IsClientInGame(player) && !g_bStopRadioSounds[player] && !(g_bStopNadeSounds[player] && StrContains(sSoundFile, "hole", false) != -1))
		{
			newPlayers[newPlayersNum++] = player;
		}
	}

	if (newPlayersNum == playersNum)
	{
		// No clients where excluded.
		return Plugin_Continue;
	}
	else if (newPlayersNum == 0)
	{
		// All clients were excluded and there is no need to broadcast.
		return Plugin_Handled;
	}

	DataPack pack = new DataPack();
	pack.WriteString(sSoundType);
	pack.WriteString(sSoundName);
	pack.WriteString(sSoundFile);
	pack.WriteCell(dest);
	pack.WriteCell(client);
	pack.WriteCell(newPlayersNum);

	for(int i = 0; i < newPlayersNum; i++)
	{
		pack.WriteCell(newPlayers[i]);
	}

	RequestFrame(OnRadioText, pack);

	return Plugin_Handled;
}

public void OnRadioText(DataPack pack)
{
	pack.Reset();

	char sSoundType[128];
	pack.ReadString(sSoundType, sizeof(sSoundType));

	char sSoundName[128];
	pack.ReadString(sSoundName, sizeof(sSoundName));

	char sSoundFile[128];
	pack.ReadString(sSoundFile, sizeof(sSoundFile));

	int dest = pack.ReadCell();
	int client = pack.ReadCell();
	int newPlayersNum = pack.ReadCell();

	int[] players = new int[newPlayersNum];
	int playersNum = 0;

	for(int i = 0; i < newPlayersNum; i++)
	{
		int player = pack.ReadCell();

		if(IsClientInGame(player))
		{
			players[playersNum++] = player;
		}
	}

	CloseHandle(pack);

	Handle RadioText = StartMessage("RadioText", players, playersNum, USERMSG_RELIABLE | USERMSG_BLOCKHOOKS);

	if (RadioText != INVALID_HANDLE)
	{
		BfWriteByte(RadioText, dest);
		BfWriteByte(RadioText, client);
		BfWriteString(RadioText, sSoundType);
		BfWriteString(RadioText, sSoundName);
		BfWriteString(RadioText, sSoundFile);
	}

	EndMessage();
}

public Action Hook_SendAudio(UserMsg msg_id, Handle bf, const int[] players, int playersNum, bool reliable, bool init)
{
	if(!g_bStopRadioSoundsHooked)
		return Plugin_Continue;

	char sSoundFile[128];
	BfReadString(bf, sSoundFile, sizeof(sSoundFile), false);

	// Check which clients need to be excluded.
	int[] newPlayers = new int[playersNum];
	int newPlayersNum = 0;

	for(int i = 0; i < playersNum; i++)
	{
		int player = players[i];

		if(IsClientInGame(player) && !g_bStopRadioSounds[player] && !(g_bStopNadeSounds[player] && StrContains(sSoundFile, "hole", false) != -1))
		{
			newPlayers[newPlayersNum++] = player;
		}
	}

	if (newPlayersNum == playersNum)
	{
		// No clients where excluded.
		return Plugin_Continue;
	}
	else if (newPlayersNum == 0)
	{
		// All clients were excluded and there is no need to broadcast.
		return Plugin_Handled;
	}

	DataPack pack = new DataPack();
	pack.WriteString(sSoundFile);
	pack.WriteCell(newPlayersNum);

	for(int i = 0; i < newPlayersNum; i++)
	{
		pack.WriteCell(newPlayers[i]);
	}

	RequestFrame(OnSendAudio, pack);

	return Plugin_Handled;
}

public void OnSendAudio(DataPack pack)
{
	pack.Reset();

	char sSoundFile[128];
	pack.ReadString(sSoundFile, sizeof(sSoundFile));

	int newPlayersNum = pack.ReadCell();

	int[] players = new int[newPlayersNum];
	int playersNum = 0;

	for(int i = 0; i < newPlayersNum; i++)
	{
		int player = pack.ReadCell();

		if(IsClientInGame(player))
		{
			players[playersNum++] = player;
		}
	}

	CloseHandle(pack);

	Handle SendAudio = StartMessage("SendAudio", players, playersNum, USERMSG_RELIABLE | USERMSG_BLOCKHOOKS);

	if (SendAudio != INVALID_HANDLE)
	{
		BfWriteString(SendAudio, sSoundFile);
	}

	EndMessage();
}

public Action OnToggleSelfMuteRadio(int client, int args)
{
	ToggleSelfMuteRadio(client);
	return Plugin_Handled;
}

public Action OnToggleSelfMuteNade(int client, int args)
{
	ToggleSelfMuteNade(client);
	return Plugin_Handled;
}

public Action ToggleSelfMuteRadio(int client)
{
	g_bStopRadioSounds[client] = !g_bStopRadioSounds[client];
	CheckHooks();

	SetClientCookie(client, g_hCookieStopRadio, g_bStopRadioSounds[client] ? "1" : "");
	CPrintToChat(client, "{cyan}[SelfMuteRadio] {white}%s", g_bStopRadioSounds[client] ? "You self-muted all Radio Messages and Sounds." : "You self-unmuted all Radio Messages and Sounds.");
}

public Action ToggleSelfMuteNade(int client)
{
	g_bStopNadeSounds[client] = !g_bStopNadeSounds[client];
	CheckHooks();

	SetClientCookie(client, g_hCookieStopNade, g_bStopNadeSounds[client] ? "1" : "");
	CPrintToChat(client, "{cyan}[SelfMuteRadio] {white}%s", g_bStopNadeSounds[client] ? "You self-muted 'Fire in the Hole' Radio Messages and Sounds." : "You self-unmuted 'Fire in the Hole' Radio Messages and Sounds.");
}

public void OnClientCookiesCached(int client)
{
	char sBuffer[2];

	GetClientCookie(client, g_hCookieStopRadio, sBuffer, sizeof(sBuffer));

	if(sBuffer[0] != '\0')
	{
		g_bStopRadioSounds[client] = true;
		g_bStopRadioSoundsHooked = true;
	}
	else
	{
		g_bStopRadioSounds[client] = false;
	}

	GetClientCookie(client, g_hCookieStopRadio, sBuffer, sizeof(sBuffer));

	if(sBuffer[0] != '\0')
	{
		g_bStopNadeSounds[client] = true;
		g_bStopRadioSoundsHooked = true;
	}
	else
	{
		g_bStopNadeSounds[client] = false;
	}
}

public void OnClientDisconnect(int client)
{
	g_bStopRadioSounds[client] = false;
	g_bStopNadeSounds[client] = false;
	CheckHooks();
}

public void CheckHooks()
{
	bool bShouldHook = false;

	for(int i = 1; i <= MaxClients; i++)
	{
		if(g_bStopRadioSounds[i] || g_bStopNadeSounds[i])
		{
			bShouldHook = true;
			break;
		}
	}

	g_bStopRadioSoundsHooked = bShouldHook;
}

public void ShowSettingsMenu(int client)
{
	Menu menu = new Menu(MenuHandler_MainMenu);

	menu.SetTitle("SelfMuteRadio Settings", client);

	char sBuffer[128];
	Format(sBuffer, sizeof(sBuffer), "Self-Muting all Radio: %s", g_bStopRadioSounds[client] ? "Enabled" : "Disabled");
	menu.AddItem("0", sBuffer);

	Format(sBuffer, sizeof(sBuffer), "Self-Muting Nades Radio only: %s", g_bStopNadeSounds[client] ? "Enabled" : "Disabled");
	menu.AddItem("1", sBuffer);

	menu.ExitBackButton = true;

	menu.Display(client, MENU_TIME_FOREVER);
}

public void MenuHandler_CookieMenu(int client, CookieMenuAction action, any info, char[] buffer, int maxlen)
{
	switch(action)
	{
		case(CookieMenuAction_DisplayOption):
		{
			Format(buffer, maxlen, "SelfMuteRadio", client);
		}
		case(CookieMenuAction_SelectOption):
		{
			ShowSettingsMenu(client);
		}
	}
}

public int MenuHandler_MainMenu(Menu menu, MenuAction action, int client, int selection)
{
	switch(action)
	{
		case(MenuAction_Select):
		{
			switch(selection)
			{
				case(0): ToggleSelfMuteRadio(client);
				case(1): ToggleSelfMuteNade(client);
			}

			ShowSettingsMenu(client);
		}
		case(MenuAction_Cancel):
		{
			ShowCookieMenu(client);
		}
		case(MenuAction_End):
		{
			delete menu;
		}
	}
}