#pragma semicolon 1

#include <sourcemod>
#include <sdktools>

#undef REQUIRE_PLUGIN
#tryinclude <HappyHour>
#define REQUIRE_PLUGIN

#pragma newdecls required

bool g_bHHLoaded;

char g_sTeamList[16][64];

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name         = "HLstatsX CE - LogHelper",
	author       = "Neon",
	description  = "Custom HLstatsX Logging",
	version      = "1.0.0"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public APLRes AskPluginLoad2(Handle hMyself, bool bLate, char[] sError, int errorSize)
{
	CreateNative("LH_LogPlayerEvent", Native_LogPlayerEvent);

	RegPluginLibrary("HlstatsXLogHelper");
	return APLRes_Success;
}

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

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

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

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnMapStart()
{
	GetTeams();
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public int Native_LogPlayerEvent(Handle hPlugin, int numParams)
{
	int iClient;
	char sVerb[64];
	char sEvent[256];
	bool bHH;

	iClient = GetNativeCell(1);
	GetNativeString(2, sVerb, sizeof(sVerb));
	GetNativeString(3, sEvent, sizeof(sEvent));
	bHH = GetNativeCell(4);

	if (iClient < 1 || iClient > MaxClients)
	{
		return ThrowNativeError(SP_ERROR_NATIVE, "Client index %d is invalid", iClient);
	}
	else if (!IsClientInGame(iClient))
	{
		return ThrowNativeError(SP_ERROR_NATIVE, "Client %d is not ingame", iClient);
	}

	LogPlayerEventCustom(iClient, sVerb, sEvent, bHH);

	return true;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock void LogPlayerEventCustom(int client, const char[] verb, const char[] event, bool hh)
{
	if (!IsValidPlayer(client))
		return;

	char player_authid[32];
	if (!GetClientAuthId(client, AuthId_Engine, player_authid, sizeof(player_authid), false))
		strcopy(player_authid, sizeof(player_authid), "UNKNOWN");


	char sEvent[256];
	Format(sEvent, sizeof(sEvent), "%s", event);

	if (g_bHHLoaded && HH_IsItHappyHour() && hh)
		Format(sEvent, sizeof(sEvent), "%s_hh", sEvent);

	char sLog[512];
	Format(sLog, sizeof(sLog), "\"%N<%d><%s><%s>\" %s \"%s\"", client, GetClientUserId(client), player_authid, g_sTeamList[GetClientTeam(client)], verb, sEvent);

	LogToGame(sLog);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock void GetTeams(bool insmod = false)
{
	if (!insmod)
	{
		int max_teams_count = GetTeamCount();
		for (int team_index = 0; (team_index < max_teams_count); team_index++)
		{
			char team_name[64];
			GetTeamName(team_index, team_name, sizeof(team_name));

			if (strcmp(team_name, "") != 0)
			{
				g_sTeamList[team_index] = team_name;
			}
		}
	}
	else
	{
		// they really need to get their act together... GetTeamName() would be awesome since they can't even keep their team indexes consistent
		char mapname[64];
		GetCurrentMap(mapname, sizeof(mapname));
		if (strcmp(mapname, "ins_karam") == 0 || strcmp(mapname, "ins_baghdad") == 0)
		{
			g_sTeamList[1] = "Iraqi Insurgents";
			g_sTeamList[2] = "U.S. Marines";
		}
		else
		{
			g_sTeamList[1] = "U.S. Marines";
			g_sTeamList[2] = "Iraqi Insurgents";
		}
		g_sTeamList[0] = "Unassigned";
		g_sTeamList[3] = "SPECTATOR";
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
static stock bool IsValidPlayer(int client)
{
	if (client > 0 && client <= MaxClients && IsClientInGame(client))
	{
		return true;
	}
	return false;
}