#pragma semicolon 1

#include <sourcemod>

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

#pragma newdecls required

ConVar g_cvInfoMessage;
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} | LagCompensation | 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_cvInfoMessage = FindConVar("sm_info_message_file")) == INVALID_HANDLE)
		SetFailState("Failed to find sm_info_message_file cvar.");

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

	HookConVarChange(g_cvInfoMessage, 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);

	int iLength = strlen(sFormattedHostname);
	if(iLength > 55) //if too long trim lagcomp
			ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "LagCompensation | ", "", 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;
}