#include <zombiereloaded>
#include <sourcemod>
#include <sdktools>

/* BOOLS */
bool g_bZAmmo_Enabled;
bool g_bZAmmo_Active[MAXPLAYERS+1];

bool g_bZCleanse_Enabled;
bool g_bZCleanse_Active[MAXPLAYERS+1];
bool g_bLastButtonReload[MAXPLAYERS+1];

/* CONVARS */
ConVar g_hCVar_ZAmmo_Enabled;
ConVar g_hCVar_ZAmmo_Duration;
ConVar g_hCVar_ZAmmo_Cost;

ConVar g_hCVar_ZCleanse_Enabled;
ConVar g_hCVar_ZCleanse_Duration;
ConVar g_hCVar_ZCleanse_Uses;
ConVar g_hCVar_ZCleanse_Cooldown;

/* INTEGERS */
int g_bZCleanse_Uses[MAXPLAYERS+1];
int g_iLastZCleanse_Used[MAXPLAYERS+1];

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name         = "ZSkills",
	author       = "Neon",
	description  = "Skills?!",
	version      = "1.0.2"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	g_hCVar_ZAmmo_Enabled			= CreateConVar("zr_zammo_enabled", "1", "", FCVAR_NONE, true, 0.0, true, 1.0);
	g_hCVar_ZAmmo_Duration			= CreateConVar("zr_zammo_duration", "6", "", FCVAR_NONE, true, 1.0);
	g_hCVar_ZAmmo_Cost				= CreateConVar("zr_zammo_cost", "4500", "", FCVAR_NONE, true, 1.0);
	g_bZAmmo_Enabled				= g_hCVar_ZAmmo_Enabled.BoolValue;
	g_hCVar_ZAmmo_Enabled.AddChangeHook(ConVarChanged);

	g_hCVar_ZCleanse_Enabled 		= CreateConVar("zr_zcleanse_enabled", "1", "", FCVAR_NONE, true, 0.0, true, 1.0);
	g_hCVar_ZCleanse_Duration		= CreateConVar("zr_zcleanse_duration", "4", "", FCVAR_NONE, true, 1.0);
	g_hCVar_ZCleanse_Uses 			= CreateConVar("zr_zcleanse_uses", "2", "", FCVAR_NONE, true, 1.0);
	g_hCVar_ZCleanse_Cooldown		= CreateConVar("zr_zcleanse_cooldown", "8", "", FCVAR_NONE, true, 1.0);
	g_bZCleanse_Enabled 			= g_hCVar_ZCleanse_Enabled.BoolValue;
	g_hCVar_ZCleanse_Enabled.AddChangeHook(ConVarChanged);

	HookEvent("weapon_fire", Event_WeaponFire);
	HookEvent("round_start", Event_RoundStart);

	RegConsoleCmd("sm_zammo", Command_ZAmmo);
	RegConsoleCmd("sm_zcleanse", Command_ZCleanse);
	AutoExecConfig();
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnMapStart()
{
	PrecacheSound("items/ammopickup.wav");
	PrecacheSound("unloze/extinguish.wav");
	AddFileToDownloadsTable("sound/unloze/extinguish.wav");
}


