tahz = HeldItem:new()
itemTable = tahz:new{
	emitBoneName = "tahz_base",
	emitBoneOffsetX = -51,
	emitBoneOffsetY = 9,
	minEmitSpeed = 50,
	maxEmitSpeed = 70,
	numProjectiles = 8,
	firing = false,
	reloading = false,
}

function tahz:initialize()
	self.item:LoadSoundEffect("fire", 			"Items/RangedWeapons/Tahz/SoundFX/tahz_fire.wav")
	self.item:LoadSoundEffect("pullBolt", 		"SharedAssets/SoundFX/Guns/gun_bolt_slideBack.wav")
	self.item:LoadSoundEffect("releaseBolt", 	"SharedAssets/SoundFX/Guns/gun_bolt_slideForward.wav")
	self.item:LoadSoundEffect("magPull", 		"SharedAssets/SoundFX/Guns/gun_magazine_pull.wav")
	self.item:LoadSoundEffect("magInsert", 		"SharedAssets/SoundFX/Guns/gun_magazine_insert.wav")
	self.item:LoadSoundEffect("click", 			"SharedAssets/SoundFX/Guns/gun_dry_fire.wav")

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

function tahz:start()
end

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

function tahz:unequip()
end

function tahz:keyframeTriggerEvent(triggerString)
	MeleeWeapon.keyframeTriggerEvent(self, triggerString)
	
	if triggerString == "sfx_pullBolt" then
		self.item:PlaySoundEffect("pullBolt", 1)
	elseif triggerString == "sfx_releaseBolt" then
		self.item:PlaySoundEffect("releaseBolt", 1)
	elseif triggerString == "sfx_magPull" then
		self.item:PlaySoundEffect("magPull", 1)
		elseif triggerString == "sfx_magInsert" then
		self.item:PlaySoundEffect("magInsert", 1)
	end
end

function tahz: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 tahz:primaryAttackStart()
	if self.item.CurClipSize <= 0 then 
		self:reloadStart()
	elseif self.item:IsCurAmmoValid() ~= true then
		self.item:TryEquipAmmo()
	end
end

function tahz: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 tahz:reloadStart()
	if self.firing == false and self.reloading == false then
		self.item:TransitionToAnimation("reload", .25)
		self.reloading = true
	end
end

function tahz: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)
		for i = 0,self.numProjectiles,1
		do
			-- Randomize the projectile speed a bit. Looks better that way.
			local emitSpeed = math.random(self.minEmitSpeed, self.maxEmitSpeed)
			self.item:FireProjectile("SHOTGUN_PELLET", emitSpeed, self.emitBoneName, self.emitBoneOffsetX, self.emitBoneOffsetY)
		end
		-- 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