--CasingMesh.Scale = Vector3.new(.75,.75,.75)
CasingMesh.Parent = CasingBase

function OnFire()
	if IsShooting then return end
	if MyHumanoid and MyHumanoid.Health > 0 then
		if RecoilTrack and AmmoInClip > 0 then
			
		end
		IsShooting = true
		if AmmoInClip > 0 and not Reloading then
			--if RecoilTrack then RecoilTrack:Play() end
			if Handle:FindFirstChild('FireSound') then
				Handle.FireSound:Play()
			end
			CreateFlash()
			Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent), "rbxasset://textures\\GunWaitCursor.png")
			local targetPoint = MOUSE_TARGET
			for i = 1, BulletCount do
				local shootDirection = (targetPoint - Handle.Position).unit
				-- Adjust the shoot direction randomly off by a little bit to account for recoil
				-- moves random*spread in a random angle from center shot point (center bias)
				shootDirection = ((CFrame.new(Vector3.new(0,0,0),shootDirection) * CFrame.Angles(0,0,math.random()*2*math.pi))*CFrame.Angles(math.random()*Spread,0,0)).lookVector
				local hitObject, bulletPos = RayCast(Handle.Position, shootDirection, Range)
				local bullet
				-- Create a bullet here
				if hitObject then
					bullet = CreateBullet(bulletPos, hitObject)
				end
				if hitObject and hitObject.Parent then
					local hitHumanoid = hitObject.Parent:FindFirstChild("Humanoid") or hitObject.Parent.Parent:FindFirstChild'Humanoid'
					if hitHumanoid then
						local hitPlayer = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent)
						if not GLib.IsTeammate(MyPlayer, hitPlayer) then
							GLib.TagHumanoid(MyPlayer, hitHumanoid, 1)
							hitHumanoid:TakeDamage(Damage)
							if bullet then
								bullet:Destroy()
								bullet = nil
								--bullet.Transparency = 1
							end
						end
					end
				end
			end

			local casing = CasingBase:Clone()
			casing.Position = Tool.Handle.Position + Vector3.new(0,1,0)
			casing.Velocity = (Vector3.new((math.random()-.5),(.5+math.random()),(math.random()-.5)) - 1.5*(Tool.Handle.CFrame * CFrame.Angles(0,math.pi/4,0)).lookVector)*20
			DebrisService:AddItem(casing, 2.5)
			casing.Parent = game.Workspace

			AmmoInClip = AmmoInClip - 1
			UpdateAmmo(AmmoInClip)
			wait(FireRate-.2)

			if AmmoInClip > 0 then
				if PumpTrack then
					PumpTrack:Play()
				end
				if Handle:FindFirstChild('PumpSound') then
					Handle.PumpSound:Play()
				end
				wait(.2)
			end
			Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent), "rbxasset://textures\\GunCursor.png")
		end		
		IsShooting = false
		if AmmoInClip == 0 then
			Reload()
		end
		if RecoilTrack then
			RecoilTrack:Stop()
		end
	end
end

function UpdateAmmo(value)
	if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then
		WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip
	end
	--if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('TotalAmmo') then
	--	WeaponGui.AmmoHud.TotalAmmo.Text = SpareAmmo
	--end
end


function OnMouseDown()
	LeftButtonDown = true
	OnFire()
end

function OnMouseUp()
	LeftButtonDown = false
end

function OnKeyDown(key)
	if string.lower(key) == 'r' then
		Reload()
	end
end

Tool.Input.OnServerEvent:connect(function(client, action, ...)
	if client.Character == Tool.Parent then
		if action == 'Mouse1' then
			local down, pos = ...
			
			if down then
				OnMouseDown()
			else
				OnMouseUp()
			end
		elseif action == 'Key' then
			local down, key = ...
			
			if down then
				OnKeyDown(key)
			end
		elseif action == 'MouseMove' then
			local pos = ...
			
			MOUSE_TARGET = pos
		end
	end
end)


function OnEquipped()
	EquipAnim = WaitForChild(Tool, 'Equip')
	PumpAnim = WaitForChild(Tool, 'Reload')
	FireSound  = WaitForChild(Handle, 'FireSound')

	MyCharacter = Tool.Parent
	MyPlayer = game:GetService('Players'):GetPlayerFromCharacter(MyCharacter)
	MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
	MyTorso = MyCharacter:FindFirstChild('Torso')
	WeaponGui = WaitForChild(Tool, 'WeaponHud'):Clone()
	if WeaponGui and MyPlayer then
		WeaponGui.Parent = MyPlayer.PlayerGui
		UpdateAmmo(AmmoInClip)
	end
	if EquipAnim then
		Tool.Grip = CFrame.new(0.132808685, 0.00749024749, 0.526850462, 0.925221086, 0.000443253666, 0.379427731, 0.00859767944, 0.999718785, -0.0221330915, -0.379331142, 0.0237402022, 0.924956799)
		EquipTrack = MyHumanoid:LoadAnimation(EquipAnim)
		EquipTrack:Play()
	end
	if PumpAnim then
		PumpTrack = MyHumanoid:LoadAnimation(PumpAnim)
	end

	Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent), "rbxasset://textures\\GunCursor.png")
end