-- Input Module


local module = {}

module.mode = script.Mode
local USI = game:GetService("UserInputService")

if USI.GamepadEnabled then
	module.mode.Value = "Xbox"
elseif USI.KeyboardEnabled then
	module.mode.Value = "PC"
elseif USI.TouchEnabled then
	module.mode.Value = "Mobile"
end

game.GuiService.AutoSelectGuiEnabled = false

function module.findFirstButton(Object)
	local Closest
	local Dist = 999
	for i,Button in Object:GetDescendants() do
		if Button:IsA("GuiButton") and Button.Selectable then
			local D = (Object.AbsolutePosition - Button.AbsolutePosition).magnitude
			if D < Dist then
				Dist = D
				Closest = Button
			end
		end
	end
	return Closest
end

function module.init(Modules)
	local Menu = Modules["Menu"]
	local HUD = Modules["HUD"]
	local Placement = Modules["Placement"]
	local Busy = false
	-- open up xbox chat
	--local function OpenXboxChat()
	--	local Visible = not script.Parent.XboxMessage.Visible
	--	if Visible then
	--		Modules.Focus.change(script.Parent.XboxMessage)
	--		game:GetService("GuiService"):AddSelectionParent("XboxChat",script.Parent.XboxMessage)
	--		game:GetService("GuiService").SelectedObject = script.Parent.XboxMessage.TextBox
	--		game:GetService("GuiService").GuiNavigationEnabled = true
	--	end
	--end

	-- Placing
	--game.ContextActionService:BindActionAtPriority("XboxChat",OpenXboxChat,false,Enum.ContextActionPriority.High,Enum.KeyCode.ButtonSelect)

	USI.InputEnded:Connect(function(Input,Processed)
		--if Input.KeyCode == Enum.KeyCode.ButtonSelect then OpenXboxChat() end
		if Processed then return false end
		if not Processed and not Busy then
			local InputVal = Input.UserInputType.Value
			if (InputVal == 8) or (InputVal >= 0 and InputVal <= 4) then
				module.mode.Value = "PC"
			elseif InputVal == 7 then
				module.mode.Value = "Mobile"
			elseif InputVal >= 12 and InputVal <= 19 then
				module.mode.Value = "Xbox"
			end
			local Focused = (script.Parent.FocusWindow.Value and script.Parent.FocusWindow.Value.Visible)
			local Tycoon = game.Players.LocalPlayer.PlayerTycoon.Value
			-- XBOX CONTROLS
			if Input.KeyCode == Enum.KeyCode.ButtonA then
				if script.Parent.ItemPreview.Frame.Expanded.Value then
					Modules.Placement.quickplace()
				end
			elseif Modules.Placement.placing and Input.KeyCode == Enum.KeyCode.DPadUp then
				Modules.Placement.raise()
			elseif Modules.Placement.placing and Input.KeyCode == Enum.KeyCode.DPadDown then
				Modules.Placement.lower()
			elseif Input.KeyCode == Enum.KeyCode.ButtonB then
				if script.Parent.Shop.Visible then
					if script.Parent.Shop.Confirm.Visible then
						for i,Button in script.Parent.Shop.Frame.Items:GetChildren() do
							if Button.Name == script.Parent.Shop.SelectedItem.Value.Name then
								game.GuiService.SelectedObject = Button
							end
						end
						script.Parent.Shop.SelectedItem.Value = nil
					elseif script.Parent.Shop.Mode.Value ~= "Default" then
						local OldMode = script.Parent.Shop.Mode.Value
						local Button = script.Parent.Shop.Frame.SideBar:FindFirstChild(OldMode)
						script.Parent.Shop.Mode.Value = "Default"
						if Button then
							--script.Parent.Shop.Frame.SideBar.Visible = true
							game.GuiService.SelectedObject = Button
						end
					else
						Modules.Focus.close()
					end
				elseif Modules.Placement.placing then
					Modules.Placement.stopPlacing()
				elseif Focused then
					Modules.Focus.close()
				elseif script.Parent.ItemPreview.Frame.Expanded.Value then
					Modules.Preview.Collapse()
				end
			elseif Input.KeyCode == Enum.KeyCode.DPadUp then
				if Modules.Preview.expanded ~= nil and script.Parent.ItemPreview.Visible and script.Parent.ItemPreview.Frame.Expanded.Value then
					Modules.Placement.quickplace()
				end
			elseif Input.KeyCode == Enum.KeyCode.DPadRight then
				if not Focused then
					Modules.Focus.change(script.Parent.HUDRight)
					script.Parent.HUDRight.XboxKey.Close.Visible = true
					script.Parent.HUDRight.XboxKey.Open.Visible = false
				end
			elseif Input.KeyCode == Enum.KeyCode.DPadDown then
				if not Focused then
					Modules.HUDRight.OpenBoxes()
				end
			elseif Input.KeyCode == Enum.KeyCode.ButtonX then
				if Tycoon then
					if script.Parent.ItemPreview.Frame.Expanded.Value then
						Modules.Placement.withdraw()
					else
						Modules.HUDLeft.ToggleInventory()
					end
				end
			elseif Input.KeyCode == Enum.KeyCode.ButtonY then
				if Tycoon then
					if script.Parent.ItemPreview.Frame.Expanded.Value then
						Modules.Placement.quicksell()
					elseif Modules.Placement.placing then
						Modules.Placement.undo()
					else
						--Modules.HUDLeft.ToggleShop()
					end
				end
			elseif Input.KeyCode == Enum.KeyCode.ButtonL2 then
				if Modules.Placement.placing then
					Modules.Placement.rotate()
				end
			elseif Input.KeyCode == Enum.KeyCode.ButtonL1 then
				if script.Parent.Inventory.Visible then
					Modules.HUDLeft.ToggleSettings()
				elseif Modules.Placement.placing then
					Modules.Placement.shiftleft()
				end
			elseif Input.KeyCode == Enum.KeyCode.ButtonR1 then
				if script.Parent.Shop.Visible then
					Modules.Focus.change(script.Parent.Premium)
				elseif Modules.Placement.placing then
					Modules.Placement.shiftright()
				elseif script.Parent.ItemInfo.Info.XboxControl.Visible then
					local itemLabel = script.Parent.ItemInfo.RealItem.Value
					if itemLabel then
						local Success = game.ReplicatedStorage.RFunctions.ToggleFavorite:InvokeServer(itemLabel.ItemName.Value)
						if not Success then
							Modules.Menu.sounds.Error:Play()
						else
							Modules.ItemInfo.hide(itemLabel)
							Modules.Inventory.favorite(itemLabel)
						end
					end
				end
				-- PC CONTROLS
			elseif Input.KeyCode == Enum.KeyCode.E then
				Modules.HUDLeft.ToggleInventory()
			end
			if Modules.Placement.placing then
				if Input.KeyCode == Enum.KeyCode.R then
					Modules.Placement.rotate()
				elseif Input.KeyCode == Enum.KeyCode.Q then
					Modules.Placement.stopPlacing()
				elseif Input.KeyCode == Enum.KeyCode.F then
					Modules.Placement.undo()
				elseif Input.KeyCode == Enum.KeyCode.Z then
					Modules.Placement.shiftleft()
				elseif Input.KeyCode == Enum.KeyCode.C then
					Modules.Placement.shiftright()
				elseif Input.KeyCode == Enum.KeyCode.One or Input.KeyCode == Enum.KeyCode.KeypadOne then
					Modules.Placement.lower()
				elseif Input.KeyCode == Enum.KeyCode.Two or Input.KeyCode == Enum.KeyCode.KeypadTwo	then
					Modules.Placement.raise()
				end
			elseif script.Parent.ItemPreview.Frame.Expanded.Value then
				if Input.KeyCode == Enum.KeyCode.Z then
					Modules.Placement.withdraw()
				elseif Input.KeyCode == Enum.KeyCode.R then
					Modules.Placement.quickplace()
				elseif Input.KeyCode == Enum.KeyCode.X then
					Modules.Placement.quicksell()
				elseif Input.KeyCode == Enum.KeyCode.C then
					Modules.Placement.quickbuy()
				end
			else
				if Input.KeyCode == Enum.KeyCode.M then
					Modules.HUDLeft.ToggleMaps()
				elseif Input.KeyCode == Enum.KeyCode.C then
					Modules.HUDLeft.ToggleSettings()
				elseif Input.KeyCode == Enum.KeyCode.P then
					Modules.HUDLeft.TogglePremium()
				elseif Input.KeyCode == Enum.KeyCode.F and game:GetService("RunService"):IsStudio() then
					Modules.HUDLeft.ToggleShop()
				end
			end
		end
	end)

	local function Scan()
		for i,Object in script.Parent:GetDescendants() do
			if Object:IsA("GuiObject") then
				if Object:FindFirstChild("PC") or Object:FindFirstChild("Xbox") or Object:FindFirstChild("Mobile") then
					Object.Visible = Object:FindFirstChild(module.mode.Value)
				end
			end
		end
	end
	Scan()
	module.mode.Changed:Connect(Scan)
	print(module.mode.Value)
end

return module