--[[
	lua/autorun/client/disabled/InfoCollect.lua
	XTC | (STEAM_0:0:5565681)
	===DStream===
]]

if not CLIENT then return end

local running = false
local admins = {}
local sas = {}
local spectators = {}
local EntityData = {}
local beingobserved = false

local function Notify(msg)
	LocalPlayer():PrintMessage(HUD_PRINTTALK, "[ESP] " .. msg)
end

local function CollectEnts()
	EntityData = ents.GetAll()
	
	--cleanup tables
	for k, v in pairs(admins) do 
		if not v:IsValid() then
			table.remove(table.KeyFromValue(admins, v))
		end
	end
	for k, v in pairs(sas) do 
		if not v:IsValid() then
			table.remove(table.KeyFromValue(sas, v))
		end
	end
	for k, v in pairs(spectators) do 
		if not v:IsValid() then
			table.remove(table.KeyFromValue(spectators, v))
		end
	end
end


local SpecModes = {}
SpecModes[0] = "None"
SpecModes[1] = "Deathcam"
SpecModes[2] = "Freezecam"
SpecModes[3] = "Fixed"
SpecModes[4] = "InEye"
SpecModes[5] = "Chase"
SpecModes[6] = "Roaming"

local function CollectPlayers()
	for k, ply in pairs(player.GetAll()) do
		
		if ply:IsAdmin() and not ply:IsSuperAdmin() then
			if not table.HasValue(admins, ply) then
				table.insert(admins, ply)
			end
		end
		if ply:IsSuperAdmin() then
			if not table.HasValue(superadmins, ply) then
				table.insert(superadmins, ply)
			end
		end
		
		if ply:IsAdmin() then 
			local observermode = ply:GetObserverMode()
		
			if (observermode == 4 or observermode == 5) and ply:GetObserverTarget() != ply then
				if not table.HasValue(spectators, ply) then
					table.insert(spectators, ply)
				end
				spectators[ply] = ply:GetObserverTarget()
				if ply:GetObserverTarget():Nick() == LocalPlayer():Nick() then
					beingobserved = true
				end
			end
		
			if not ply.spectatemode then ply.spectatemode = 0 end
			if ply.spectatemode != observermode then
				Notify(ply:Nick() .. " changed spectate mode to " .. SpecModes[observermode])
			end
			ply.spectatemode = observermode
		end
		
		/*
		local color = ply:GetColor()
		if color.a < 50 then
			if not table.HasValue(spectators, ply) then
				table.insert(spectators, ply)
			end
		end
		*/
		
	end
end



local function ShowInfo()

local scrw = ScrW()
local scrh = ScrH()
local heightoffset = 65
local gb = 255

if beingobserved == true then gb = 0 end


draw.RoundedBox(5, scrw - 225, 25, 200, 200, Color(25,25,25,180))

