#pragma semicolon 1

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

ArrayList g_aPastVIPs;
ConVar g_Cvar_CheckPastVIPs;

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);
	
	g_aPastVIPs = new ArrayList(1, 0);
	
	g_Cvar_CheckPastVIPs = CreateConVar("sm_vipmode_check", "1", "Toggles checking steam IDs of past VIPs", 0, false);

	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} {white}%N{red} 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 = 0;
	int PotentialVIPClient[64];

	for (int client = 1; client <= MaxClients; client++)
	{
		if (IsClientInGame(client) && !IsFakeClient(client) && IsPlayerAlive(client) && ZR_IsClientHuman(client))
		{
			if (g_Cvar_CheckPastVIPs.BoolValue && g_aPastVIPs.FindValue(GetSteamAccountID(client)) != -1)
				continue;

			PotentialVIPClient[PotentialVIPCount] = client;
			PotentialVIPCount++;
		}
	}

	if(PotentialVIPCount == 0)
	{
		// Try one more time while ignoring past vips
		for (int client = 1; client <= MaxClients; client++)
		{
			if (IsClientInGame(client) && !IsFakeClient(client) && IsPlayerAlive(client) && ZR_IsClientHuman(client))
			{
				PotentialVIPClient[PotentialVIPCount] = client;
				PotentialVIPCount++;
			}
		}
		
		if(PotentialVIPCount == 0)
		{
			CPrintToChatAll("{purple}VIP Mode:{red} Couldn't find a valid client to set as VIP. Aborting on this round.", g_iVIPClient);
			return;
		}
	}
	
	g_iVIPClient = PotentialVIPClient[GetRandomInt(0, PotentialVIPCount - 1)];
	
	g_aPastVIPs.Push(GetSteamAccountID(g_iVIPClient));

	CPrintToChatAll("{purple}VIP Mode:{red} {white}%N{red} is the current 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) && !IsFakeClient(player) && IsPlayerAlive(player) && ZR_IsClientHuman(player))
			{
				PotentialVIPClient[PotentialVIPCount] = player;
				PotentialVIPCount++;
			}
		}

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

		ReplyToCommand(client, "[SM] You have randomly chosen another VIP.");
		PrintToChatAll("[SM] %N randomly chose another VIP!", client);

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

	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);
		}
	}
}