#include <sourcemod>
#include <unloze>
#include <cstrike>
#include <sdktools>

ConVar g_cvarEventIsOn = null;
char g_directory_path[128];

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name 		= "Unloze_VIP_Reward",
	author 		= "Neon + WASD + ??????????",
	description = "",
	version 	= "1.1",
	url 		= "https://steamcommunity.com/id/n3ontm"
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	RegAdminCmd("sm_listwinners", Command_Winners, ADMFLAG_GENERIC);
	g_cvarEventIsOn = CreateConVar("sv_event_is_on", "0", "Cvar decides whether event mode is on or not.", _);
	HookEvent("round_end", Event_Round_End)

	BuildPath(Path_SM, g_directory_path, sizeof(g_directory_path), "data/events/");

	if (!DirExists(g_directory_path, false)) {
		CreateDirectory(g_directory_path, 457, false);
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Command_Winners(int iClient, int iArgs)
{
	static char sSID[64];
	static char sName[64];
	
	for (int client = 1; client <= MaxClients; client++)
	{
		if(IsValidWinner(client))
		{
			GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
			GetForumNameFormat(client, sName, sizeof(sName));
			PrintToConsole(iClient, "%s --- %N --- %s", sSID, client, sName);
		}
	}

	return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Event_Round_End(Handle event, const char[] name, bool dontBroadcast)
{
	if (g_cvarEventIsOn.IntValue == 1 && GetEventInt(event, "winner") == CS_TEAM_CT) {
		int t_score = GetTeamScore(CS_TEAM_T);
		int ct_score = GetTeamScore(CS_TEAM_CT);

		char date[20];
		FormatTime(date, sizeof(date), "%Y%m%d");

		char map[90];
		GetCurrentMap(map, sizeof(map));

		char file_path[256];
		Format(file_path, sizeof(file_path), "%s%s-%s.txt", g_directory_path, date, map);

		Handle file_handle = OpenFile(file_path, "a", false);

		WriteFileLine(file_handle, "Current score: (CT) : %d - (T): %d", ct_score, t_score);
		
		static char sSID[64];
		static char sName[64];
		
		for (int client = 1; client <= MaxClients; client++)
		{
			if(IsValidWinner(client))
			{
				GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID));
				GetForumNameFormat(client, sName, sizeof(sName));
				WriteFileLine(file_handle, "%s --- %N --- %s", sSID, client, sName);
			}
		}
		
		WriteFileLine(file_handle, "\n");
		CloseHandle(file_handle);
	}
}

GetForumNameFormat(int client, char[] sName, int sNameLen)
{
	char sForumName[64];
	GetClientForumName(client, sForumName, sizeof(sForumName));
	
	if(strlen(sForumName) > 0)
		Format(sName, sNameLen, "FORUM NAME: %s", sForumName);
	else
		Format(sName, sNameLen, "NO FORUM ACCOUNT");
}

stock bool IsValidWinner(int client)
{
	if (IsValidClient(client, true) && IsPlayerAlive(client) && GetClientTeam(client) == CS_TEAM_CT && !IsClientSourceTV(client))
		return true;
		
	return false;
}

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