-- Updating weapons HUD


function updateWeaponHud(mag, ammo)
	clipUI.Text = mag
	ammoUI.Text = ammo
	ammoUI.AmmoEffect.Text = ammo
	if ammo > ammoLeft then
		spawn(ammoEffect)
	end
	if mag == 0 then
		clipUI.TextColor3 =  Color3.new(227/255, 29/255, 32/255)
	else
		clipUI.TextColor3 = Color3.new(199/255, 199/255, 199/255)
	end
	if ammo == 0 then
		ammoUI.TextColor3 =  Color3.new(227/255, 29/255, 32/255)
	else
		ammoUI.TextColor3 = Color3.new(199/255, 199/255, 199/255)
	end
	currentAmmo = mag
	ammoLeft = ammo
end

if configs.infiniteAmmo == true then
	updateWeaponHud(currentAmmo, clipSize)
else
	updateWeaponHud(currentAmmo, ammoLeft)
end

function equip()
	local char = player.Character

	-- Setup joint variables
	if player.Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
		neck = player.Character.Torso.Neck
		oldNeckC0 = neck.C0
		shoulder = player.Character.Torso:FindFirstChild("Right Shoulder")
		oldShoulderC0 = shoulder.C0
	else
		neck = player.Character.Head.Neck
		oldNeckC0 = neck.C0
		shoulder = player.Character.RightUpperArm:FindFirstChild("RightShoulder")
		oldShoulderC0 = shoulder.C0
	end

	-- Remember old mouse icon and update current
	hud.AmmoHud:TweenPosition(UDim2.new(0.98, 0, 0.95, 0), "InOut", "Quad", 0.3, true)
	runService:BindToRenderStep("Sights", Enum.RenderPriority.Last.Value, function()
		hud.Crosshair.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
	end)
	userInputService.MouseIconEnabled = false
	hud.Crosshair.Visible = true

	-- Bind TouchMoved event if on mobile. Otherwise connect to renderstepped
	if userInputService.TouchEnabled then
		connection = userInputService.TouchMoved:Connect(mobileFrame)
	else
		connection = render:Connect(pcFrame)
	end

	-- Bind TouchStarted and TouchEnded. Used to determine if character should rotate
	-- during touch input
	userInputService.TouchStarted:Connect(function(touch, processed)
		mobileShouldTrack = not processed
	end)
	userInputService.TouchEnded:Connect(function(touch, processed)
		mobileShouldTrack = false
	end)

	-- Fire server's equip event
	game.ReplicatedStorage:WaitForChild("ROBLOX_PistolEquipEvent"):FireServer()

	-- Bind event for when mouse is clicked to fire server's fire event
	mouse.Button1Down:Connect(function()
		game.ReplicatedStorage:WaitForChild("ROBLOX_PistolFireEvent"):FireServer(mouse.Hit.p)
		updateWeaponHud(currentAmmo, ammoLeft)
	end)

	-- Bind reload event to mobile button and r key
	contextActionService:BindActionToInputTypes("Reload", function()
		game.ReplicatedStorage:WaitForChild("ROBLOX_PistolReloadEvent"):FireServer()
	end, true, "r")

	-- If game uses filtering enabled then need to update server while tool is
	-- held by character.
	if workspace.FilteringEnabled then
		while connection do
			wait()
			game.ReplicatedStorage:WaitForChild("ROBLOX_PistolUpdateEvent"):FireServer(neck.C0, shoulder.C0)
		end
	end
end

local function unequip()
	if connection then connection:disconnect() end
	contextActionService:UnbindAction("Reload")
	game.ReplicatedStorage:WaitForChild("ROBLOX_PistolUnequipEvent"):FireServer()
	hud.AmmoHud:TweenPosition(UDim2.new(1.5, 0, 0.95, 0), "InOut", "Quad", 0.3, true)
	runService:UnbindFromRenderStep("Sights")
	userInputService.MouseIconEnabled = true
	hud.Crosshair.Visible = false
	neck.C0 = oldNeckC0
	shoulder.C0 = oldShoulderC0
end

function hitmarkerUpdate()
	hud.Crosshair.Hitmarker.Visible = true
	wait(0.1)
	hud.Crosshair.Hitmarker.Visible = false
end