-- Basic settings


local MAXIMUM_DIFFERENT_ITEMS = 6

local plr = game.Players.LocalPlayer

function getItemTable()
	
	-- Formulate and return a table of items
	local items = {}
	for _, i in pairs(script.Parent:GetChildren()) do
		if i.Name == "Item" then
			for _=1, i.Count.Value do
				table.insert(items, i.Item.Value.Name)
			end
		end
	end
	return items
	
end

function refreshOutlines()
	
	-- Create table of item names
	local items = {}
	for _, i in pairs(script.Parent:GetChildren()) do
		if i.Name == "Item" then
			table.insert(items, i.Item.Value.Name)
		end
	end
	
	-- Check size
	if #items > 0 then
	
		-- Create table of connected items
		local connectedItems = {}
		for _, i in pairs(game.ReplicatedStorage.Recipes:GetChildren()) do
			local okay = true
			for _, o in pairs(items) do
				if i.Ingredients:FindFirstChild(o) == nil then
					okay = false
					break
				end
			end
			if okay then
				for _, o in pairs(i.Ingredients:GetChildren()) do
					table.insert(connectedItems, o.Name)
				end
			end
		end
		
		-- Go through displayed items adding/removing outlines
		for _, i in pairs(script.Parent.Parent.Parent.Display.Container:GetChildren()) do
			if i.Name == "Item" then
				local okay = false
				for n, o in pairs(connectedItems) do
					if o == i.Item.Value.Name then
						okay = true
						table.remove(connectedItems, n)
						break
					end
				end
				i.BlueEdge.Visible = okay
			end
		end
		
	else
		
		-- Remove all outlines
		for _, i in pairs(script.Parent.Parent.Parent.Display.Container:GetChildren()) do
			if i.Name == "Item" then
				i.BlueEdge.Visible = false
			end
		end
		
	end
	
end

script.Parent.AddItem.OnInvoke = function(item)
	
	-- Check if there already is an entry for the item
	local existing = nil
	for _, i in pairs(script.Parent:GetChildren()) do
		if i.Name == "Item" then
			if i.Item.Value == item then
				existing = i
				break
			end
		end
	end
	if existing == nil then
		
		-- Check if the item count maximum has been reached
		local entryCount = #script.Parent:GetChildren() - 5
		if entryCount >= MAXIMUM_DIFFERENT_ITEMS then
			return false
		end
		
		-- Create an item GUI element and add to container
		local itemGui = script.Parent.ItemTemplate:clone()
		itemGui.Name = "Item"
		itemGui.Item.Value = item
		itemGui.ItemIcon.Image = item.IconAsset.Value
		itemGui.ItemNameLabel.Text = item.Name
		itemGui.ItemNameLabel.TextColor3 = plr.PlayerScripts.Functions.GetRarityColor:Invoke(item.Rarity.Value)
		if item.Rarity.Value == "Immortal" or item.Rarity.Value == "Developer" then
			itemGui.ItemName.TextStrokeColor3 = Color3.fromRGB(255, 255, 255)
		end
		itemGui.Position = UDim2.new(0, (entryCount % 3) * 116 + 10, 0, math.floor(entryCount / 3) * 40 + 10)
		itemGui.Parent = script.Parent
		itemGui.Visible = true
		itemGui.InteractionHandler.Disabled = false
		
		-- Tell preview to refresh
		plr.PlayerGui.MainGui.Crafting.MainFrame.Preview.Refresh:Fire(getItemTable())
		refreshOutlines()
		
		-- Return success
		return true
		
	else
	
		-- Increment count value
		existing.Count.Value = existing.Count.Value + 1
		existing.ItemCount.Text = "x " .. existing.Count.Value
		
		-- Tell preview to refresh
		plr.PlayerGui.MainGui.Crafting.MainFrame.Preview.Refresh:Fire(getItemTable())
		
		-- Return success
		return true
		
	end
	
end

script.Parent.RemoveItem.Event:connect(function(itemGui)
	
	-- Get item
	local item = itemGui.Item.Value
	
	-- Decrement count
	itemGui.Count.Value = itemGui.Count.Value - 1
	
	-- Check if none left
	if itemGui.Count.Value <= 0 then
		
		-- Calculate gui rank to be used below
		local thisRank = (((itemGui.Position.Y.Offset - 10) / 40) * 3) + ((itemGui.Position.X.Offset - 10) / 116)
	
		-- Destroy the gui
		itemGui:destroy()
		
		-- Move each item over/up
		for _, i in pairs(script.Parent:GetChildren()) do
			if i.Name == "Item" then
				local rank = (((i.Position.Y.Offset - 10) / 40) * 3) + ((i.Position.X.Offset - 10) / 116)
				if rank > thisRank then
					i.Position = i.Position + UDim2.new(0, -116, 0, 0)
					if i.Position.X.Offset < 0 then
						i.Position = i.Position + UDim2.new(0, 348, 0, -40)
					end
				end
			end
		end
		
	elseif itemGui.Count.Value > 1 then
		
		-- Change text
		itemGui.ItemCount.Text = "x " .. itemGui.Count.Value
		
	else
		
		-- Change text
		itemGui.ItemCount.Text = ""
		
	end
	
	-- Add back to main display
	plr.PlayerGui.MainGui.Crafting.MainFrame.Display.Container.AddItem:Fire(item)
	
	-- Tell preview to refresh
	plr.PlayerGui.MainGui.Crafting.MainFrame.Preview.Refresh:Fire(getItemTable())
	refreshOutlines()
	
end)

script.Parent.ClearItems.Event:connect(function()
	
	-- Destroy all items
	for _, i in pairs(script.Parent:GetChildren()) do
		if i.Name == "Item" then
			i:destroy()
		end
	end
	
	-- Tell preview to refresh
	plr.PlayerGui.MainGui.Crafting.MainFrame.Preview.Refresh:Fire({})
	
end)