if CLIENT then 
    SWEP.WepSelectIcon = surface.GetTextureID( "vgui/hud/item_gene_therapy" )
	SWEP.BounceWeaponIcon = true 
    SWEP.DrawWeaponInfoBox = true
end

SWEP.PrintName = "Genetherapy"
SWEP.Author = "Craft_Pig"
SWEP.Purpose = 
[[                    					
]]
SWEP.Category = "NMRIH"

SWEP.ViewModelFOV = 65
SWEP.ViewModel = "models/weapons/nmrih/items/genetherapy/v_item_genetherapy.mdl"
SWEP.WorldModel = "models/weapons/nmrih/items/genetherapy/w_genetherapy.mdl"
SWEP.UseHands = true
SWEP.DrawCrosshair = false 

SWEP.Spawnable = true
SWEP.Slot = 0
SWEP.SlotPos = 5
SWEP.DrawAmmo = true

SWEP.Primary.Ammo = "genetherapy"
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = 1
SWEP.Primary.Automatic = false

SWEP.Secondary.Ammo = "none"
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false

SWEP.HitDistance = 90
SWEP.PrimaryHitTime = 0.1

local HealAmount = 0
local ArmorAmount = 50
local InitializeSEF = false

function SWEP:Initialize()
    self:SetHoldType("slam")
	
	local FilePathSEF = "lua/SEF/SEF_Functions.lua"
    if file.Exists(FilePathSEF, "GAME") then
        InitializeSEF = true
    else
        InitializeSEF = false
    end
end 

function SWEP:Deploy()
    local owner = self:GetOwner() 
	
    self:SendWeaponAnim(ACT_VM_DRAW)
    self.IdleTimer = CurTime() + self.Owner:GetViewModel():SequenceDuration()
	self.Idle = 0
	self.Sprint = 0
	self.Walk = 0
	self.Give = 0
	self.InitializeHealing = 0
	self.vmcamera = nil

	if owner:GetAmmoCount(self.Primary.Ammo) == 0 then owner:StripWeapon("weapon_nmrih_genetherapy") end -- Reminder
	return true
end

local function Heal(owner, weapon)
    local activeWeapon = owner:GetActiveWeapon()

    if IsValid(weapon) then
        if IsValid(owner) and SERVER and activeWeapon:GetClass() == "weapon_nmrih_genetherapy" then -- Reminder
		
		    if InitializeSEF == true then
			    owner:ApplyEffect("Tenacity", math.huge)
			else
                owner:SetHealth(math.min(owner:GetMaxHealth(), owner:Health() + HealAmount))
                owner:SetArmor(math.min(owner:GetMaxArmor(), owner:Armor() + ArmorAmount))
			end
            owner:RemoveAmmo(1, "genetherapy") -- Reminder
            -- owner:EmitSound("nil")
            weapon:Deploy()
        end
    end
end 

function SWEP:PrimaryAttack()
    local owner = self.Owner

    if owner:GetAmmoCount(self.Primary.Ammo) == 0 then return end

    self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
	self:SetNextPrimaryFire(CurTime() + self.Owner:GetViewModel():SequenceDuration() + 0)
	self.IdleTimer = CurTime() + self.Owner:GetViewModel():SequenceDuration()
	self.InitializeHealing = 1
	self.Idle = 1
end

