#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <clientprefs>

Cookie g_CookieUsp = null;
Cookie g_CookieM4a1 = null;
bool g_bLateLoad = false;
bool g_bM4A1Silenced[MAXPLAYERS+1] = true;
bool g_bUSPSilenced[MAXPLAYERS+1] = false;

public Plugin myinfo =
{
	name = "Auto Silencer",
	author = "pan",
	description = "",
	version = "no.",
	url = "unloze.gay"
};

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
	g_bLateLoad = late;
	return APLRes_Success;
}

public void OnPluginStart()
{
	g_CookieUsp = new Cookie("usp_silence", "uh...", CookieAccess_Private);
	g_CookieM4a1 = new Cookie("m4a1_silence", "eh...", CookieAccess_Private);
	RegConsoleCmd("sm_sm4", Command_SM4, "Toggle M4 Silencer");
	RegConsoleCmd("sm_susp", Command_SUSP, "Toggle USPS Silencer");

	if (g_bLateLoad)
	{
		for (int i = 1; i <= MaxClients; i++)
		{
			if (IsClientConnected(i) && IsClientInGame(i))
			{
				OnClientPutInServer(i);
			}
		}
	}
}

public void OnClientPutInServer(int client)
{
    SDKHook(client, SDKHook_WeaponEquip, Hook_OnWeaponEquip);
}

public Action Command_SM4(int client, int args)
{
	g_bM4A1Silenced[client] = !g_bM4A1Silenced[client];
	char buffer[2];
	Format(buffer, sizeof(buffer), "%s", g_bM4A1Silenced[client] ? "1":"0");
	g_CookieM4a1.Set(client, buffer);
	PrintToChat(client, "Toggled Silencer for M4A1 to %s", g_bM4A1Silenced[client] ? "On":"Off")

	return Plugin_Handled;
}

public Action Command_SUSP(int client, int args)
{
	g_bUSPSilenced[client] = !g_bUSPSilenced[client];

	char buffer[2];
	Format(buffer, sizeof(buffer), "%s", g_bUSPSilenced[client] ? "1":"0");
	g_CookieUsp.Set(client, buffer);

	PrintToChat(client, "Toggled Silencer for USP to %s", g_bUSPSilenced[client] ? "On":"Off")

	return Plugin_Handled;
}

public Action Hook_OnWeaponEquip(int client, int weapon)
{
	char item[20];
	GetEdictClassname(weapon, item, sizeof(item));

	if (StrEqual(item, "weapon_m4a1"))
	{
		if(g_bM4A1Silenced[client])
		{
			SetEntProp(weapon, Prop_Send, "m_bSilencerOn", 1);
			SetEntProp(weapon, Prop_Send, "m_weaponMode", 1);
		}
		else
		{
			SetEntProp(weapon, Prop_Send, "m_bSilencerOn", 0);
			SetEntProp(weapon, Prop_Send, "m_weaponMode", 0);
		}
	}

	if(StrEqual(item, "weapon_usp"))
	{
		if(g_bUSPSilenced[client])
		{
			SetEntProp(weapon, Prop_Send, "m_bSilencerOn", 1);
			SetEntProp(weapon, Prop_Send, "m_weaponMode", 1);
		}
		else
		{
			SetEntProp(weapon, Prop_Send, "m_bSilencerOn", 0);
			SetEntProp(weapon, Prop_Send, "m_weaponMode", 0);
		}
	}
}

public void OnClientCookiesCached(int client)
{
	//Get cookies
	char strUSP[2], strM4A1[2];
	g_CookieUsp.Get(client, strUSP, sizeof(strUSP));
	g_CookieM4a1.Get(client, strM4A1, sizeof(strM4A1));

	if (StrEqual(strUSP, "")) //Means the cookie has never been set. Default is Off for USP, On for M4A1.
	{
		g_bM4A1Silenced[client] = true;
		g_CookieM4a1.Set(client, "1");

		g_bUSPSilenced[client] = false;
		g_CookieUsp.Set(client, "0");

		return;
	}

	g_bM4A1Silenced[client] = StrEqual(strM4A1, "1");
	g_bUSPSilenced[client] = StrEqual(strUSP, "1");

}