epsilonScript.Items = {};
epsilonScript.Items.Stored = {};

-- The function to register a item to the main item table.
function epsilonScript.Items.RegisterItem( Item )
	epsilonScript.Items.Stored[ Item.Unique ] = Item;
	epsilonScript.Items.Stored[ Item.Unique ].Unique = Item.Unique or "";
	epsilonScript.Items.Stored[ Item.Unique ].Cost = Item.Cost or 0;
	epsilonScript.Items.Stored[ Item.Unique ].Name = Item.Name or "";
	epsilonScript.Items.Stored[ Item.Unique ].Use = Item.Use or false;
	epsilonScript.Items.Stored[ Item.Unique ].Model = Item.Model or "";
	epsilonScript.Items.Stored[ Item.Unique ].Weight = Item.Weight or 0;
	epsilonScript.Items.Stored[ Item.Unique ].Drop = Item.Drop or false;
	epsilonScript.Items.Stored[ Item.Unique ].Description = Item.Description or "";
	
	if ( SERVER ) then
		epsilonScript.Items.Stored[ Item.Unique ].OnUse = Item.OnUse or function( ply )end;
		epsilonScript.Items.Stored[ Item.Unique ].RemoveOnUse = Item.RemoveOnUse or true;
		epsilonScript.Items.Stored[ Item.Unique ].OnDrop = Item.OnDrop or function( ply )end;
		epsilonScript.Items.Stored[ Item.Unique ].OnPickup = Item.OnPickup or function( ply )end;
	end;
end;

-- Function to return the given items table.
function epsilonScript.Items.GetItem( item )
	if ( epsilonScript.Items.Stored[ item ] != nil ) then
		return epsilonScript.Items.Stored[ item ];
	else
		Error( "Item: " .. item .. " does not exist!" );
	end;
end;

-- Include items.
for k, v in pairs( file.FindInLua( "epsilonScript/gamemode/core/schemes/" .. epsilonScript.Config:Get( "ActiveScheme" ) .. "/items/*.lua" ) ) do
	include( "epsilonScript/gamemode/core/schemes/" .. epsilonScript.Config:Get( "ActiveScheme" ) .. "/items/" .. v );
	
	if ( SERVER ) then
		AddCSLuaFile( "epsilonScript/gamemode/core/schemes/" .. epsilonScript.Config:Get( "ActiveScheme" ) .. "/items/" .. v );
	end;
end;

--------------
-- Crafting --
--------------
epsilonScript.Crafting = {};
epsilonScript.Crafting.Stored = {};

function epsilonScript.Crafting.RegisterCraftRecipe( craftRecipe )
	epsilonScript.Crafting.Stored[ craftRecipe.Unique ] = craftRecipe;
	epsilonScript.Crafting.Stored[ craftRecipe.Unique ].Time = craftRecipe.Time or 1;
	epsilonScript.Crafting.Stored[ craftRecipe.Unique ].Model = craftRecipe.Model or "";
	epsilonScript.Crafting.Stored[ craftRecipe.Unique ].GivenItem = craftRecipe.GivenItem or "";
	epsilonScript.Crafting.Stored[ craftRecipe.Unique ].Information = craftRecipe.Information or "";
	epsilonScript.Crafting.Stored[ craftRecipe.Unique ].RequiredItems = craftRecipe.RequiredItems or {};
	
	if ( CLIENT ) then
		epsilonScript.Crafting.Stored[ craftRecipe.Unique ].Name = craftRecipe.Name or "";
		epsilonScript.Crafting.Stored[ craftRecipe.Unique ].Information = craftRecipe.Information or "";
	end;
end;

function epsilonScript.Crafting.GetCraftRecipe( craftRecipe )
	if ( epsilonScript.Crafting.Stored[ craftRecipe ] ) then
		return epsilonScript.Crafting.Stored[ craftRecipe ];
	else
		Error( "!! Crafting !! Recipe: '" .. craftRecipe .. "' was not found in database!" );
	end;
end;

function epsilonScript.Crafting.GetRequiredItems( craftRecipe )
	if ( epsilonScript.Crafting.Stored[ craftRecipe ] ) then
		return epsilonScript.Crafting.Stored[ craftRecipe ].RequiredItems;
	end;
end;

if ( SERVER ) then
	function _R.Player:HasRequiredItems( itemsTable )
		local count = 0;
		local requiredAmount = table.Count( itemsTable );
		
		for k, v in pairs( itemsTable ) do
			if ( self:HasItem( k ) and self:GetActiveInventory().Items[ k ] >= v ) then
				count = count + 1;
			end;
		end;
		
		if ( count >= requiredAmount ) then
			return true;
		end;
		
		return false;
	end;
	
	concommand.Add( "eps_craftitem", function( ply, cmd, args )
		local craftRecipe = args[ 1 ];
		local craftTable = epsilonScript.Crafting.GetCraftRecipe( craftRecipe );
		local requiredItems = epsilonScript.Crafting.GetRequiredItems( craftRecipe );
		
		if ( ply:HasRequiredItems( requiredItems ) ) then
			ply:SendNotify( "Crafting Item..." );
			
			timer.Simple( craftTable.Time, function()
				for k, v in pairs( requiredItems ) do
					if ( epsilonScript.Items.Stored[ k ].RemovedOnCrafting ) then
						ply:RemoveItem( k, 1 );
					end;
				end;
				
				ply:GiveItem( craftTable.GivenItem, 1 );
				ply:SendNotify( "Success!" );
			end, ply );
		else
			ply:SendNotify( "You don't have the required items!" );
			return;
		end;
	end );
end;

-- Include crafts.
for k, v in pairs( file.FindInLua( "epsilonScript/gamemode/core/schemes/" .. epsilonScript.Config:Get( "ActiveScheme" ) .. "/items/crafting/*.lua" ) ) do
	include( "epsilonScript/gamemode/core/schemes/" .. epsilonScript.Config:Get( "ActiveScheme" ) .. "/items/crafting/" .. v );
	
	if ( SERVER ) then
		AddCSLuaFile( "epsilonScript/gamemode/core/schemes/" .. epsilonScript.Config:Get( "ActiveScheme" ) .. "/items/crafting/" .. v );
	end;
end;