#pragma semicolon 1

#include <PlayerManager>
#include <sourcemod>

#pragma newdecls required

/* STRINGS */
char g_sAdmGroup[32] = "Game-Donator";
char g_sGroup[MAXPLAYERS+1][64];

/* CONVARS */
ConVar g_cvFreeVIPDuration;

/* DATABASE */
Database g_hDatabase;

/* BOOLS */
bool g_bPreAdminChecked[MAXPLAYERS+1];
bool g_bResponseFailed[MAXPLAYERS+1];
bool g_bResponsePassed[MAXPLAYERS+1];

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name        = "UNLOZE_VIP_Test",
	author      = "Neon",
	description = "",
	version     = "2.1",
	url         = "https://steamcommunity.com/id/n3ontm"
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	g_cvFreeVIPDuration = CreateConVar("sm_unloze_vip_test_duration", "1440", "", FCVAR_NONE);

	RegConsoleCmd("sm_viptest", Command_VIP, "Activate free VIP period");
	RegConsoleCmd("sm_testvip", Command_VIP, "Activate free VIP period");

	AutoExecConfig();

	char sError[256];
	if (SQL_CheckConfig("testvip"))
		g_hDatabase = SQL_Connect("testvip", true, sError, sizeof(sError));

	if (g_hDatabase == null)
		LogError("Could not connect to database: %s", sError);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnRebuildAdminCache(AdminCachePart part)
{
	if (part != AdminCache_Admins)
		return;

	CreateTimer(1.0, OnRebuildAdminCachePost, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnRebuildAdminCachePost(Handle hTimer)
{
	for (int client = 1; client <= MaxClients; client++)
	{
		if(g_bResponsePassed[client] && g_bPreAdminChecked[client])
			ApplyGroupFlags(client);
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientConnected(int client)
{
	g_bPreAdminChecked[client] = false;
	g_bResponseFailed[client] = false;
	g_bResponsePassed[client] = false;

	g_sGroup[client][0] = 0;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientDisconnect(int client)
{
	g_bPreAdminChecked[client] = false;
	g_bResponseFailed[client] = false;
	g_bResponsePassed[client] = false;

	g_sGroup[client][0] = 0;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientAuthorized(int client, const char[] sSteamID32)
{
	if (IsFakeClient(client))
		return;

	char sSteamID[32];
	GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));

	char sQuery[256];
	Format(sQuery, sizeof(sQuery), "SELECT * FROM testvip_table WHERE steam_auth = '%s'", sSteamID);
	SQL_TQuery(g_hDatabase, TQueryCBConnect, sQuery, GetClientUserId(client));
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void TQueryCBConnect(Handle owner, Handle rs, const char[] error, any data)
{
	int client = 0;

	if ((client = GetClientOfUserId(data)) == 0)
		return;

	if (SQL_GetRowCount(rs) > 0)
	{
		int iField;
		SQL_FetchRow(rs);
		SQL_FieldNameToNum(rs, "activated", iField);
		int iTimestampActivated = SQL_FetchInt(rs, iField);
		int iTimestamp = GetTime();
		delete rs;

		if ((iTimestamp - iTimestampActivated) < g_cvFreeVIPDuration.IntValue *60)
		{
			strcopy(g_sGroup[client], sizeof(g_sGroup[]), g_sAdmGroup);
		}
	}

	g_bResponsePassed[client] = true;
	if (g_bPreAdminChecked[client])
		NotifyPostAdminCheck(client);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action OnClientPreAdminCheck(int client)
{
	g_bPreAdminChecked[client] = true;

	if (g_bResponsePassed[client] || g_bResponseFailed[client])
		return Plugin_Continue;

	RunAdminCacheChecks(client);
	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnClientPostAdminFilter(int client)
{
	ApplyGroupFlags(client);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_VIP(int client, int iArgs)
{
    if (!IsValidClient(client))
        return Plugin_Handled;

    if (!PM_IsPlayerSteam(client))
    {
        PrintToChat(client, "Nosteam cant use test vip anymore.");
        return Plugin_Handled;
    }

    char sSteamID[32];
    GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));

    char sQuery[255];
    Format(sQuery, sizeof(sQuery), "SELECT * FROM testvip_table WHERE steam_auth = '%s'", sSteamID);
    SQL_TQuery(g_hDatabase, TQueryCBCommand, sQuery, GetClientUserId(client));

    return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void TQueryCBCommand(Handle owner, Handle rs, const char[] error, any data)
{
	int client = 0;

	if ((client = GetClientOfUserId(data)) == 0)
		return;

	int iTimestamp = GetTime();

	if (SQL_GetRowCount(rs) > 0)
	{
		int iField;
		SQL_FetchRow(rs);
		SQL_FieldNameToNum(rs, "activated", iField);
		int iTimestampActivated = SQL_FetchInt(rs, iField);
		delete rs;

		if ((iTimestamp - iTimestampActivated) < g_cvFreeVIPDuration.IntValue *60)
			PrintToChat(client, "[UNLOZE] Your TEST VIP will expire in %d minutes!", g_cvFreeVIPDuration.IntValue - (iTimestamp - iTimestampActivated) / 60);
		else
			PrintToChat(client, "[UNLOZE] Your TEST VIP expired already!");
	}
	else
	{
		if (IsVIP(client))
		{
			PrintToChat(client, "[UNLOZE] You already have VIP activated!");
			return;
		}

		char sSteamID[32];
		GetClientAuthId(client, AuthId_Steam2, sSteamID, sizeof(sSteamID));

		char sQuery[255];
		Format(sQuery, sizeof(sQuery), "INSERT INTO testvip_table (steam_auth, activated) VALUES ('%s', '%d')", sSteamID, iTimestamp);
		SQL_FastQuery(g_hDatabase, sQuery);
		strcopy(g_sGroup[client], sizeof(g_sGroup[]), g_sAdmGroup);
		ApplyGroupFlags(client);
		PrintToChat(client, "[UNLOZE] You have now access to !zclass, !tag and !glow and other VIP-Perks.");
		PrintToChat(client, "[UNLOZE] Your TEST VIP will expire in %d minutes!", g_cvFreeVIPDuration.IntValue);
		FakeClientCommandEx(client, "sm_vip");
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock void ApplyGroupFlags(int client)
{
	if (!g_bResponsePassed[client])
		return;

	if (g_sGroup[client][0] == 0)
		return;

	AdminId AdmID;
	GroupId GrpID;

	if ((AdmID = GetUserAdmin(client)) == INVALID_ADMIN_ID)
	{
		LogMessage("Creating new admin for %L", client);

		AdmID = CreateAdmin("");
		SetUserAdmin(client, AdmID, true);
	}

	if ((GrpID = FindAdmGroup(g_sGroup[client])) != INVALID_GROUP_ID)
	{
		if (AdminInheritGroup(AdmID, GrpID))
			LogMessage("%L added to group %s", client, g_sGroup[client]);
	}
	else
		LogMessage("%L group not found %s", client, g_sGroup[client]);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
stock int IsValidClient(int client, bool nobots = true)
{
	if (client <= 0 || client > MaxClients || !IsClientConnected(client) || (nobots && IsFakeClient(client)))
		return false;

	return IsClientInGame(client);
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public bool IsVIP(int client)
{
	AdminId AdmID;

	if ((AdmID = GetUserAdmin(client)) == INVALID_ADMIN_ID)
		return false;
	else
	{
		for (int i = 0; i <= GetAdminGroupCount(AdmID); i++)
		{
			char sGroup[32];
			if ((GetAdminGroup(AdmID, i, sGroup, sizeof(sGroup)) != INVALID_GROUP_ID))
			{
				if (StrEqual(sGroup, g_sAdmGroup))
					return true;
			}
		}
	}
	return false;
}