function SWEP:SecondaryAttack()
	local owner = self:GetOwner()
	local dmginfo = DamageInfo()
	local tr = util.TraceLine( {
			start = owner:GetShootPos(),
			endpos = owner:GetShootPos() + owner:GetAimVector() * self.HitDistance,
			filter = owner,
			mask = MASK_SHOT_HULL
	    } )
	if ( !IsValid( tr.Entity ) ) then
		tr = util.TraceHull( {
			start = owner:GetShootPos(),
			endpos = owner:GetShootPos() + owner:GetAimVector() * self.HitDistance,
			filter = owner,
			mins = Vector( -10, -10, -8 ),
			maxs = Vector( 10, 10, 8 ),
		    mask = MASK_SHOT_HULL
		} )
	end
	
	if self.InitializeHealing == 1 then return end
	
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
	
	self:SendWeaponAnim(ACT_VM_HITCENTER) 
	self.IdleTimer = CurTime() + self.Owner:GetViewModel():SequenceDuration()
	self:SetNextSecondaryFire(CurTime() + 0.9)
	
	self.Idle = 0
	self.Sprint = 0
	self.Walk = 0
	 
	timer.Create("TimerHitWhen", self.PrimaryHitTime, 1, function() -- Impact Timer
	    if SERVER then
	        if ( SERVER and IsValid( tr.Entity ) and ( tr.Entity:IsNPC() or tr.Entity:IsPlayer() or tr.Entity:Health() > 0 ) ) then
                dmginfo:SetDamage(10)
                dmginfo:SetAttacker(self.Owner)
                dmginfo:SetInflictor(self)
                dmginfo:SetDamageType(DMG_CRUSH)
                tr.Entity:TakeDamageInfo(dmginfo)
		    end
	    end        
    end)
end

function SWEP:Reload()
    local owner = self:GetOwner()
    local trace = owner:GetEyeTrace()
	
	if self.Give == 1 then return end

    if IsValid(trace.Entity) and trace.Entity:IsPlayer() then
        local targetPlayer = trace.Entity
		
		self:SendWeaponAnim(ACT_VM_THROW)
		self.IdleTimer = CurTime() + self.Owner:GetViewModel():SequenceDuration()
		self.Idle = 0
		self.Sprint = 0
		self.Walk = 0
		self.Give = 1
		
		owner:RemoveAmmo(1, "genetherapy") 
		
		if CLIENT then return end
		timer.Create("TimerNMRIHBandageGive", 1.2, 1, function()
			if targetPlayer:HasWeapon("weapon_nmrih_genetherapy") then
				targetPlayer:GiveAmmo( 1, "genetherapy")
			else
				targetPlayer:Give("weapon_nmrih_genetherapy")
			end
				-- print(owner:Nick() .. " gave a bandage to " .. targetPlayer:Nick())
				self:Deploy()
		end)  
    end
end

function SWEP:Think()
	local owner = self.Owner
	if self.InitializeHealing == 1 and self.IdleTimer <= CurTime() then
	    if IsValid(self) then
            Heal(owner, self)
		end
	end
	
	if self.InitializeHealing == 1 then return end
    if (owner:KeyDown(IN_FORWARD + IN_BACK + IN_MOVELEFT + IN_MOVERIGHT) and owner:KeyDown(IN_SPEED)) then -- If Sprinting
			if self.Sprint == 0 and self.IdleTimer <= CurTime() then
				self:SendWeaponAnim(ACT_VM_SPRINT_IDLE) 
				self.Sprint = 1	
				self.Walk = 0
				self.Idle = 0	
			end
		elseif (owner:KeyDown(IN_FORWARD + IN_BACK + IN_MOVELEFT + IN_MOVERIGHT)) then -- If Walking
			if self.Walk == 0 and self.IdleTimer <= CurTime() then
				self:SendWeaponAnim(ACT_VM_IDLE_LOWERED) 
				self.Walk = 1
				self.Sprint = 0				
				self.Idle = 0	
			end	
		elseif self.Idle == 0 and self.IdleTimer <= CurTime() then -- Idle Sequence
			self:SendWeaponAnim(ACT_VM_IDLE) 
			self.Idle = 1
			self.Sprint = 0	
			self.Walk = 0
	end 
	
	
end

function SWEP:Holster()
	timer.Remove("TimerHitWhen")
	timer.Remove("TimerNMRIHBandageGive")
    return true
end

function SWEP:PostDrawViewModel( vm )
    local attachment = vm:GetAttachment(1)
    if attachment then
        self.vmcamera = vm:GetAngles() - attachment.Ang
    else
        self.vmcamera = Angle(0, 0, 0) 
    end
end

function SWEP:CalcView( ply, pos, ang, fov )
	self.vmcamera = self.vmcamera or Angle(0, 0, 0)  
    return pos, ang + self.vmcamera, fov
end