//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void ConVarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
	g_bZAmmo_Enabled = g_hCVar_ZAmmo_Enabled.BoolValue;
	g_bZCleanse_Enabled = g_hCVar_ZCleanse_Enabled.BoolValue;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_ZAmmo(int client, int args)
{
	if (client == 0)
	{
		ReplyToCommand(client, "[ZAmmo] Can't use this from console.");
		return Plugin_Handled;
	}
	if (!g_bZAmmo_Enabled)
	{
		PrintToChat(client, "[ZAmmo] is currently disabled.");
		return Plugin_Handled;
	}
	if (!IsPlayerAlive(client))
	{
		PrintToChat(client, "[ZAmmo] This feature requires you to be alive.");
		return Plugin_Handled;
	}
	if (!ZR_IsClientHuman(client))
	{
		PrintToChat(client, "[ZAmmo] This feature requires you to be Human.");
		return Plugin_Handled;
	}
	if(g_bZAmmo_Active[client])
	{
		PrintToChat(client, "[ZAmmo] is already active on you.");
		return Plugin_Handled;
	}
	int iCost = g_hCVar_ZAmmo_Cost.IntValue;
	if (GetEntProp(client, Prop_Send, "m_iAccount")  < iCost)
	{
		PrintToChat(client, "[ZAmmo] Insufficent funds (%d).", iCost);
		return Plugin_Handled;
	}
	int iDuration = g_hCVar_ZAmmo_Duration.IntValue;

	SetEntProp(client, Prop_Send, "m_iAccount", GetEntProp(client, Prop_Send, "m_iAccount") - iCost);

	SetEntPropFloat(client, Prop_Send, "m_flProgressBarStartTime", GetGameTime());
	SetEntProp(client, Prop_Send, "m_iProgressBarDuration", iDuration);
	CreateTimer(float(iDuration), Timer_Disable_ZAmmo, GetClientSerial(client), TIMER_FLAG_NO_MAPCHANGE);

	g_bZAmmo_Active[client] = true;
	PrintToChat(client, "[ZAmmo] Enabling Infinite Ammo for %d seconds in exchange for %d$.", iDuration, iCost);
	EmitSoundToAll("items/ammopickup.wav", .entity=client, .volume=0.4);
	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_ZCleanse(int client, int args)
{
	if (client == 0)
	{
		ReplyToCommand(client, "[ZCleanse] Can't use this from console.");
		return Plugin_Handled;
	}
	if (!g_bZCleanse_Enabled)
	{
		PrintToChat(client, "[ZCleanse] is currently disabled.");
		return Plugin_Handled;
	}
	if (!IsPlayerAlive(client))
	{
		PrintToChat(client, "[ZCleanse] This feature requires you to be alive.");
		return Plugin_Handled;
	}
	if (!ZR_IsClientZombie(client))
	{
		PrintToChat(client, "[ZCleanse] This feature requires you to be Zombie.");
		return Plugin_Handled;
	}
	if(g_bZCleanse_Active[client])
	{
		PrintToChat(client, "[ZCleanse] is already active on you.");
		return Plugin_Handled;
	}
	int iMaxUses = g_hCVar_ZCleanse_Uses.IntValue;
	if (g_bZCleanse_Uses[client] >= iMaxUses)
	{
		PrintToChat(client, "[ZCleanse] Already used (%d/%d) times this round.", g_bZCleanse_Uses[client], iMaxUses);
		return Plugin_Handled;
	}
	int iTimeSinceLastZCleanse = GetTime() - g_iLastZCleanse_Used[client];
	int iCooldown = g_hCVar_ZCleanse_Cooldown.IntValue;
	if (iTimeSinceLastZCleanse < iCooldown)
	{
		PrintToChat(client, "[ZCleanse] You need to wait another %d Seconds before you can use ZCleanse again.", iCooldown - iTimeSinceLastZCleanse);
		return Plugin_Handled;
	}
	int iDuration = g_hCVar_ZCleanse_Duration.IntValue;

	SetEntPropFloat(client, Prop_Send, "m_flProgressBarStartTime", GetGameTime());
	SetEntProp(client, Prop_Send, "m_iProgressBarDuration", iDuration);
	NapalmExtinguishEntity(client);
	CreateTimer(float(iDuration), Timer_Disable_ZCleanse, GetClientSerial(client), TIMER_FLAG_NO_MAPCHANGE);

	g_bZCleanse_Active[client] = true;
	g_bZCleanse_Uses[client]++;
	g_iLastZCleanse_Used[client] = GetTime();
	PrintToChat(client, "[ZCleanse] You are immune against napalm for the next %d seconds. (%d/%d) uses.", iDuration, g_bZCleanse_Uses[client], iMaxUses);
	EmitSoundToAll("unloze/extinguish.wav", .entity=client, .volume=0.4);
	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
	g_bZAmmo_Active[client] = false;
	g_bZCleanse_Active[client] = false;
	g_bZCleanse_Uses[client] = 0;
	g_iLastZCleanse_Used[client] = 0;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void Event_RoundStart(Handle hEvent, char[] name, bool dontBroadcast)
{
	for (int client = 1; client <= MaxClients; client++)
	{
		g_bZAmmo_Active[client] = false;
		g_bZCleanse_Active[client] = false;
		g_bZCleanse_Uses[client] = 0;
		g_iLastZCleanse_Used[client] = 0;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void Event_WeaponFire(Handle hEvent, char[] name, bool dontBroadcast)
{
	int client = GetClientOfUserId(GetEventInt(hEvent, "userid"));
	if(!g_bZAmmo_Active[client])
		return;

	int weapon = GetEntPropEnt(client, Prop_Data, "m_hActiveWeapon", 0);
	if(IsValidEntity(weapon))
	{
		if(weapon == GetPlayerWeaponSlot(client, 0) || weapon == GetPlayerWeaponSlot(client, 1))
		{
			if(GetEntProp(weapon, Prop_Send, "m_iState", 4, 0) == 2 && GetEntProp(weapon, Prop_Send, "m_iClip1", 4, 0))
			{
				int toAdd = 1;
				char weaponClassname[128];
				GetEntityClassname(weapon, weaponClassname, sizeof(weaponClassname));

				if(StrEqual(weaponClassname, "weapon_glock", true) || StrEqual(weaponClassname, "weapon_famas", true))
				{
					if(GetEntProp(weapon, Prop_Send, "m_bBurstMode"))
					{
						switch (GetEntProp(weapon, Prop_Send, "m_iClip1"))
						{
							case 1:
							{
								toAdd = 1;
							}
							case 2:
							{
								toAdd = 2;
							}
							default:
							{
								toAdd = 3;
							}
						}
					}
				}
				SetEntProp(weapon, Prop_Send, "m_iClip1", GetEntProp(weapon, Prop_Send, "m_iClip1", 4, 0) + toAdd, 4, 0);
			}
		}
	}

	return;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void ZR_OnClientInfected(int client, int attacker, bool motherInfect, bool respawnOverride, bool respawn)
{
	if (!g_bZAmmo_Active[client])
		return;

	SetEntPropFloat(client, Prop_Send, "m_flProgressBarStartTime", GetGameTime()-1);
	SetEntProp(client, Prop_Send, "m_iProgressBarDuration", 0);

	g_bZAmmo_Active[client] = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void NapalmExtinguishEntity(int client)
{
	int iFire = GetEntPropEnt(client, Prop_Data, "m_hEffectEntity");
	if (IsValidEntity(iFire))
	{
		char sClassName[64];
		GetEdictClassname(iFire, sClassName, sizeof(sClassName));
		if (StrEqual(sClassName, "entityflame", false))
		SetEntPropFloat(iFire, Prop_Data, "m_flLifetime", 0.0);
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action ZR_OnClientIgnite(int &client, float &duration)
{
	if(g_bZCleanse_Active[client])
		return Plugin_Handled;

	return Plugin_Continue;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Timer_Disable_ZAmmo(Handle timer, int iSerial)
{
	int client;
	if ((client = GetClientFromSerial(iSerial)) == 0)
		return;

	SetEntPropFloat(client, Prop_Send, "m_flProgressBarStartTime", GetGameTime()-1);
	SetEntProp(client, Prop_Send, "m_iProgressBarDuration", 0);

	g_bZAmmo_Active[client] = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Timer_Disable_ZCleanse(Handle timer, int iSerial)
{
	int client;
	if ((client = GetClientFromSerial(iSerial)) == 0)
		return;

	SetEntPropFloat(client, Prop_Send, "m_flProgressBarStartTime", GetGameTime()-1);
	SetEntProp(client, Prop_Send, "m_iProgressBarDuration", 0);

	g_bZCleanse_Active[client] = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnPlayerRunCmd(int client, int &buttons)
{
	if(!IsPlayerAlive(client))
		return Plugin_Continue;

	if(!ZR_IsClientZombie(client))
		return Plugin_Continue;

	if (g_bZCleanse_Active[client])
		return Plugin_Continue;

	bool bPressingReload = view_as<bool>(buttons & IN_RELOAD);
	if (!bPressingReload)
	{
		g_bLastButtonReload[client] = false;
		return Plugin_Continue;
	}

	if (!g_bLastButtonReload[client])
	{
		g_bLastButtonReload[client] = true;
		Command_ZCleanse(client, 0);
	}
	return Plugin_Continue;
}