AddCSLuaFile()

SWEP.PrintName = "Landmines"

SWEP.Slot = 0
SWEP.SlotPos = 4
SWEP.Instructions = "LMB - Build, RMB - Rotate, R - Menu"

SWEP.Spawnable = true

SWEP.ViewModel = "models/surrounded/equipment/fieldupgrade_proximitymine.mdl"
SWEP.WorldModel = "models/surrounded/equipment/fieldupgrade_proximitymine.mdl"
SWEP.ViewModelFOV = 54
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")
	self:SetNWBool('Ready', true)
end

function SWEP:ReloadMine()
	self:SetNWBool('Ready', false)
	self:SetHoldType("normal")
	self.Owner:EmitSound("buttons/combine_button1.wav")

	timer.Create("ReloadMineTime"..math.random(0,99999999), 15, 1, function()
		if !IsValid(self) or !IsValid(self.Owner) then return end

		self:SetNWBool('Ready', true)
		self:SetHoldType("slam")
		self.Owner:EmitSound("buttons/combine_button_locked.wav")
	end)
end

function SWEP:PrimaryAttack()
	if SERVER then
		local ply = self.Owner
		if !self:GetNWBool('Ready') then return end

		local ent = ents.Create("sur_landmine")
		ent:SetPos(ply:EyePos()+ply:GetForward()*32)
		ent:Spawn()
		ent:GetPhysicsObject():SetVelocity(ply:GetAimVector()*256)
		self:ReloadMine()
	end
end

function SWEP:SecondaryAttack()
end

function SWEP:Reload()
end

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

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

		if (IsValid(_Owner)) then
			local offsetVec = Vector(5, -2.7, -3.4)
			local offsetAng = Angle(180, 90, 0)
			
			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())

			WorldModel:SetPos(newPos)
			WorldModel:SetAngles(newAng)

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

		if self:GetNWBool('Ready') then
			WorldModel:DrawModel()
		end
	end

	SWEP.IronSightsPos  = Vector(0, 25, -12)
	SWEP.IronSightsAng  = Vector(10, 0, 0)
	
	function SWEP:GetViewModelPosition(EyePos, EyeAng)
		local Mul = 1.0
	
		local Offset = self.IronSightsPos
		if !self:GetNWBool('Ready') then
			Offset = Vector(0,0,999)
		end
	
		if (self.IronSightsAng) then
			EyeAng = EyeAng * 1
			
			EyeAng:RotateAroundAxis(EyeAng:Right(), 	self.IronSightsAng.x * Mul)
			EyeAng:RotateAroundAxis(EyeAng:Up(), 		self.IronSightsAng.y * Mul)
			EyeAng:RotateAroundAxis(EyeAng:Forward(),   self.IronSightsAng.z * Mul)
		end
	
		local Right 	= EyeAng:Right()
		local Up 		= EyeAng:Up()
		local Forward 	= EyeAng:Forward()
	
		EyePos = EyePos + Offset.x * Right * Mul
		EyePos = EyePos + Offset.y * Forward * Mul
		EyePos = EyePos + Offset.z * Up * Mul
		
		return EyePos, EyeAng
	end
end