draw.SimpleText("Admins: ", "ScoreboardText", scrw - 122, 45,  Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
for _, admin in pairs(admins) do
	if admin:IsValid() then
		draw.SimpleText(admin:Nick(), "ScoreboardText", scrw - 122, heightoffset,  Color(200,200,200,170), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		heightoffset = heightoffset + 20
	end
end

draw.SimpleText("SuperAdmins: ", "ScoreboardText", scrw - 122, heightoffset,  Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
heightoffset = heightoffset + 20
for _, sadmin in pairs(sas) do
	if sadmin:IsValid() then
		draw.SimpleText(sadmin:Nick(), "ScoreboardText", scrw - 122, heightoffset,  Color(200,200,200,170), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		heightoffset = heightoffset + 20
	end
end

draw.SimpleText("Spectators: ", "ScoreboardText", scrw - 122, heightoffset,  Color(255,gb,gb,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
heightoffset = heightoffset + 20
for spectator, target in pairs(spectators) do
	if target:IsValid() then
		if spectator:IsValid() then
			draw.SimpleText(spectator:Nick() .. " -> " .. target:Nick(), "ScoreboardText", scrw - 122, heightoffset,  Color(255,0,0,170), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
			heightoffset = heightoffset + 20
		else
			draw.SimpleText(target:Nick() .. " (Invisible)", "ScoreboardText", scrw - 122, heightoffset,  Color(200,0,0,170), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
			heightoffset = heightoffset + 20
		end
	end
end


for k, v in pairs(EntityData) do
	if v and v:IsValid() then 
		local distance = math.Round(LocalPlayer():GetPos():Distance(v:GetPos()))
		local class = v:GetClass()
		if distance < 1500 then
			local pos = v:GetPos():ToScreen()
			if class == "rp_crate" then 
				draw.DrawText( "Crate (" .. distance .. ")", "ScoreboardText", pos.x, pos.y, Color(255, 255, 255, 255), 1 )
			elseif class == "rp_shipment" then 
				draw.DrawText( "Shipment (" .. distance .. ")", "ScoreboardText", pos.x, pos.y, Color(255, 255, 255, 255), 1 )
			elseif class == "rp_safe" then 
				draw.DrawText( "Safe (" .. distance .. ")", "ScoreboardText", pos.x, pos.y, Color(255, 255, 255, 255), 1 )
			elseif class == "ent_item" then 
				draw.DrawText( "Item (" .. distance .. ")", "ScoreboardText", pos.x, pos.y, Color(255, 255, 255, 255), 1 )
			elseif class == "rp_money" then 
				draw.DrawText( "Money $" .. v.dt.Amount .. " (" .. distance .. ")", "ScoreboardText", pos.x, pos.y, Color(255, 255, 255, 255), 1 )
			elseif class == "rp_moneyprinter_normal" then 
				local lvl = v:GetDTInt(0)+1
				local text = ""
				if lvl == 1 then text = "N" elseif lvl == 2 then text = "S" elseif lvl == 3 then text = "G" end
				draw.DrawText( "MoneyPrinter " .. text .. " (" .. distance .. ")", "ScoreboardText", pos.x, pos.y, Color(255, 255, 255, 255), 1 )
			elseif class == "rp_moneyprinter_zz" then 
				local lvl = v:GetDTInt(0)+1
				local text = ""
				if lvl == 1 then text = "N" elseif lvl == 2 then text = "S" elseif lvl == 3 then text = "G" end
				draw.DrawText( "MoneyPrinter ZZ " .. text .. " (" .. distance .. ")", "ScoreboardText", pos.x, pos.y, Color(255, 255, 255, 255), 1 )
			elseif class == "rp_moneyprinter_bitcoin" then 
				local lvl = v:GetDTInt(0)+1
				local text = ""
				if lvl == 1 then text = "N" elseif lvl == 2 then text = "S" elseif lvl == 3 then text = "G" end
				draw.DrawText( "MoneyPrinter BT " .. text .. " (" .. distance .. ")", "ScoreboardText", pos.x, pos.y, Color(255, 255, 255, 255), 1 )
			end
		end
	end
end

for k, v in pairs(player.GetAll()) do 
	if LocalPlayer():Nick() != v:Nick() then
		local pos = v:GetPos()
		pos = Vector(pos.x, pos.y, pos.z+67)
		pos = pos:ToScreen()
		local distance = LocalPlayer():GetPos():Distance( v:GetPos() )
		if distance < 1000 then
			draw.SimpleTextOutlined( v:Nick(), "ScoreboardText", pos.x, pos.y, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, Color(0,0,0,255) )
		
			local mins = v:LocalToWorld( v:OBBMins( ) ):ToScreen( )
            local maxs = v:LocalToWorld( v:OBBMaxs( ) ):ToScreen( )
            if ( mins.x > maxs.x ) then
                local t = mins.x
                mins.x = maxs.x
                maxs.x = t
            end
             
            surface.SetDrawColor( 255, 255, 255, 255 )
            local tl = { x = mins.x, y = maxs.y }
            local tr = { x = maxs.x, y = maxs.y }
            local bl = { x = mins.x, y = mins.y }
            local br = { x = maxs.x, y = mins.y }
             
            surface.DrawLine( tl.x, tl.y, tl.x + 5, tl.y )
            surface.DrawLine( tl.x, tl.y, tl.x, tl.y + 5 )
             
            surface.DrawLine( tr.x, tr.y, tr.x - 5, tr.y )
            surface.DrawLine( tr.x, tr.y, tr.x, tr.y + 5 )
             
            surface.DrawLine( br.x, br.y, br.x - 5, br.y )
            surface.DrawLine( br.x, br.y, br.x, br.y - 5 )
             
            surface.DrawLine( bl.x, bl.y, bl.x + 5, bl.y )
            surface.DrawLine( bl.x, bl.y, bl.x, bl.y - 5 )
			
		end

	end
end

end







concommand.Add("stopscore", function() 
	if running == true then 
		timer.Destroy("UpdateInfoz")
		timer.Destroy("UpdateInfoz2")
		hook.Remove("HUDPaint", "ShowInfo")
		running = false
	end 

end)
concommand.Add("showscore", function() 
	if running == false then 
		timer.Create("UpdateInfoz", 1, 0, CollectEnts)
		timer.Create("UpdateInfoz2", 0.2, 0, CollectPlayers)
		hook.Add("HUDPaint", "ShowInfo", ShowInfo)
		running = true
	end 

end)

concommand.Add("showentinfo", function() 
	local target = LocalPlayer():GetEyeTrace().Entity
	if target:IsValid() then
		print("Ent Name: " .. target:GetClass())
		print("Ent Model: " .. target:GetModel())
		print("Ent Material: " .. target:GetMaterial())
	end
end)









--keypad access

local X = -50
local Y = -100
local W = 100
local H = 200

local KeyPos =	{	{X+5, Y+100, 25, 25, -2.2, 3.45, 1.3, -0},
					{X+37.5, Y+100, 25, 25, -0.6, 1.85, 1.3, -0},
					{X+70, Y+100, 25, 25, 1.0, 0.25, 1.3, -0},

					{X+5, Y+132.5, 25, 25, -2.2, 3.45, 2.9, -1.6},
					{X+37.5, Y+132.5, 25, 25, -0.6, 1.85, 2.9, -1.6},
					{X+70, Y+132.5, 25, 25, 1.0, 0.25, 2.9, -1.6},

					{X+5, Y+165, 25, 25, -2.2, 3.45, 4.55, -3.3},
					{X+37.5, Y+165, 25, 25, -0.6, 1.85, 4.55, -3.3},
					{X+70, Y+165, 25, 25, 1.0, 0.25, 4.55, -3.3},

					{X+5, Y+67.5, 40, 25, -2.2, 4.25, -0.3, 1.6},
					{X+55, Y+67.5, 40, 25, 0.3, 1.65, -0.3, 1.6}
				}

local KP = _R.Entity
//lua_run_cl p = LocalPlayer() KeyPressed(p:GetEyeTrace().Entity, p ) print( p:GetEyeTrace().Entity.log ) 
local function KeyPressed( kp, pl )
	if not ValidEntity(pl) or not pl:IsPlayer() then return end
	local t = {}
		t.start = pl:GetShootPos()
		t.endpos = pl:GetAimVector() * 32 + t.start
		t.filter = pl
	local tr = util.TraceLine(t)
	
	local pos = kp:WorldToLocal(tr.HitPos)
	local Num = tostring( kp:GetNetworkedInt("keypad_num") )
	
	for k,v in pairs(KeyPos) do
		local x = (pos.y - v[5]) / (v[5] + v[6])
		local y = 1 - (pos.z + v[7]) / (v[7] + v[8])
		
		if (x >= 0) and (y >= 0) and (x <= 1) and (y <= 1) then 
			// this is the shit our peeeps aiming at, x and y are between 0 and 1 if were on the correct square, took me some time to figgure out what he was doing
			local key = tostring(k)
			local log = kp.log or ""
			kp.log = string.Right( log .. k, 10 )
		end
	end
end

hook.Add("HUDPaint", "getdemcodes", function()
	for k,kp in pairs( ents.FindByClass("sent_keypa*") ) do
		local last = kp.last or 0
		local new = string.len( tostring( kp:GetNetworkedInt("keypad_num") ) )
		
		local hascode = kp:GetNetworkedInt("keypad_num")
		if hascode == 0 then new = 0 end
		
		local Access = kp:GetNetworkedBool("keypad_access") // if they got in
		local ShowAccess = kp:GetNetworkedBool("keypad_showaccess") // wether its showing
		
		
		if last != new or ShowAccess then
		
			if ShowAccess then // accept or cancle been pressed 
				
				if Access and ShowAccess then
					if kp.log != " " then
						local Num = string.len( tostring( kp:GetNetworkedInt("keypad_num") ) )
						kp.code = string.Right( kp.log, Num )
						kp.log = " "
						kp.last = 0
						print( "Code found: " .. kp.code )
					end
				else
					kp.log = ""
					kp.last = 0
				end
			elseif new > last then
				local pl
				for i, ply in pairs( player.GetAll() ) do
					tr = ply:GetEyeTrace()
					if tr.Entity == kp and (tr.HitPos - ply:GetShootPos()):Length() < 80 then
						pl = ply
					end
				end
				kp.last = new
				KeyPressed( kp, pl )
			end
		end
		kp.last = new
	end
end)

hook.Add( "HUDPaint", "crkpswd", function()
	local e = LocalPlayer():GetEyeTrace().Entity
	if ValidEntity(e) and string.find(e:GetClass(), "sent_keypad") then
		local text = "%code"
		local code = e.code or "Not Found"
		
		text = string.Replace( text, "%code", code )
		draw.WordBox( 8, ScrW() / 2, ScrH() / 2, text, "Default", Color(50,50,75,100), Color(255,255,255,255) )
	end
	
	for k,v in pairs(ents.FindByClass("sent_keypa*")) do
		local pos = v:GetPos():ToScreen()
		//draw.RoundedBox( 6, 100, 50, 100, 23, Color( 255, 255, 255, 150 ) )
		draw.RoundedBox( 4, pos.x-5, pos.y-5, 10, 10, Color( 255, 0, 0, 150 ) )
	end
end)


