#if defined entWatch_helpers_included
	#endinput
#endif

#define entWatch_helpers_included

/**
 * Converts the whole String to lower case.
 * Only works with alphabetical characters (not ײִ) because Sourcemod suxx !
 * The Output String can be the same as the Input String.
 *
 * @param input				Input String.
 * @param output			Output String.
 * @param size				Max Size of the Output string
 * @noreturn
 */
stock void String_ToLower(const char[] input, char[] output, int size)
{
	size--;

	int x;
	while (input[x] != '\0' || x < size) {

		if (IsCharUpper(input[x])) {
			output[x] = CharToLower(input[x]);
		}
		else {
			output[x] = input[x];
		}

		x++;
	}

	output[x] = '\0';
}

/**
* Checks if the specified index is a player and connected.
*
* @param entity				An entity index.
* @param checkConnected		Set to false to skip the IsClientConnected check
* @return					Returns true if the specified entity index is a player connected, false otherwise.
*/
stock bool Client_IsValid(int client, bool checkConnected=true)
{
	if (client > 4096) {
		client = EntRefToEntIndex(client);
	}

	if (client < 1 || client > MaxClients) {
		return false;
	}

	if (checkConnected && !IsClientConnected(client)) {
		return false;
	}

	return true;
}

/**
 * Gets the client's current observer target entity.
 *
 * @param client		Client Index.
 * @return				Observed Entity Index.
 */
stock int Client_GetObserverTarget(int client)
{
	return GetEntPropEnt(client, Prop_Send, "m_hObserverTarget");
}

/*
 * Checks if an entity is valid and exists.
 *
 * @param entity		Entity Index.
 * @return				True if the entity is valid, false otherwise.
 */
stock bool Entity_IsValid(int entity)
{
	return IsValidEntity(entity);
}

/**
 * Gets the Hammer-ID of an entity.
 * The Hammer Editor gives every entity a unique ID.
 * Note: Old maps don't have Hammer-ID's set for entities
 *
 * @param entity			Entity index.
 * @return					Hammer ID.
 */
stock int Entity_GetHammerID(int entity)
{
	return GetEntProp(entity, Prop_Data, "m_iHammerID");
}

/**
 * Gets the owner of an entity.
 * For example the owner of a weapon entity.
 *
 * @param entity		Entity index.
 * @return				Ground Entity or -1
 */
stock int Entity_GetOwner(int entity)
{
	return GetEntPropEnt(entity, Prop_Data, "m_hOwnerEntity");
}

/*
 * Gets the parent entity of an entity.
 *
 * @param entity		Entity Index.
 * @return				Entity Index of the parent.
 */
stock int Entity_GetParent(int entity)
{
	return GetEntPropEnt(entity, Prop_Data, "m_pParent");
}

/**
 * Sets the Name of an entity.
 *
 * @param entity			Entity index.
 * @param name				The name you want to give.
 * @noreturn
 */
stock void Entity_SetName(int entity, const char[] name, any ...)
{
	char format[128];
	VFormat(format, sizeof(format), name, 3);

	DispatchKeyValue(entity, "targetname", format);
}