#pragma semicolon 1

#include <sourcemod>

#undef REQUIRE_PLUGIN
#include <HappyHour>
#include <StageDisplay>
#define REQUIRE_PLUGIN

#pragma newdecls required

ConVar g_cvClasses;
ConVar g_cvHostname;
ConVar g_cvFormattedHostname;

bool g_bHHLoaded;
bool g_bNemesis;
bool g_bSDLoaded;


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

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------

public void OnPluginStart()
{
	g_cvHostname = FindConVar("hostname");
	g_cvFormattedHostname = CreateConVar("sm_hostname_format", "{HH}UNLOZE | {GM} | NoSteam{SD}", "HH = HappyHour, , GM = GameMode, SD = StageDisplay");

	CreateTimer(60.0, TimerRepeat, INVALID_HANDLE, TIMER_REPEAT);

	HookEvent("round_start", OnRoundStart, EventHookMode_PostNoCopy);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnAllPluginsLoaded()
{
	g_bHHLoaded = LibraryExists("HappyHour");
	g_bSDLoaded = LibraryExists("StageDisplay");

	if((g_cvClasses = FindConVar("zr_config_path_playerclasses")) == INVALID_HANDLE)
		SetFailState("Failed to find zr_config_path_playerclasses cvar.");

	char sFile[PLATFORM_MAX_PATH];
	g_cvClasses.GetString(sFile, sizeof(sFile));
	OnClassesPathChange(g_cvClasses, "", sFile);

	HookConVarChange(g_cvClasses, OnClassesPathChange);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnLibraryAdded(const char[] sName)
{
	if (strcmp(sName, "HappyHour", false) == 0)
		g_bHHLoaded = true;

	if (strcmp(sName, "StageDisplay", false) == 0)
		g_bSDLoaded = true;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnLibraryRemoved(const char[] sName)
{
	if (strcmp(sName, "HappyHour", false) == 0)
		g_bHHLoaded = false;

	if (strcmp(sName, "StageDisplay", false) == 0)
		g_bSDLoaded = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnRoundStart(Event hEvent, const char[] sEvent, bool bDontBroadcast)
{
	CreateTimer(5.0, TimerRoundStart, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action TimerRoundStart(Handle timer)
{
	RefreshHostname();
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action TimerRepeat(Handle timer)
{
	RefreshHostname();
	return Plugin_Continue;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void RefreshHostname()
{
	char sFormattedHostname[128];
	g_cvFormattedHostname.GetString(sFormattedHostname, sizeof(sFormattedHostname));

	if (g_bHHLoaded && HH_IsItHappyHour())
		ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{HH}", "[HappyHour] ", true);
	else
		ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{HH}", "", true);

	char sStage[64];
	if (g_bSDLoaded && SD_GetStage(sStage, sizeof(sStage)))
	{
		Format(sStage, sizeof(sStage), " | Stage: [%s]", sStage);
		ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{SD}", sStage, true);
	}
	else
		ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{SD}", "", true);

	if (g_bNemesis)
		ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{GM}", "Nemesis Zombie Escape", true);
	else
		ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{GM}", "Zombie Escape", true);

	g_cvHostname.SetString(sFormattedHostname, false, false);
}


//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClassesPathChange(ConVar convar, const char[] oldValue, const char[] newValue)
{
	char sFile[PLATFORM_MAX_PATH];
	BuildPath(Path_SM, sFile, sizeof(sFile), "%s", newValue);

	if (StrContains(sFile, "nemesis", false) != -1)
		g_bNemesis = true;
	else
		g_bNemesis = false;
}