#pragma semicolon 1

#include <sourcemod>
#include <sdktools>
#include <multicolors>
#include <Spectate>
#include <zombiereloaded>

int g_iVIPClient = -1;
bool g_bmotherInfect = false;

public Plugin myinfo =
{
	name = "VIP Mode",
	author = "Dogan + zaCade",
	description = "VIP Mode from Paranoid as Plugin",
	version = "1.0.0",
	url = ""
};

public void OnPluginStart()
{
	HookEvent("player_death", OnPlayerDeath);
	HookEvent("round_start", OnRoundStart);
	HookEvent("round_end", OnRoundEnd);
	HookEvent("player_team", OnPlayerTeam);

	RegConsoleCmd("sm_currentvip", WhoIsVIP);
	RegAdminCmd("sm_randomvip", PerformAdminVIPSelection, ADMFLAG_GENERIC, "Randomly chooses another alive humans as VIP");
}

public Action WhoIsVIP(int client, int args)
{
	if (g_iVIPClient == -1)
	{
		CReplyToCommand(client, "{purple}VIP Mode:{red} There currently is no VIP!");
	}
	else
	{
		CReplyToCommand(client, "{purple}VIP Mode:{red} %N is the current VIP! Protect him.", g_iVIPClient);
	}
	return Plugin_Handled;
}

public void ZR_OnClientInfected(int client, int attacker, bool motherInfect, bool respawnOverride, bool respawn)
{
	if(motherInfect && g_bmotherInfect == false)
	{
	g_bmotherInfect = true;
	CreateTimer(5.0, SelectVIP, _, TIMER_FLAG_NO_MAPCHANGE);
	}
}

public Action OnRoundStart(Event event, const char[] name, bool dontBroadcast)
{
	g_iVIPClient = -1;
	g_bmotherInfect = false;
}

public Action OnRoundEnd(Event event, const char[] name, bool dontBroadcast)
{
	g_iVIPClient = -1;
	g_bmotherInfect = false;
}

public Action SelectVIP(Handle timer)
{
	PerformVIPSelection(false);
}

public Action SlayHumans(Handle timer)
{
	PerformCTSlay();
}

public void PerformVIPSelection(bool reselect)
{
	int PotentialVIPCount;
	int PotentialVIPClient[64];

	for (int client = 1; client <= MaxClients; client++)
	{
		if (IsClientInGame(client) && IsPlayerAlive(client) && ZR_IsClientHuman(client))
		{
			PotentialVIPClient[PotentialVIPCount] = client;
			PotentialVIPCount++;
		}
	}

	g_iVIPClient = PotentialVIPClient[GetRandomInt(0, PotentialVIPCount - 1)];

	CPrintToChatAll("{purple}VIP Mode:{red} %N is the new VIP! Protect him.", g_iVIPClient);

	if (!reselect)
	{
		CPrintToChatAll("{purple}VIP Mode:{red} Everyone will die when the VIP dies!");
		CPrintToChatAll("{purple}VIP Mode:{red} Everyone will die when the VIP dies!");
		CPrintToChatAll("{purple}VIP Mode:{red} Everyone will die when the VIP dies!");
	}
}

public Action PerformAdminVIPSelection(int client, int args)
{
	if(g_iVIPClient == -1)
	{
		CReplyToCommand(client, "{purple}VIP Mode:{red} You can't choose a VIP yet.");
	}

	else
	{
		g_iVIPClient = -1;

		int PotentialVIPCount;
		int PotentialVIPClient[64];

		for (int player = 1; player <= MaxClients; player++)
		{
			if (IsClientInGame(player) && IsPlayerAlive(player) && ZR_IsClientHuman(player))
			{
				PotentialVIPClient[PotentialVIPCount] = player;
				PotentialVIPCount++;
			}
		}
		g_iVIPClient = PotentialVIPClient[GetRandomInt(0, PotentialVIPCount - 1)];

		CPrintToChatAll("{purple}VIP Mode:{red} %N is the new VIP! Protect him.", g_iVIPClient);

		CReplyToCommand(client, "{purple}VIP Mode:{red} You have randomly chosen another VIP.");
	}
	return Plugin_Handled;
}

public Action OnPlayerDeath(Event event, const char[] name, bool dontBroadcast)
{
	int client = GetClientOfUserId(GetEventInt(event, "userid"));

	if(client == g_iVIPClient)
	{
		g_iVIPClient = -1;

		CPrintToChatAll("{purple}VIP Mode:{red} The VIP died! It's over.");
		CPrintToChatAll("{purple}VIP Mode:{red} The VIP died! It's over.");
		CPrintToChatAll("{purple}VIP Mode:{red} The VIP died! It's over.");

		CreateTimer(2.0, SlayHumans, _, TIMER_FLAG_NO_MAPCHANGE);
	}
}

public void OnClientDisconnect(int client)
{
	if(client == g_iVIPClient)
	{
		g_iVIPClient = -1;
		PerformVIPSelection(true);
	}
}

public Action OnPlayerTeam(Event event, const char[] name, bool dontBroadcast)
{
	int client = GetClientOfUserId(GetEventInt(event, "userid"));

	if(client == g_iVIPClient)
    {
		g_iVIPClient = -1;
		RequestFrame(RequestFrame_Callback);
    }
}

public void OnPlayerSwitchedToSpectateByCommand(int client)
{
	if(client == g_iVIPClient)
    {
		g_iVIPClient = -1;
		RequestFrame(RequestFrame_Callback);
    }
}

public void RequestFrame_Callback(int iPacked)
{
	PerformVIPSelection(true);
}

public void PerformCTSlay()
{
	for (int player = 1; player <= MaxClients; player++)
	{
		if (IsClientInGame(player) && IsPlayerAlive(player) && ZR_IsClientHuman(player))
		{
			ForcePlayerSuicide(player);
		}
	}
}