//====================================================================================================
//
// Name: [entWatch] Beacon
// Author: zaCade & Prometheum
// Description: Handle the tools of [entWatch]
//
//====================================================================================================
#include <multicolors>

#pragma newdecls required

#include <sourcemod>
#include <sdkhooks>
#include <sdktools>
#include <entWatch_core>
#include <entWatch_helpers>

int g_iBeamSprite = -1;
int g_iHaloSprite = -1;
bool g_bDropped[256];
bool g_bActive[256];

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name         = "[entWatch] Beacon",
	author       = "Neon",
	description  = "",
	version      = "1.0.0",
	url 		= "https://steamcommunity.com/id/n3ontm"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	HookEvent("round_start", Event_RoundStart, EventHookMode_Pre);
}

public Action Event_RoundStart(Event event, const char[] name, bool dontBroadcast)
{
    for (int i = 0; i < sizeof(g_bDropped); i++)
    {
        g_bDropped[i] = false;
        g_bActive[i] = false;
    }
    return Plugin_Handled;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnMapStart()
{
	GameData gameConfig = new GameData("funcommands.games");
	if (gameConfig == null)
	{
		SetFailState("Unable to load game config funcommands.games");
		return;
	}

	char buffer[PLATFORM_MAX_PATH];
	if (gameConfig.GetKeyValue("SpriteBeam", buffer, sizeof(buffer)) && buffer[0])
	{
		g_iBeamSprite = PrecacheModel(buffer);
	}

	if (gameConfig.GetKeyValue("SpriteHalo", buffer, sizeof(buffer)) && buffer[0])
	{
		g_iHaloSprite = PrecacheModel(buffer);
	}

	delete gameConfig;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void EW_OnClientItemDrop(int client, int index)
{
	g_bDropped[index] = true;
	if (!g_bActive[index])
	{
		CreateTimer(1.0, Timer_Beacon, index, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
		g_bActive[index] = true;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void EW_OnClientItemDeath(int client, int index)
{
	g_bDropped[index] = true;
	if (!g_bActive[index])
	{
		CreateTimer(1.0, Timer_Beacon, index, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
		g_bActive[index] = true;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void EW_OnClientItemDisconnect(int client, int index)
{
	g_bDropped[index] = true;
	if (!g_bActive[index])
	{
		CreateTimer(1.0, Timer_Beacon, index, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
		g_bActive[index] = true;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void EW_OnClientItemPickup(int client, int index)
{
	g_bDropped[index] = false;
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Action Timer_Beacon(Handle timer, any index)
{
	if (index == 0 || index > EW_GetItemCount() ||!g_bDropped[index])
	{
		g_bActive[index] = false;
		return Plugin_Stop;
	}

	CItem item = EW_GetItemData(index);

	if (item.bClient || !item.bWeapon)
	{
		g_bActive[index] = false;
		return Plugin_Stop;
	}

	CConfig cfg = item.dConfig	;

	if (cfg.iMode == 2 && item.iTimesUsed >= cfg.iMaxUses)
	{
		g_bActive[index] = false;
		return Plugin_Stop;
	}

	float fOrigin[3];
	GetEntPropVector(item.iWeapon, Prop_Send, "m_vecOrigin", fOrigin)

	char sColor[32];
	item.dConfig.GetColor(sColor, sizeof(sColor));

	int iColor;
	StringToIntEx(sColor, iColor, 16);

	int iVecColor[4]
	iVecColor[0] = ((iColor >> 16) & 0xFF);
	iVecColor[1] = ((iColor >> 8) & 0xFF);
	iVecColor[2] = ((iColor >> 0) & 0xFF);
	iVecColor[3] = 255


	TE_SetupBeamRingPoint(fOrigin, 5.0, 50.0, g_iBeamSprite, g_iHaloSprite, 0, 10, 0.6, 6.0, 0.5, iVecColor, 10, 0);
	TE_SendToAll();

	return Plugin_Continue;

}