--------------------[ RELOAD FUNCTIONS ]----------------------------------------------


function animateReload()
	tweenJoint(LWeld2, CF(), CF(), Sine, 0.15)
	tweenJoint(RWeld2, CF(), CF(), Sine, 0.15)
	local magParts = {}
	local magTable = {}
			
	for _, Obj in pairs(Gun:GetChildren()) do
		if string.sub(Obj.Name, 1, 3) == "Mag" and Obj:IsA("BasePart") then
			INSERT(magParts, Obj)
		end
	end
	
	local animVars = {
		--FUNCTIONS--
		tweenJoint = tweenJoint;
		
		makeMagInvisible = function()
			for _, v in pairs(magParts) do
				v.Transparency = 1
			end
			magVisible = false
		end;
		
		makeMagVisible = function()
			for _, v in pairs(magParts) do
				v.Transparency = v:WaitForChild("magTrans").Value
			end
			magVisible = true
		end;
		
		isMagVisible = function()
			return magVisible
		end;
		
		isMagEmpty = function()
			return ammoInClip == 0
		end;
		
		setNewMag = function()
			newMag = true
		end;
		
		isNewMag = function()
			return newMag
		end;
		
		createMag = function(Key)
			local magModel = Instance.new("Model")
			local magClones = {}
			for i, v in pairs(magParts) do
				local vClone = v:Clone()
				vClone.Transparency = v:WaitForChild("magTrans").Value
				vClone.CanCollide = false
				vClone.Parent = magModel
				INSERT(magClones, {Original = v, magClone = vClone})
				if i ~= 1 then
					local W = Instance.new("Weld")
					W.Part0 = magClones[1].magClone
					W.Part1 = vClone
					W.C0 = magClones[1].magClone.CFrame:toObjectSpace(vClone.CFrame)
					W.Parent = magClones[1].magClone
				end
			end
			magTable[Key] = {magModel, magClones}
			return magModel, magClones
		end;
		
		getMag = function(Key)
			if magTable[Key] then
				return magTable[Key][1], magTable[Key][2]
			else
				return nil, nil
			end
		end;
		
		attachGripToHead = function()
			local handleCF = RArm.CFrame * Grip.C0
			Grip.C0 = Head.CFrame:toObjectSpace(handleCF)
			Grip.Part0 = Head
		end;
		
		attachGripToArm = function()
			local handleCF = Head.CFrame * Grip.C0
			Grip.C0 = RArm.CFrame:toObjectSpace(handleCF)
			Grip.Part0 = RArm
		end;
		
		Sine = Sine;
		
		Linear = Linear;
		
		--VARIABLES--
		Handle = Handle;
		LArm = LArm;
		RArm = RArm;
		LWeld = LWeld;
		RWeld = RWeld;
		LC0 = armC0[1];
		RC0 = armC0[2];
		Grip = Grip;
		gunIgnore = gunIgnore;
		Cam = Cam;
		CF = CF;
		CFANG = CFANG;
		V3 = V3;
		RAD = RAD;
		reloadTimeLoaded = S.reloadSettings.Times.Loaded;
		reloadTimeEmpty = S.reloadSettings.Times.Empty
	}
	
	local sequenceTable = Anims.Reload(animVars)
	--local T = tick()
	for _, reloadFunction in pairs(sequenceTable) do
		if breakReload then
			break
		end
		reloadFunction()
		
		if (not magVisible) then
			Ammo.Value = 0
		end
		updateClipAmmo()
	end
	--print(tick() - T) --I divide the reloadTime by this number to get the animation speed
	
	if (not isCrawling) then
		if Running and (not S.canFireWhileRunning) then
			tweenJoint(LWeld, armC0[1], S.runningC1.leftArm, Sine, 0.4)
			tweenJoint(RWeld, armC0[2], S.runningC1.rightArm, Sine, 0.4)
			tweenJoint(Grip, nil, S.runningC1.Grip, Sine, 0.4)
		else
			tweenJoint(LWeld, armC0[1], S.unAimedC1.leftArm, Sine, 0.4)
			tweenJoint(RWeld, armC0[2], S.unAimedC1.rightArm, Sine, 0.4)
			tweenJoint(Grip, nil, S.unAimedC1.Grip, Sine, 0.4)
		end
	end
	
	for _, v in pairs(magTable) do --In case the reload animations was stopped mid way and there were still fake mags that weren't deleted
		v[1]:Destroy()
	end
end

function Reload()
	if Ammo.Value < (ClipSize.Value + 1) and (not Reloading) and StoredAmmo.Value > 0 then
		Firing = false
		ammoInClip = (ammoInClip == 0 and Ammo.Value or ammoInClip)
		Reloading = true
		lowerSpread()
		if Aimed then unAimGun(S.reloadSettings.Anim) end
		crossHair.Reload.Visible = true
		if Handle:FindFirstChild("ReloadSound") then Handle.ReloadSound:Play() end
		if S.reloadSettings.Anim then
			wait()
			animateReload()
		else
			local startReload = tick()
			local initialReloadTime = Ammo.Value == 0 and S.reloadSettings.Times.Empty or S.reloadSettings.Times.Loaded
			while true do
				if breakReload then break end
				if (tick() - startReload) >= initialReloadTime then break end
				RS.RenderStepped:wait()
			end
		end
		if (not breakReload) then
			newMag = false
			if StoredAmmo.Value >= ClipSize.Value then
				if ammoInClip > 0 then
					StoredAmmo.Value = StoredAmmo.Value - ((ClipSize.Value + 1) - ammoInClip)
					Ammo.Value = ClipSize.Value + 1
				else
					StoredAmmo.Value = StoredAmmo.Value - ClipSize.Value
					Ammo.Value = ClipSize.Value
				end
			elseif StoredAmmo.Value < ClipSize.Value and StoredAmmo.Value > 0 then
				Ammo.Value = StoredAmmo.Value
				StoredAmmo.Value = 0
			end
		end
		Reloading = false
		if Selected then
			ammoInClip = (breakReload and ammoInClip or 0)
			crossHair.Reload.Visible = false
		end
		breakReload = false
	end
	
	updateClipAmmo()
	updateStoredAmmo()
end
