AddCSLuaFile()

SWEP.PrintName = "Jerry Can"

SWEP.Slot = 0
SWEP.SlotPos = 4
SWEP.Instructions = ""

SWEP.Spawnable = true

SWEP.ViewModel = "models/surrounded/obj/v_JerryCan.mdl"
SWEP.WorldModel = "models/surrounded/obj/w_JerryCanBlue.mdl"
SWEP.ViewModelFOV = 90
SWEP.UseHands = true

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

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

SWEP.DrawAmmo = false

function SWEP:Initialize()
	self:SetHoldType("slam")
end

function SWEP:PrimaryAttack() 
	if CLIENT then return end

	local ply = self:GetOwner()
	local tr = util.TraceLine({
		start = ply:EyePos(),
		endpos = ply:EyePos() + ply:GetAimVector()*64,
		filter = ply,
	})	
	local ent = tr.Entity
	if IsValid(ent) and ent:GetClass() == "sur_generator" and !ent:GetNWBool('HaveFuel') then
		self:SetNextPrimaryFire(CurTime()+10)

		local anim = "sur_fuel_up"
		local time = select(2, ply:LookupSequence(anim))
		ply:SetSVAnimation(anim, true, true, true)
		ply:EmitSound("surrounded/weapons/fuel/sfx_sequence_fuel.wav", 60, math.random(80,110))
		ply:Freeze(true)
		timer.Simple(time-0.1, function()
			if !IsValid(ply) or ply:GetSVAnim() != anim then return end
			ply:Freeze(false)

			if !IsValid(ent) then return end
			ent:Fuel()

			if !IsValid(self) then return end
			self:Remove()
			ply:AddXP(SuR.Config.XP_Giving["fuel"])
			SuR:MessageOnClient(19, ply:Nick())
		end)
	end
end

function SWEP:SecondaryAttack() 
	if CLIENT then return end

	local ply = self:GetOwner()
	ply:ConCommand("sur_dropweapon sur_fuel")
	self:SetNextSecondaryFire(CurTime()+2)
end

function SWEP:Holster()
	return false
end

function SWEP:Reload() end

if CLIENT then
	local wm = ClientsideModel(SWEP.WorldModel)
	wm:SetNoDraw(true)

	function SWEP:DrawWorldModel()
		local owner = self:GetOwner()

		if IsValid(owner) then
			local offsetVec = Vector(5, -4, 16)
			local offsetAng = Angle(180, 90, 10)
			
			local boneid = owner:LookupBone("ValveBiped.Bip01_R_Hand")
			if !boneid then return end

			local matrix = owner:GetBoneMatrix(boneid)
			if !matrix then return end

			local newPos, newAng = LocalToWorld(offsetVec, offsetAng, matrix:GetTranslation(), matrix:GetAngles())

			wm:SetPos(newPos)
			wm:SetAngles(newAng)

            wm:SetupBones()
		else
			wm:SetPos(self:GetPos())
			wm:SetAngles(self:GetAngles())
		end

		wm:DrawModel()
	end
end