#include <icpack/load>

new Handle:hfOnTouchPre = INVALID_HANDLE,
	Handle:hfOnTouchPost = INVALID_HANDLE

public OnPluginStart()
{
	hfOnTouchPre = CreateGlobalForward("GCreator_OnTouchPre", ET_Event, Param_Cell, Param_Cell)
	hfOnTouchPost = CreateGlobalForward("GCreator_OnTouchPost", ET_Ignore, Param_Cell, Param_Cell)
}

public OnMapStart()
{
	PrecacheModel("models/items/cs_gift.mdl")
}

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
	CreateNative("GCreator_Create", GCreator_Create_Native)
	return APLRes_Success
}

public int GCreator_Create_Native(Handle plugin, int nums)
{
	new Float:pos[3]
	GetNativeArray(1, pos, 3)
	return CreateGift(pos)
}

int CreateGift(const Float:pos[3])
{
	new ent = -1;

	if ((ent = CreateEntityByName("prop_physics_override")) != -1)
	{
		decl String:tmp[64];

		FormatEx(tmp, sizeof(tmp), "gift_%i", ent);

		DispatchKeyValue(ent, "model", "models/items/cs_gift.mdl");
		DispatchKeyValue(ent, "physicsmode", "2");
		DispatchKeyValue(ent, "massScale", "1.0");
		DispatchKeyValue(ent, "targetname", tmp);
		DispatchSpawn(ent);
		
		SetEntProp(ent, Prop_Send, "m_usSolidFlags", 8);
		SetEntProp(ent, Prop_Send, "m_CollisionGroup", 1);
		
		TeleportEntity(ent, pos, NULL_VECTOR, NULL_VECTOR);
		
		new rot = CreateEntityByName("func_rotating");
		FormatEx(tmp, sizeof(tmp), "gift_rot_%i", rot);
		DispatchKeyValueVector(rot, "origin", pos);
		DispatchKeyValue(rot, "targetname", tmp);
		DispatchKeyValue(rot, "maxspeed", "200");
		DispatchKeyValue(rot, "friction", "0");
		DispatchKeyValue(rot, "dmg", "0");
		DispatchKeyValue(rot, "solid", "0");
		DispatchKeyValue(rot, "spawnflags", "64");
		DispatchSpawn(rot);
		SetEntPropEnt(rot, Prop_Send, "m_hOwnerEntity", ent)
		// Format(tmp, sizeof(tmp), "%s,Kill,,0,-1", tmp);
		// DispatchKeyValue(ent, "OnKilled", tmp);
		
		SetVariantString("!activator");
		AcceptEntityInput(ent, "SetParent", rot, rot);
		
		new trigger = CreateEntityByName("trigger_multiple");
		FormatEx(tmp, sizeof(tmp), "gift_trigger_%i", trigger);
		DispatchKeyValueVector(trigger, "origin", pos);
		DispatchKeyValue(trigger, "targetname", tmp);
		DispatchKeyValue(trigger, "wait", "0");
		DispatchKeyValue(trigger, "spawnflags", "64");
		DispatchSpawn(trigger);
		
		// Format(tmp, sizeof(tmp), "%s,Kill,,0,-1", tmp);
		// DispatchKeyValue(rot, "OnKilled", tmp);
		
		ActivateEntity(trigger);
		SetEntProp(trigger, Prop_Data, "m_spawnflags", 64);
		SetEntityModel(trigger, "models/items/cs_gift.mdl");
		
		decl Float:fMins[3], Float:fMaxs[3];
		GetEntPropVector(ent, Prop_Send, "m_vecMins", fMins);
		GetEntPropVector(ent, Prop_Send, "m_vecMaxs", fMaxs);
		
		SetEntPropVector(trigger, Prop_Send, "m_vecMins", fMins);
		SetEntPropVector(trigger, Prop_Send, "m_vecMaxs", fMaxs);
		SetEntProp(trigger, Prop_Send, "m_nSolidType", 2);
		
		new iEffects = GetEntProp(trigger, Prop_Send, "m_fEffects");
		iEffects |= 32;
		SetEntProp(trigger, Prop_Send, "m_fEffects", iEffects);
		
		SetVariantString("!activator");
		AcceptEntityInput(trigger, "SetParent", rot, rot);
		AcceptEntityInput(rot, "Start");
		SetEntPropEnt(trigger, Prop_Send, "m_hOwnerEntity", rot)
		HookSingleEntityOutput(trigger, "OnStartTouch", OnStartTouch);
		return ent
	}
	return -1
}

public OnStartTouch(const String:output[], caller, activator, Float:delay)
{
	new rot = GetEntPropEnt(caller, Prop_Send, "m_hOwnerEntity")
	new gift = GetEntPropEnt(rot, Prop_Send, "m_hOwnerEntity")
	if(Func_OnTouchPre(activator, gift)==Plugin_Continue)
		Func_OnTouchPost(activator, gift)
	else
		return;
	UnhookSingleEntityOutput(caller, "OnStartTouch", OnStartTouch)
	AcceptEntityInput(gift, "kill")
	AcceptEntityInput(rot, "kill")
	AcceptEntityInput(caller, "kill")
}

Action:Func_OnTouchPre(client, gift)
{
	new Action:result = Plugin_Continue
	Call_StartForward(hfOnTouchPre)
	Call_PushCell(client)
	Call_PushCell(gift)
	Call_Finish(result)
	return result
}

Func_OnTouchPost(client, gift)
{
	Call_StartForward(hfOnTouchPost)
	Call_PushCell(client)
	Call_PushCell(gift)
	Call_Finish()
}