glick = HeldItem:new()
itemTable = glick:new{
	emitBoneName = "glick_base",
	emitBoneOffsetX = -20,
	emitBoneOffsetY = 9,
	emitSpeed = 80,
	numProjectiles = 8,
	firing = false,
	reloading = false,
}

function glick:initialize()
	self.item:LoadSoundEffect("fire", 			"SharedAssets/SoundFX/Guns/9mm_fire.wav")
	self.item:LoadSoundEffect("pullSlide", 		"SharedAssets/SoundFX/Guns/handgun_slide_pull.wav")
	self.item:LoadSoundEffect("releaseSlide", 	"SharedAssets/SoundFX/Guns/handgun_slide_release.wav")
	self.item:LoadSoundEffect("magPull", 		"SharedAssets/SoundFX/Guns/handgun_magazine_pull.wav")
	self.item:LoadSoundEffect("magInsert", 		"SharedAssets/SoundFX/Guns/handgun_magazine_insert.wav")
	self.item:LoadSoundEffect("click", 			"SharedAssets/SoundFX/Guns/gun_dry_fire.wav")

	self.item._holdAnimationPlayer:StartAnimation("hold")
end

function glick:start()
end

function glick:equip()
	self.reloading = false
	self.item._holdAnimationPlayer:StartAnimation("hold")
end

function glick:unequip()
end

function glick:keyframeTriggerEvent(triggerString)
	MeleeWeapon.keyframeTriggerEvent(self, triggerString)
	
	if triggerString == "sfx_slidePull" then
		self.item:PlaySoundEffect("pullSlide", 1)
	elseif triggerString == "sfx_slideRelease" then
		self.item:PlaySoundEffect("releaseSlide", 1)
	elseif triggerString == "sfx_magPull" then
		self.item:PlaySoundEffect("magPull", 1)
		elseif triggerString == "sfx_magInsert" then
		self.item:PlaySoundEffect("magInsert", 1)
	end
end

function glick:animationCompleted(animationName)
	if animationName == "fire" then
		self.item:TransitionToAnimation("hold", 0)
	elseif animationName == "reload" then
		self.reloading = false
		self.item:TransitionToAnimation("hold", .25)
		local numBulletsRemaining = self.player:GetItemQuantity(self.item:GetCurAmmoType())
		if numBulletsRemaining >= self.item.MaxClipSize then
			self.item.CurClipSize = self.item.MaxClipSize
		else
			self.item.CurClipSize = numBulletsRemaining
		end
	end
end

function glick:primaryAttackStart()
	if self.item.CurClipSize <= 0 then 
		self:reloadStart()
	elseif self.item:IsCurAmmoValid() ~= true then
		self.item:TryEquipAmmo()
	end
end

function glick:primaryAttackUpdate()	
	if self.item:IsTransitioning() ~= true and self.item:GetCurrentAnimationName() == "hold" then
		if self.player:HasItemQuantity(self.item:GetCurAmmoType(), 1) and self.item.CurClipSize > 0 then
			self:tryFire()
		end
	end
end

function glick:reloadStart()
	if self.firing == false and self.reloading == false then
		self.item:TransitionToAnimation("reload", .25)
		self.reloading = true
	end
end

function glick:tryFire()
	-- Check to see that the player has enough ammo in their inventory as well as in the weapon's clip
	if self.player:HasItemQuantity(self.item:GetCurAmmoType(), 1) and self.item.CurClipSize > 0 then
		self.item:PlaySoundEffect("fire", 1, (.2 * math.random() - .1))
		-- Do a muzzle flash that emits from the same spot that the projectile is fired from.
		self.item:MuzzleFlash(self.emitBoneName, self.emitBoneOffsetX, self.emitBoneOffsetY)
		-- Transition very quickly to the fire animation
		self.item:TransitionToAnimation("fire", .01)
		self.item:FireProjectile("ROUND_9MM", self.emitSpeed, self.emitBoneName, self.emitBoneOffsetX, self.emitBoneOffsetY)
		-- We fired a projectile, so reduce the clip size by 1
		self.item.CurClipSize = self.item.CurClipSize - 1
		self.player:RemoveItemQuantity(self.item:GetCurAmmoType(), 1)
	else
		self.item:PlaySoundEffect("click", .25, (.2 * math.random() - .1))
	end
end

return itemTable