#pragma semicolon 1

#include <sourcemod>

#include "loghelper.inc"

#pragma newdecls required

float g_fAdminConnectionTime[MAXPLAYERS + 1];
char g_cAdminName[MAXPLAYERS + 1][128];
char g_cAdminSteamID[MAXPLAYERS + 1][128];
AdminId g_AdminID[MAXPLAYERS + 1];
char g_cAdminActivityFile[128];

public Plugin myinfo =
{
	name         = "AdminActivity",
	author       = "Dogan",
	description  = "Log Admin Activity",
	version      = "1.0.0"
};

public void OnPluginStart()
{
	CreateTimer(30.0, CheckConnectionTime, _, TIMER_REPEAT);

	BuildPath(Path_SM, g_cAdminActivityFile, sizeof(g_cAdminActivityFile), "logs/adminactivity/AdminActivity.txt");

	HookEvent("player_disconnect", EventHook_PlayerDisconnect, EventHookMode_Post);
}

public void OnPluginEnd()
{
	UnhookEvent("player_disconnect", EventHook_PlayerDisconnect, EventHookMode_Post);
}

public void OnMapStart()
{
	GetTeams();
}

public void EventHook_PlayerDisconnect(Event hEvent, const char[] sName, bool bDontBroadcast)
{
	bool bIsBot = view_as<bool>(hEvent.GetInt("bot"));

	if (bIsBot)
		return;

	int client = GetClientOfUserId(hEvent.GetInt("userid"));

	g_AdminID[client] = GetUserAdmin(client);

	if (GetAdminFlag(g_AdminID[client], Admin_Generic))
	{
		GetClientAuthId(client, AuthId_Steam2, g_cAdminSteamID[client], 128);
		GetClientName(client, g_cAdminName[client], 128);
		LogToFile(g_cAdminActivityFile, "%s %s Time in Minutes: %f", g_cAdminName[client], g_cAdminSteamID[client], g_fAdminConnectionTime[client] / 60.0);
		g_fAdminConnectionTime[client] = 0.0;
	}
}

public Action CheckConnectionTime(Handle hThis)
{
	for (int i = 1; i <= MaxClients; i++)
	{
		if (IsValidClient(i) && GetAdminFlag(g_AdminID[i], Admin_Generic))
			continue;

		g_AdminID[i] = GetUserAdmin(i);

		g_fAdminConnectionTime[i] += 30.0;
	}
}

stock bool IsValidClient(int client)
{
	return (client >= 1 && client <= MaxClients && IsClientInGame(client) && !IsFakeClient(client));
}