#include <cstrike>
#include <multicolors>
#include <sourcemod>
#include <sdkhooks>
#include <sdktools>

#pragma semicolon 1
#pragma newdecls required

public Plugin myinfo =
{
	name        = "MakoVoteSystem",
	author 	    = "Neon",
	description = "MakoVoteSystem",
	version     = "1.1",
	url         = "https://steamcommunity.com/id/n3ontm"
}

#define NUMBEROFSTAGES 6

bool g_bVoteFinished = true;
bool g_bIsRevote = false;
bool g_bStartVoteNextRound = false;
bool g_bPlayedZM = false;

bool g_bOnCooldown[NUMBEROFSTAGES];
static char g_sStageName[NUMBEROFSTAGES][32] = {"Extreme 2", "Extreme 2 (Heal + Ultima)", "Extreme 3 (ZED)", "Extreme 3 (Hellz)", "Race Mode", "Zombie Mode"};
int g_Winnerstage;

Handle g_VoteMenu = INVALID_HANDLE;
Handle g_StageList = INVALID_HANDLE;
Handle g_CountdownTimer = INVALID_HANDLE;

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name        = "MakoVoteSystem",
	author 	    = "Neon",
	description = "MakoVoteSystem",
	version     = "1.3",
	url         = "https://steamcommunity.com/id/n3ontm"
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	RegServerCmd("sm_makovote", Command_StartVote);
	HookEvent("round_start",  OnRoundStart);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnMapStart()
{
	VerifyMap();

	g_bStartVoteNextRound = false;
	g_bPlayedZM = false;

	for (int i = 0; i < NUMBEROFSTAGES; i++)
		g_bOnCooldown[i] = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action VerifyMap()
{
	char currentMap[64];
	GetCurrentMap(currentMap, sizeof(currentMap));

	if (!StrEqual(currentMap, "ze_ffvii_mako_reactor_v5_3", false))
	{
		char sFilename[256];
		GetPluginFilename(INVALID_HANDLE, sFilename, sizeof(sFilename));

		ServerCommand("sm plugins unload %s", sFilename);
	}
	else
	{
		PrecacheSound("#unloze/Pendulum - Witchcraft.mp3", true);

		AddFileToDownloadsTable("sound/unloze/Pendulum - Witchcraft.mp3");
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnEntitySpawned(int iEntity, const char[] sClassname)
{
	if (!IsValidEntity(iEntity) || g_bVoteFinished)
		return;

	char sTargetname[128];
	GetEntPropString(iEntity, Prop_Data, "m_iName", sTargetname, sizeof(sTargetname));

	if ((strcmp(sTargetname, "espad") != 0) && (strcmp(sTargetname, "ss_slow") != 0) && (strcmp(sClassname, "ambient_generic") == 0))
		AcceptEntityInput(iEntity, "Kill");
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnRoundStart(Event hEvent, const char[] sEvent, bool bDontBroadcast)
{
	if (g_bStartVoteNextRound)
	{
		g_bVoteFinished = false;
		if (!g_bPlayedZM)
		{
			CPrintToChatAll("{green}[Mako Vote] {white}ZM has not been played yet. Rolling the dice...");
			if (GetRandomInt(1, 100) <= 35)
			{
				CPrintToChatAll("{green}[Mako Vote] {white}Result: ZM, restarting round.");
				ServerCommand("sm_stage zm");
				g_bVoteFinished = true;
				g_bStartVoteNextRound = false;
				g_bPlayedZM = true;
				CS_TerminateRound(1.0, CSRoundEnd_GameStart, false);
				return;
			}
			CPrintToChatAll("{green}[Mako Vote] {white}Result: Normal vote");
		}
		else
			CPrintToChatAll("{green}[Mako Vote] {white}ZM already has been played. Starting normal vote.");

		g_CountdownTimer = CreateTimer(1.0, StartVote, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
		g_bStartVoteNextRound = false;
	}

	if (!(g_bVoteFinished))
	{
		int iStrip = FindEntityByTargetname(INVALID_ENT_REFERENCE, "RaceZone", "game_zone_player");
		if (iStrip != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iStrip, "FireUser1");

		int iButton1 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "boton", "func_button");
		if (iButton1 != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iButton1, "Lock");

		int iButton2 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "RaceMapButton1", "func_button");
		if (iButton2 != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iButton2, "Lock");

		int iButton3 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "RaceMapButton2", "func_button");
		if (iButton3 != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iButton3, "Lock");

		int iButton4 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "RaceMapButton3", "func_button");
		if (iButton4 != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iButton4, "Lock");

		int iButton5 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "RaceMapButton4", "func_button");
		if (iButton5 != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iButton5, "Lock");

		int iCounter = FindEntityByTargetname(INVALID_ENT_REFERENCE, "LevelCounter", "math_counter");
		if (iCounter != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iCounter, "Kill");

		int iFilter = FindEntityByTargetname(INVALID_ENT_REFERENCE, "humanos", "filter_activator_team");
		if (iFilter != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iFilter, "Kill");

		int iBarrerasfinal = FindEntityByTargetname(INVALID_ENT_REFERENCE, "barrerasfinal", "prop_dynamic");
		if (iBarrerasfinal != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iBarrerasfinal, "Kill");

		int iBarrerasfinal2 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "barrerasfinal2", "func_breakable");
		if (iBarrerasfinal2 != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iBarrerasfinal2, "Break");

		int iLevelText = FindEntityByTargetname(INVALID_ENT_REFERENCE, "LevelText", "game_text");
		if (iLevelText != INVALID_ENT_REFERENCE)
		{
			SetVariantString("message > INTERMISSION ROUND <");
			AcceptEntityInput(iLevelText, "AddOutput");
		}

		int iDestination = FindEntityByTargetname(INVALID_ENT_REFERENCE, "arriba2ex", "info_teleport_destination");
		if (iDestination != INVALID_ENT_REFERENCE)
		{
			SetVariantString("origin -9350 4550 100");
			AcceptEntityInput(iDestination, "AddOutput");

			SetVariantString("angles 0 -90 0");
			AcceptEntityInput(iDestination, "AddOutput");
		}

		int iTemplate1 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "EX2Laser1Temp", "point_template");
		if (iTemplate1 != INVALID_ENT_REFERENCE)
		{
			DispatchKeyValue(iTemplate1, "OnEntitySpawned", "EX2Laser1Hurt,SetDamage,0,0,-1");
			DispatchKeyValue(iTemplate1, "OnEntitySpawned", "EX2Laser1Hurt,AddOutput,OnStartTouch !activator:AddOutput:origin -7000 -1000 100:0:-1,0,-1");
		}

		int iTemplate2 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "EX2Laser2Temp", "point_template");
		if (iTemplate2 != INVALID_ENT_REFERENCE)
		{
			DispatchKeyValue(iTemplate2, "OnEntitySpawned", "EX2Laser2Hurt,SetDamage,0,0,-1");
			DispatchKeyValue(iTemplate2, "OnEntitySpawned", "EX2Laser2Hurt,AddOutput,OnStartTouch !activator:AddOutput:origin -7000 -1000 100:0:-1,0,-1");
		}

		int iTemplate3 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "EX2Laser3Temp", "point_template");
		if (iTemplate3 != INVALID_ENT_REFERENCE)
		{
			DispatchKeyValue(iTemplate3, "OnEntitySpawned", "EX2Laser3Hurt,SetDamage,0,0,-1");
			DispatchKeyValue(iTemplate3, "OnEntitySpawned", "EX2Laser3Hurt,AddOutput,OnStartTouch !activator:AddOutput:origin -7000 -1000 100:0:-1,0,-1");
		}

		int iTemplate4 = FindEntityByTargetname(INVALID_ENT_REFERENCE, "EX2Laser4Temp", "point_template");
		if (iTemplate4 != INVALID_ENT_REFERENCE)
		{
			DispatchKeyValue(iTemplate4, "OnEntitySpawned", "EX2Laser4Hurt,SetDamage,0,0,-1");
			DispatchKeyValue(iTemplate4, "OnEntitySpawned", "EX2Laser4Hurt,AddOutput,OnStartTouch !activator:AddOutput:origin -7000 -1000 100:0:-1,0,-1");
		}

		int iMusic = FindEntityByTargetname(INVALID_ENT_REFERENCE, "ss_slow", "ambient_generic");
		if (iMusic != INVALID_ENT_REFERENCE)
		{
			SetVariantString("message #unloze/Pendulum - Witchcraft.mp3");
			AcceptEntityInput(iMusic, "AddOutput");

			AcceptEntityInput(iMusic, "PlaySound");
		}

		int iTeleport = FindEntityByTargetname(INVALID_ENT_REFERENCE, "teleporte_extreme", "trigger_teleport");
		if (iTeleport != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iTeleport, "Enable");

		int iTimer = FindEntityByTargetname(INVALID_ENT_REFERENCE, "cortes2", "logic_timer");
		if (iTimer != INVALID_ENT_REFERENCE)
			AcceptEntityInput(iTimer, "Enable");
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void GenerateArray()
{
	int iBlockSize = ByteCountToCells(PLATFORM_MAX_PATH);
	g_StageList = CreateArray(iBlockSize);

	for (int i = 0; i < NUMBEROFSTAGES; i++)
		PushArrayString(g_StageList, g_sStageName[i]);

	int iArraySize = GetArraySize(g_StageList);

	for (int i = 0; i < iArraySize; i++)
	{
		int iRandom = GetRandomInt(0, iArraySize - 1);
		char sTemp1[128];
		GetArrayString(g_StageList, iRandom, sTemp1, sizeof(sTemp1));
		char sTemp2[128];
		GetArrayString(g_StageList, i, sTemp2, sizeof(sTemp2));
		SetArrayString(g_StageList, i, sTemp1);
		SetArrayString(g_StageList, iRandom, sTemp2);
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_StartVote(int args)
{
	int iCurrentStage = GetCurrentStage();

	if (iCurrentStage > 0)
		g_bOnCooldown[iCurrentStage] = true;

	if (iCurrentStage == 5)
		g_bPlayedZM = true;

	int iOnCD = 0;
	for (int i = 0; i < NUMBEROFSTAGES; i++)
	{
		if (g_bOnCooldown[i])
			iOnCD += 1;
	}

	if (iOnCD >= 4)
	{
		for (int i = 0; i < NUMBEROFSTAGES; i++)
			g_bOnCooldown[i] = false;
	}

	GenerateArray();
	g_bStartVoteNextRound = true;

	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action StartVote(Handle timer)
{
	static int iCountDown = 5;
	PrintCenterTextAll("[MakoVote] Starting Vote in %ds", iCountDown);

	if (iCountDown-- <= 0)
	{
		iCountDown = 5;
		KillTimer(g_CountdownTimer);
		g_CountdownTimer = INVALID_HANDLE;
		InitiateVote();
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void InitiateVote()
{
	if(IsVoteInProgress())
	{
		CPrintToChatAll("{green}[Mako Vote] {white}Another vote is currently in progress, retrying again in 5s.");
		g_CountdownTimer = CreateTimer(1.0, StartVote, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
		return;
	}

	Handle menuStyle = GetMenuStyleHandle(view_as<MenuStyle>(0));
	g_VoteMenu = CreateMenuEx(menuStyle, Handler_MakoVoteMenu, MenuAction_End | MenuAction_Display | MenuAction_DisplayItem | MenuAction_VoteCancel);

	int iArraySize = GetArraySize(g_StageList);
	for (int i = 0; i < iArraySize; i++)
	{
		char sBuffer[128];
		GetArrayString(g_StageList, i, sBuffer, sizeof(sBuffer));

		for (int j = 0; j < NUMBEROFSTAGES; j++)
		{
			if (strcmp(sBuffer, g_sStageName[j]) == 0)
			{
				if (g_bOnCooldown[j])
					AddMenuItem(g_VoteMenu, sBuffer, sBuffer, ITEMDRAW_DISABLED);
				else
					AddMenuItem(g_VoteMenu, sBuffer, sBuffer);
			}
		}
	}

	SetMenuOptionFlags(g_VoteMenu, MENUFLAG_BUTTON_NOVOTE);
	SetMenuTitle(g_VoteMenu, "What stage to play next?");
	SetVoteResultCallback(g_VoteMenu, Handler_SettingsVoteFinished);
	VoteMenuToAll(g_VoteMenu, 20);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public int Handler_MakoVoteMenu(Handle menu, MenuAction action, int param1, int param2)
{
	switch(action)
	{
		case MenuAction_End:
		{
			delete menu;

			if (param1 != -1)
			{
				g_bVoteFinished = true;
				float fDelay = 3.0;
				CS_TerminateRound(fDelay, CSRoundEnd_GameStart, false);
			}
		}
	}
	return 0;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public int MenuHandler_NotifyPanel(Menu hMenu, MenuAction iAction, int iParam1, int iParam2)
{
	switch (iAction)
	{
		case MenuAction_Select, MenuAction_Cancel:
			delete hMenu;
	}
}

public void Handler_SettingsVoteFinished(Handle menu, int num_votes, int num_clients, const int[][] client_info, int num_items, const int[][] item_info)
{
	int highest_votes = item_info[0][VOTEINFO_ITEM_VOTES];
	int required_percent = 60;
	int required_votes = RoundToCeil(float(num_votes) * float(required_percent) / 100);

	if ((highest_votes < required_votes) && (!g_bIsRevote))
	{
		CPrintToChatAll("{green}[MakoVote] {white}A revote is needed!");
		char sFirst[128];
		char sSecond[128];
		GetMenuItem(menu, item_info[0][VOTEINFO_ITEM_INDEX], sFirst, sizeof(sFirst));
		GetMenuItem(menu, item_info[1][VOTEINFO_ITEM_INDEX], sSecond, sizeof(sSecond));
		ClearArray(g_StageList);
		PushArrayString(g_StageList, sFirst);
		PushArrayString(g_StageList, sSecond);
		g_bIsRevote = true;
		g_CountdownTimer = CreateTimer(1.0, StartVote, INVALID_HANDLE, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);

		return;
	}

	// No revote needed, continue as normal.
	g_bIsRevote = false;
	Handler_VoteFinishedGeneric(menu, num_votes, num_clients, client_info, num_items, item_info);
}

public void Handler_VoteFinishedGeneric(Handle menu, int num_votes, int num_clients, const int[][] client_info, int num_items, const int[][] item_info)
{
	g_bVoteFinished = true;
	char sWinner[128];
	GetMenuItem(menu, item_info[0][VOTEINFO_ITEM_INDEX], sWinner, sizeof(sWinner));
	float fPercentage = float(item_info[0][VOTEINFO_ITEM_VOTES] * 100) / float(num_votes);

	CPrintToChatAll("{green}[MakoVote] {white}Vote Finished! Winner: {red}%s{white} with %d%% of %d votes!", sWinner, RoundToFloor(fPercentage), num_votes);

	for (int i = 0; i < NUMBEROFSTAGES; i++)
	{
		if (strcmp(sWinner, g_sStageName[i]) == 0)
			g_Winnerstage = i;
	}

	ServerCommand("sm_stage %d", (g_Winnerstage + 4));

	float fDelay = 3.0;
	CS_TerminateRound(fDelay, CSRoundEnd_GameStart, false);
}

public int GetCurrentStage()
{
	int iLevelCounterEnt = FindEntityByTargetname(INVALID_ENT_REFERENCE, "LevelCounter", "math_counter");

	int offset = FindDataMapInfo(iLevelCounterEnt, "m_OutValue");
	int iCounterVal = RoundFloat(GetEntDataFloat(iLevelCounterEnt, offset));

	int iCurrentStage;
	if (iCounterVal == 5)
		iCurrentStage = 0;
	else if (iCounterVal == 6)
		iCurrentStage = 5;
	else if (iCounterVal == 7)
		iCurrentStage = 1;
	else if (iCounterVal == 9)
		iCurrentStage = 3;
	else if (iCounterVal == 10)
		iCurrentStage = 2;
	else if (iCounterVal == 11)
		iCurrentStage = 4;
	else
		iCurrentStage = 0;

	return iCurrentStage;
}

public int FindEntityByTargetname(int entity, const char[] sTargetname, const char[] sClassname)
{
	if(sTargetname[0] == '#') // HammerID
	{
		int HammerID = StringToInt(sTargetname[1]);

		while((entity = FindEntityByClassname(entity, sClassname)) != INVALID_ENT_REFERENCE)
		{
			if(GetEntProp(entity, Prop_Data, "m_iHammerID") == HammerID)
				return entity;
		}
	}
	else // Targetname
	{
		int Wildcard = FindCharInString(sTargetname, '*');
		char sTargetnameBuf[64];

		while((entity = FindEntityByClassname(entity, sClassname)) != INVALID_ENT_REFERENCE)
		{
			if(GetEntPropString(entity, Prop_Data, "m_iName", sTargetnameBuf, sizeof(sTargetnameBuf)) <= 0)
				continue;

			if(strncmp(sTargetnameBuf, sTargetname, Wildcard) == 0)
				return entity;
		}
	}
	return INVALID_ENT_REFERENCE;
}