#pragma semicolon 1

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

bool g_bHideLennies[MAXPLAYERS + 1] = { false, ... };
Handle g_hCookieHideLennies = null;

bool g_bBlockCommands[MAXPLAYERS + 1] = { false, ... };
Handle g_hCookieBlockCommands = null;

bool g_bBlockChat[MAXPLAYERS + 1] = { false, ...};
Handle g_hCookieBlockChat = null;

#define NUMBEROFLENNIES 23

char g_cLennies[NUMBEROFLENNIES][] = {"( ͡° ͜ʖ ͡°)", "͜ʖ", "(° ͜ʖ °)", "( ͝͠°͜ل͝͠°)", "( ͡° ͜ ͡°)", "( ͡°╭͜ʖ╮͡° )", "( ͠° ͜ʖ ͡°)", "( ° ͜ʖ °)", "(╯°□°)╯", "_(ツ)_", "_ツ_", "( ̿°̿ ͜ل͜ ̿°̿ )", "( ͡", "( ͠", "( ͝", "( °", "(͡", "ಠ_ಠ", "͡°", "°͡", "ʖ", "͡", "͜"};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name = "ChatFilter",
	author = "Dogan",
	description = "Makes it possible to selfmute several things in chat",
	version = "2.1.0",
	url = ""
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	RegConsoleCmd("sm_cf", OnToggleSettings, "ChatFilter Settings");
	RegConsoleCmd("sm_chatfilter", OnToggleSettings, "ChatFilter Settings");

	RegConsoleCmd("sm_hide_lennies", OnToggleLennies, "Toggle blocking Lennies");
	g_hCookieHideLennies = RegClientCookie("lennies_blocked", "are lennies blocked", CookieAccess_Protected);

	RegConsoleCmd("sm_hide_commands", OnToggleCommands, "Toggle blocking Commands");
	g_hCookieBlockCommands = RegClientCookie("commands_blocked", "are commands blocked", CookieAccess_Protected);

	RegConsoleCmd("sm_hide_chat", OnToggleChat, "Toggle blocking other players Messages");
	g_hCookieBlockChat = RegClientCookie("chat_blocked", "are messages from others players blocked", CookieAccess_Protected);

	SetCookieMenuItem(MenuHandler_CookieMenu, 0, "ChatFilter");
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnToggleSettings(int client, int args)
{
	ShowSettingsMenu(client);
	return Plugin_Handled;
}

public Action OnToggleLennies(int client, int args)
{
	ToggleLennies(client);
	return Plugin_Handled;
}

public Action OnToggleCommands(int client, int args)
{
	ToggleCommands(client);
	return Plugin_Handled;
}

public Action OnToggleChat(int client, int args)
{
	ToggleChat(client);
	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void ShowSettingsMenu(int client)
{
	Menu menu = new Menu(MenuHandler_MainMenu);

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

	char sBuffer[128];

	Format(sBuffer, sizeof(sBuffer), "Hiding Lennies: %s", g_bHideLennies[client] ? "Enabled" : "Disabled");
	menu.AddItem("0", sBuffer);

	Format(sBuffer, sizeof(sBuffer), "Hiding Commands: %s", g_bBlockCommands[client] ? "Enabled" : "Disabled");
	menu.AddItem("1", sBuffer);

	Format(sBuffer, sizeof(sBuffer), "Hiding all Messages: %s", g_bBlockChat[client] ? "Enabled" : "Disabled");
	menu.AddItem("2", sBuffer);

	menu.ExitBackButton = true;

	menu.Display(client, MENU_TIME_FOREVER);
}

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

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public int MenuHandler_MainMenu(Menu menu, MenuAction action, int client, int selection)
{
	switch(action)
	{
		case(MenuAction_Select):
		{
			switch(selection)
			{
				case(0): ToggleLennies(client);
				case(1): ToggleCommands(client);
				case(2): ToggleChat(client);
			}

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

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action CCC_OnChatMessage(int client, int author, const char[] message)
{
	int index = FindCharInString(message, '!', false);
	int lennies = 0;

	for(int i = 0; i < NUMBEROFLENNIES; i++)
	{
		if(g_bHideLennies[client] && StrContains(message, g_cLennies[i], false) != -1)
		{
			lennies = 1;
		}
	}

	if(lennies == 1 || (g_bBlockCommands[client] && (index == 0 || index == 7)) || g_bBlockChat[client])
		return Plugin_Handled;

	return Plugin_Continue;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void ToggleLennies(int client)
{
	g_bHideLennies[client] = !g_bHideLennies[client];

	SetClientCookie(client, g_hCookieHideLennies, g_bHideLennies[client] ? "1" : "");
	CPrintToChat(client, "{cyan}[ChatFilter] {white}%s", g_bHideLennies[client] ? "Lennies are now hidden." : "Lennies are not hidden anymore.");
}

public void ToggleCommands(int client)
{
	g_bBlockCommands[client] = !g_bBlockCommands[client];

	SetClientCookie(client, g_hCookieBlockCommands, g_bBlockCommands[client] ? "1" : "");
	CPrintToChat(client, "{cyan}[ChatFilter] {white}%s", g_bBlockCommands[client] ? "Commands are now hidden." : "Commands are not hidden anymore.");
}

public void ToggleChat(int client)
{
	g_bBlockChat[client] = !g_bBlockChat[client];

	SetClientCookie(client, g_hCookieBlockChat, g_bBlockChat[client] ? "1" : "");
	CPrintToChat(client, "{cyan}[ChatFilter] {white}%s", g_bBlockChat[client] ? "Messages from all Players are now hidden." : "Messages from all Players are not hidden anymore.");
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientCookiesCached(int client)
{
	char sBuffer[2];

	GetClientCookie(client, g_hCookieHideLennies, sBuffer, sizeof(sBuffer));
	if(sBuffer[0] != '\0')
	{
		g_bHideLennies[client] = true;
	}
	else
	{
		g_bHideLennies[client] = false;
	}

	GetClientCookie(client, g_hCookieBlockCommands, sBuffer, sizeof(sBuffer));
	if(sBuffer[0] != '\0')
	{
		g_bBlockCommands[client] = true;
	}
	else
	{
		g_bBlockCommands[client] = false;
	}

	GetClientCookie(client, g_hCookieBlockChat, sBuffer, sizeof(sBuffer));
	if(sBuffer[0] != '\0')
	{
		g_bBlockChat[client] = true;
	}
	else
	{
		g_bBlockChat[client] = false;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
	g_bHideLennies[client] = false;
	g_bBlockCommands[client] = false;
	g_bBlockChat[client] = false;
}