ItemData = {};

-- items to not remove from inventory when used
local doNotTake = 
{
	"radio",
}

function CAKE.LoadItemData()

	local list = file.FindInLua( "ECPA/gamemode/items/*.lua" );
	
	for k, v in pairs(list) do
	
		CAKE.LoadItem(v);
		
	end
	
end

function CAKE.LoadItem(filename)

	local path = "items/" .. filename;
	
	ITEM = {};
	
	include(path);
	
	ItemData[ITEM.Class] = ITEM;
	
end

function CAKE.CreateItem(class, pos, ang)

	if(ItemData[class] == nil) then return; end
	
	local itemtable = ItemData[class];
	
	local item = ents.Create("item_prop");
	
	item:SetModel(itemtable.Model);
	item:SetAngles(ang);
	item:SetPos(pos);
	
	for k, v in pairs(itemtable) do
		item[k] = v;
		if(type(v) == "string") then
			item:SetNWString(k, v);
		end
	end
	
	item:Spawn();
	item:Activate();
	
end

function ccDropItem(ply, cmd, args)

	local inv = ply:GetField("inventory");
	for k, v in pairs(inv) do
		if(v == args[1]) then
			CAKE.CreateItem(args[1], ply:CalcDrop(), Angle(0,0,0));
			ply:TakeItem(args[1]);
			
			-- when dropping an item check for radio to remove their ability to hear it
			if( args[1] == "radio" ) then
				ply:SetNWFloat("radioChannel", 0 );
			end
			
			return;
		end
	end
	
end
concommand.Add("rp_dropitem", ccDropItem);

function ccUseItem(ply, cmd, args)
	
	-- this command is used from in an inventory menu, no ent is "created" per say, so combining / rewriting things
	--ply:ConCommand( "rp_dropitem " .. args[1] );
	--ply:ConCommand( "rp_useitem" );
	
-- table's should have a toString() .......... argh
--Msg( ItemData );
	
	local inv = ply:GetField("inventory");
	for k, v in pairs(inv) do
		if(v == args[1]) then
			
			if(ItemData[args[1]] == nil) then return; end
	
			local itemtable = ItemData[args[1]];
	
			--local item = ents.Create("item_prop");
	
			--item:SetModel(itemtable.Model);
			--item:SetAngles( Angle(0,0,0) ); -- preset position / angle, faster than finding out
			--item:SetPos( Vector(0,0,0) ); -- some random ones, plus item is about to be removed
	
			local item = {};
	
			for k, v in pairs(itemtable) do
				item[k] = v;
				--if(type(v) == "string") then
					--item:SetNWString(k, v);
				--end
			end
	
			-- do this before useItem(), use may give the item back (for sweps) and removing will just cause probs
			-- if they disconnect and then it isn't saved.
			
--Msg("class: " .. itemtable.Class .. "\n" );
			
			if( table.HasValue(doNotTake, itemtable.Class ) ) then
				-- do not take, durr
--Msg("has value!\n");
			else
				ply:TakeItem(args[1]);
			end
	
			--item:Spawn();
			--item:Activate();
			
			-- maybe more efficient to not create and remove an ent, but would require adding a
			-- new function to all items for when used from inventory
			item:UseItem(ply);
			
		end
	end
	

end
concommand.Add("rp_useinvitem", ccUseItem);

function ccBuyItem(ply, cmd, args)
	
	if(ItemData[args[1]] != nil) then
		if(Teams[ply:Team()]["business"]) then
			if(ItemData[args[1]].Purchaseable and ply:GetField("money") >= ItemData[args[1]].Price) then
				ply:SetField("money", ply:GetField("money") - ItemData[args[1]].Price);
				CAKE.CreateItem(args[1], ply:CalcDrop(), Angle(0,0,0));
			end
		else
			CAKE.Notify(ply, "You do not have access to Business!");
		end
	end
	
end
concommand.Add("rp_buyitem", ccBuyItem);

CAKE.LoadItemData();