#include <sourcemod>
#include <calladmin>
#include <basecomm>
#include <AFKManager>
#include <multicolors>

#pragma semicolon 1
#pragma newdecls required

int g_iAdminAFKTime;
ConVar g_cvAdminAFKTime;

public Plugin myinfo =
{
	name        = "CallAdmin Restrictions",
	author      = "Neon + Dogan",
	description = "",
	version     = "2.0",
	url         = "https://steamcommunity.com/id/n3ontm"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnAllPluginsLoaded()
{
	if((g_cvAdminAFKTime = FindConVar("sm_admin_afk_time")) == INVALID_HANDLE)
		SetFailState("Failed to find sm_admin_afk_time cvar.");
	else
		g_iAdminAFKTime = g_cvAdminAFKTime.IntValue;

	HookConVarChange(g_cvAdminAFKTime, OnAdminAFKTimeChanged);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnAdminAFKTimeChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
	g_iAdminAFKTime = g_cvAdminAFKTime.IntValue;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action CallAdmin_OnDrawTarget(int client, int target)
{
	if (CheckCommandAccess(target, "", ADMFLAG_GENERIC, true))
		return Plugin_Handled;
	else
		return Plugin_Continue;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action CallAdmin_OnReportPre(int client, int target, const char[] reason)
{
	bool ActiveAdmins = false;

	for (int i = 1; i <= MaxClients; i++)
	{
		if (IsClientInGame(i) && (CheckCommandAccess(i, "", ADMFLAG_GENERIC, true)) && GetClientIdleTime(i) < g_iAdminAFKTime)
		{
			ActiveAdmins = true;
			CPrintToChat(i, "{green}[CALLADMIN] {lightgreen}%N reported %N for %s.", client, target, reason);
		}
	}

	if(ActiveAdmins)
	{
		CPrintToChat(client, "{green}[CALLADMIN] {lightgreen}Reported %N for %s.", target, reason);
		return Plugin_Handled;
	}
	else
		return Plugin_Continue;
}