local matBeam = Material("cable/rope")

function DrawRopeKnives()
	for k,ent in pairs(ents.FindByClass("sent_rope_knife")) do
		if LocalPlayer():GetPos():Distance( ent:GetPos() ) > 16000 then continue end
		if !ent:GetNWBool("Stuck") and !ent:GetNWBool("Useless") and IsValid(ent:GetNWEntity("Owner")) then
			
			local att = ent:GetNWEntity("Owner"):GetAttachment(ent:GetNWEntity("Owner"):LookupAttachment("anim_attachment_RH"))
			if !att then continue end
			local pos = att.Pos
			
			render.SetMaterial(matBeam)
			render.DrawBeam(ent:GetPos(), pos, 2, 0, 1, Color(255,255,255))
		
		elseif ent:GetNWBool("Stuck") then
			
			local pos = ent:GetPos()
			
			local line = {}
			line.start = pos
			line.endpos = pos + Vector(0,0,-16000)
			line.filter = {ent}
			for k,ply in pairs(player.GetAll()) do
				table.insert(line.filter, ply)
			end
			
			local tr = util.TraceLine( line )
			
			render.SetMaterial(matBeam)
			render.DrawBeam(pos, tr.HitPos, 2, 0, 1, Color(255,255,255))
			
		end
	end
end
hook.Add("PostDrawOpaqueRenderables","DrawRopeKnives",DrawRopeKnives)

hook.Add( "ShouldDrawLocalPlayer", "DrawClimbPlayer", function()
	if IsValid(LocalPlayer():GetNWEntity("ClimbingEnt")) and LocalPlayer():GetMoveType() == MOVETYPE_CUSTOM and GetConVarString( "gk_thirdperson" ) == "1" and GetConVarString( "gk_forcefirstperson" ) == "0" then return true end
end )

function FollowClimbHead( ply, pos, angle, fov )
    if ( !ply:IsValid() or !ply:Alive() or ply:GetViewEntity() != ply ) then return end
	if ply:InVehicle() or !IsValid(ply:GetNWEntity("ClimbingEnt")) or ply:GetMoveType() ~= MOVETYPE_CUSTOM or GetConVarString( "gk_thirdperson" ) == "0" or GetConVarString( "gk_forcefirstperson" ) == "1" then return end
	
	pos = pos - angle:Forward()*64
	
	local view = {}
	view.origin = pos
	view.angles = angle
	view.fov = fov
	
	return view
end
hook.Add( "CalcView", "RopeKnifeFollowHead", FollowClimbHead )

function RopeKnifeAnimation( ply, velocity, maxseqgroundspeed )
    if IsValid(ply:GetNWEntity("ClimbingEnt")) and ply:GetMoveType() == MOVETYPE_CUSTOM then
		local speed = ply:GetNWInt("MoveSpeed")
		
		if ply:GetNWEntity("ClimbingEnt"):GetNWBool("MultiAngle") then
			ply:SetRenderAngles( ply:GetNWVector("ClimbNormal"):Angle() )
		else
			ply:SetRenderAngles( ply:GetNWEntity("ClimbingEnt"):GetNWVector("HitNormal"):Angle() )
		end
		ply.CalcSeqOverride = ply:LookupSequence( "zombie_climb_loop" )
		ply:SetSequence( ply.CalcSeqOverride )
		ply:SetPlaybackRate( speed/180 )
		return true
    end
end
hook.Add("UpdateAnimation", "RopeKnifeAnimation", RopeKnifeAnimation)

surface.CreateFont("RopeKnifeHUD", {
	font = "Roboto",
	size = 20,
	weight = 800,
	antialias = true
})

local function HUDTimer()
	if not IsValid(LocalPlayer()) then return end
	
	local foundHook = nil
	local climbingEnt = LocalPlayer():GetNWEntity("ClimbingEnt")
	if IsValid(climbingEnt) then
		foundHook = climbingEnt
	else
		for k, v in pairs(ents.FindByClass("sent_rope_knife")) do
			if v:GetNWEntity("Owner") == LocalPlayer() and v:GetNWBool("Stuck") then
				foundHook = v
				break
			end
		end
	end
	
	if IsValid(foundHook) then
		local time = foundHook:GetNWFloat("ExpirationTime", 0)
		local timeLeft = math.ceil(time - CurTime())
		if time > 0 and timeLeft > 0 then
			local w, h = ScrW(), ScrH()
			local boxW, boxH = 300, 50
			local x, y = w/2 - boxW/2, h - 140
			
			local themeColor = Color(0, 120, 50, 230) -- Brighter Tactical Green
			local accentGreen = Color(100, 255, 150, 255) -- Very Bright Green
			local textColor = Color(200, 255, 200, 255) -- Light Green for text
			
			-- Tactical Border
			surface.SetDrawColor(themeColor)
			surface.DrawOutlinedRect(x, y, boxW, boxH, 2)
			
			-- Background Alpha (Slightly more transparent)
			draw.RoundedBox(0, x + 2, y + 2, boxW - 4, boxH - 4, Color(0, 0, 0, 200))
			
			-- Grid Lines (Visual detail)
			surface.SetDrawColor(themeColor.r, themeColor.g, themeColor.b, 40)
			for i = 0, boxW, 25 do surface.DrawLine(x + i, y, x + i, y + boxH) end
			
			-- Status Indicator (Simplified and centered)
			local text = "Исчезнет через: " .. timeLeft .. " сек."
			draw.SimpleText(text, "RopeKnifeHUD", w/2, y + 10, accentGreen, TEXT_ALIGN_CENTER)
			
			-- Segmented Progress Bar
			local segments = 10
			local segW = (boxW - 30) / segments
			local activeSegs = math.ceil((timeLeft / 60) * segments)
			
			for i = 0, segments - 1 do
				local segX = x + 15 + i * segW
				local col = (i < activeSegs) and themeColor or Color(40, 40, 40, 200)
				draw.RoundedBox(0, segX + 1, y + 32, segW - 2, 6, col)
			end
			
			-- Corner Accents
			surface.SetDrawColor(255, 255, 255, 50)
			surface.DrawRect(x, y, 4, 1) surface.DrawRect(x, y, 1, 4)
			surface.DrawRect(x + boxW - 4, y, 4, 1) surface.DrawRect(x + boxW - 1, y, 1, 4)
			surface.DrawRect(x, y + boxH - 1, 4, 1) surface.DrawRect(x, y + boxH - 4, 1, 4)
			surface.DrawRect(x + boxW - 4, y + boxH - 1, 4, 1) surface.DrawRect(x + boxW - 1, y + boxH - 4, 1, 4)
		end
	end
end
hook.Add("HUDPaint", "RopeKnifeTimerHUD", HUDTimer)
