#pragma semicolon 1

#include <sourcemod>
#include <sdktools>
/*#include <zombiereloaded>
#include <cstrike>
#include <ccc>
#include <multicolors>*/

ConVar g_cMessagePosition;
ConVar g_cMessageColor;
ConVar g_cMessageCooldown;
ConVar g_cMessageText;

float MessagePos[2];
int MessageColor[3];
char MessageTxt[512];
Handle MessageTimer;
Handle MessageSync;

public Plugin myinfo =
{
	name = "SpamMessage",
	author = "Dogan",
	description = "Provide the server with important informations/advertisements",
	version = "1.1.0",
	url = ""
}

public void OnPluginStart()
{
	g_cMessagePosition = CreateConVar("sm_message_position", "-1.0 0.8125", "The x and y positions for the message");
	g_cMessageColor = CreateConVar("sm_message_color", "0 255 0", "RGB color value for the message");
	g_cMessageCooldown = CreateConVar("sm_message_cooldown", "180", "Cooldown of the message in seconds");
	g_cMessageText = CreateConVar("sm_message_text", "UNLOZE Christmas Sale: 25%% on all VIP Offers. Check www.unloze.com for more info.", "Text thats shown on the message");

	g_cMessagePosition.AddChangeHook(ConVarChange);
	g_cMessageColor.AddChangeHook(ConVarChange);
	g_cMessageCooldown.AddChangeHook(ConVarChange);
	g_cMessageText.AddChangeHook(ConVarChange);

	MessageSync = CreateHudSynchronizer();

	AutoExecConfig(true, "plugin.SpamMessage");
	GetConVars();
}

public void ColorStringToArray(const char[] sColorString, int aColor[3])
{
	char asColors[4][4];
	ExplodeString(sColorString, " ", asColors, sizeof(asColors), sizeof(asColors[]));

	aColor[0] = StringToInt(asColors[0]);
	aColor[1] = StringToInt(asColors[1]);
	aColor[2] = StringToInt(asColors[2]);
}

public void GetConVars()
{
	char StringPos[2][8];
	char PosValue[16];
	g_cMessagePosition.GetString(PosValue, sizeof(PosValue));
	ExplodeString(PosValue, " ", StringPos, sizeof(StringPos), sizeof(StringPos[]));

	MessagePos[0] = StringToFloat(StringPos[0]);
	MessagePos[1] = StringToFloat(StringPos[1]);

	char ColorValue[64];
	g_cMessageColor.GetString(ColorValue, sizeof(ColorValue));

	ColorStringToArray(ColorValue, MessageColor);

	g_cMessageText.GetString(MessageTxt, sizeof(MessageTxt));

	if (MessageTimer != INVALID_HANDLE && CloseHandle(MessageTimer))
		MessageTimer = INVALID_HANDLE;

	MessageTimer = CreateTimer(g_cMessageCooldown.FloatValue, TimerCooldown, _, TIMER_REPEAT);
}

public void ConVarChange(ConVar convar, char[] oldValue, char[] newValue)
{
	GetConVars();
}

public void SendMessage()
{
	SetHudTextParams(MessagePos[0], MessagePos[1], 6.0, MessageColor[0], MessageColor[1], MessageColor[2], 255, 1, 4.0, 0.8, 0.8);

	for (int client = 1; client <= MAXPLAYERS; client++)
	{
		if (IsClientInGame(client) && !IsFakeClient(client))
		{
			ShowSyncHudText(client, MessageSync, MessageTxt);
		}
	}
}

public Action TimerCooldown(Handle timer)
{
	SendMessage();
}