local radio_bgMat = Material("surrounded/radio_bg.png")
local radioMat = Material("surrounded/radio.png")
local bgMat = Material("surrounded/ammo_bg.png")
local bg = Material('surrounded/vgui_bg.png')
local bg2 = Material('surrounded/vgui_bg2.png')
local voiceMat = Material('surrounded/voice.png')
local def_bg_mat = Material("surrounded/tips/default.png", "noclamp smooth")
local lang = SuR.Language

local buttons = {
    lang["radio_1"],
    lang["radio_2"],
    lang["radio_3"],
    lang["radio_4"],
    lang["radio_5"],
    lang["radio_6"],
    lang["radio_7"],
}

function SuR:OpenRadioMenu()
    local ply = LocalPlayer()
    if ply:GetNWFloat('CanCallSupport') <= 0 or ply:IsLockedAbilities() then return end

    surface.PlaySound("surrounded/other/radio/radiomenu_0"..math.random(1,3)..".mp3")

    local df = vgui.Create("DFrame")
    df:SetSize(We(600), He(420))
    df:SetPos(We(50),He(200))
    df:SetTitle("")
    df:ShowCloseButton(false)
    df:MakePopup()

    local radionum = 0
    local force = false
    function df:Paint(w, h)
        surface.SetMaterial(radio_bgMat)
        surface.SetDrawColor(0,0,0,200)
        surface.DrawTexturedRect(0, 0, w, h)

        if force then
            radionum = math.min(radionum+FrameTime()/0.01, 20)
            if radionum == 20 then
                force = false
            end
        else
            radionum = math.max(radionum-FrameTime()/0.01, -20)
            if radionum == -20 then
                force = true
            end
        end

        surface.SetMaterial(radioMat)
        surface.SetDrawColor(255,255,255)
        surface.DrawTexturedRectRotated(We(60), He(45), We(72), He(72), radionum)

        draw.SimpleText(lang["radio_main"], "SuR_MediumFont1", We(120), He(32), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
    end

    local db = vgui.Create("DButton", df)
    db:SetSize(We(32), He(32))
    db:SetPos(We(375),He(30))
    db:SetText("")
    function db:Paint(w, h)
        if self:IsHovered() then
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        else
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    function db:DoClick()
        df:Remove()
    end

    for k, v in ipairs(buttons) do
        local db = vgui.Create("DButton", df)
        db:SetSize(We(320), He(40))
        db:SetPos(We(25),He(60)+(k*He(45)))
        db:SetText("")
        function db:OnCursorEntered()
            surface.PlaySound("surrounded/tick.wav")
        end
        function db:DoClick()
            if k == 6 and (#SuR.Config.Exfil_Points == 0 or SuR.Config.Objective_Mode) then
                chat.AddText(Color(255,255,255), string.format(SuR.Language["radio_6_disable"], SuR.Config.Exfil_Wave))
                chat.PlaySound()
                df:Remove()
                return
            elseif k == 6 and (SuR.Data["ExfilWave"] or not SuR.Data["WaveState"] or SuR.Data["Wave"] < SuR.Config.Exfil_Wave) then
                chat.AddText(Color(255,255,255), string.format(SuR.Language["radio_6_warn"], SuR.Config.Exfil_Wave))
                chat.PlaySound()
                df:Remove()
                return
            end
            surface.PlaySound("surrounded/other/radio/radiosend.mp3")
            net.Start("SuR.RadioSupport")
            net.WriteFloat(k)
            net.SendToServer()
            df:Remove()
        end
        function db:Paint(w, h)
            surface.SetMaterial(bgMat)
            surface.SetDrawColor(0,0,0,200)
            surface.DrawTexturedRect(0, 0, w, h)
    
            if db:IsHovered() then
                draw.SimpleText("- "..v, "SuR_SmallFont1", We(25), h/2, color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            else
                draw.SimpleText(v, "SuR_SmallFont1", We(25), h/2, Color(150,150,150), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            end
        end
    end
end

CreateConVar("sur_viewmod", 2, {FCVAR_ARCHIVE, FCVAR_USERINFO}, "", 0, 2)
CreateConVar("sur_viewmod_cutscene", 1, {FCVAR_ARCHIVE, FCVAR_USERINFO}, "", 0, 1)
CreateConVar("sur_viewmod_deathanims", 0, {FCVAR_ARCHIVE, FCVAR_USERINFO}, "", 0, 1)
CreateConVar("sur_music_volume", 1, FCVAR_ARCHIVE, "", 0.00, 2.00)
CreateConVar("sur_disable_hud", 0, FCVAR_ARCHIVE, "", 0, 1)
CreateConVar("sur_disable_hints", 0, FCVAR_ARCHIVE, "", 0, 1)
function SuR:OpenSettingsMenu()
    local df = vgui.Create("DFrame")
    df:SetSize(We(400), 210)
    df:Center()
    df:SetTitle("")
    df:MakePopup()
    df:ShowCloseButton(false)
    df.Paint = function(self, w, h)
        surface.SetMaterial(bg)
        surface.SetDrawColor(0,0,0,230)
        surface.DrawTexturedRect(0, 0, w, h)

        draw.SimpleText(lang["settings_main"], "SuR_SmallFont1", We(20), He(10), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
    end

    local db = vgui.Create("DButton", df)
    db:SetSize(We(32), He(32))
    db:SetPos(We(345),He(10))
    db:SetText("")
    function db:Paint(w, h)
        if self:IsHovered() then
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        else
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    function db:DoClick()
        surface.PlaySound("surrounded/click.mp3")
        df:Remove()
    end
    function db:OnCursorEntered()
        surface.PlaySound("surrounded/tick.wav")
    end

    local DermaCheckbox = df:Add("DCheckBoxLabel")
	DermaCheckbox:SetText(lang["settings_zc"])
	DermaCheckbox:SetConVar("sur_disable_hud")
    DermaCheckbox:Dock(TOP)
    DermaCheckbox:DockMargin(We(20), 5, 0, 10)
	DermaCheckbox:SizeToContents()

    local DermaCheckbox = df:Add("DCheckBoxLabel")
	DermaCheckbox:SetText(lang["settings_dh"])
	DermaCheckbox:SetConVar("sur_disable_hints")
    DermaCheckbox:Dock(TOP)
    DermaCheckbox:DockMargin(We(20), 0, 0, 10)
	DermaCheckbox:SizeToContents()

    local DermaCheckbox = df:Add("DCheckBoxLabel")
	DermaCheckbox:SetText(lang["settings_vmc"])
	DermaCheckbox:SetConVar("sur_viewmod_cutscene")
    DermaCheckbox:Dock(TOP)
    DermaCheckbox:DockMargin(We(20), 0, 0, 10)
	DermaCheckbox:SizeToContents()

    local DermaCheckbox = df:Add("DCheckBoxLabel")
	DermaCheckbox:SetText(lang["settings_vmd"])
	DermaCheckbox:SetConVar("sur_viewmod_deathanims")
    DermaCheckbox:Dock(TOP)
    DermaCheckbox:DockMargin(We(20), 0, 0, 10)
	DermaCheckbox:SizeToContents()

    local DermaNumSlider = df:Add("DNumSlider")			
    DermaNumSlider:SetText(lang["settings_vm"])	
    DermaNumSlider:SetMin(0)
    DermaNumSlider:SetMax(2)		
    DermaNumSlider:SetDecimals(0)		
    DermaNumSlider:SetConVar("sur_viewmod")
    DermaNumSlider:DockMargin(We(20), 0, 0, 0)
    DermaNumSlider:Dock(TOP)
    DermaNumSlider:SizeToContents()

    local DermaNumSlider = df:Add("DNumSlider")			
    DermaNumSlider:SetText(lang["settings_mv"])	
    DermaNumSlider:SetMin(0)
    DermaNumSlider:SetMax(2)		
    DermaNumSlider:SetDecimals(2)		
    DermaNumSlider:SetConVar("sur_music_volume")
    DermaNumSlider:DockMargin(We(20), 0, 0, 0)
    DermaNumSlider:Dock(TOP)
    DermaNumSlider:SizeToContents()	
end

function SuR:OpenAdminMenu()
    local ply = LocalPlayer()
    if !ply:IsAdmin() then return end

    local df = vgui.Create("DFrame")
    df:SetSize(We(400), He(180))
    df:Center()
    df:SetTitle("")
    df:MakePopup()
    df:ShowCloseButton(false)
    df.Paint = function(self, w, h)
        surface.SetMaterial(bg)
        surface.SetDrawColor(0,0,0,230)
        surface.DrawTexturedRect(0, 0, w, h)

        draw.SimpleText(lang["adminsettings_main"], "SuR_SmallFont1", We(10), He(10), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
    end

    local db = vgui.Create("DButton", df)
    db:SetSize(We(32), He(32))
    db:SetPos(We(345),He(10))
    db:SetText("")
    function db:Paint(w, h)
        if self:IsHovered() then
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        else
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    function db:DoClick()
        surface.PlaySound("surrounded/click.mp3")
        df:Remove()
    end
    function db:OnCursorEntered()
        surface.PlaySound("surrounded/tick.wav")
    end

    local DermaButton = df:Add("DButton")
	DermaButton:SetText(lang["adminsettings_startgame"])
    DermaButton:Dock(TOP)
    DermaButton:SetSize(0, He(30))
    DermaButton:DockMargin(0, He(20), 0, He(10))
    DermaButton.DoClick = function()
        RunConsoleCommand("sur_startgame")
        df:Remove()
    end

    local DermaButton = df:Add("DButton")
	DermaButton:SetText(lang["adminsettings_skipwave"])
    DermaButton:Dock(TOP)
    DermaButton:SetSize(0, He(30))
    DermaButton:DockMargin(0, 0, 0, He(10))
    DermaButton.DoClick = function()
        RunConsoleCommand("sur_skipwave")
        df:Remove()
    end

    local DermaButton = df:Add("DButton")
	DermaButton:SetText(lang["adminsettings_spawnmenu"])
    DermaButton:Dock(TOP)
    DermaButton:SetSize(0, He(30))
    DermaButton:DockMargin(0, 0, 0, He(10))
    DermaButton.DoClick = function()
        RunConsoleCommand("sur_spawnmenu")
        df:Remove()
    end
end

function SuR:OpenPMMenu()
    local ply = LocalPlayer()
    local modcur = ply:GetModel()
    local selectedweapon = ply:GetNWString('StartWeapon')
    local selectedvoice = ply:GetNWString('VoicePack', 'English 1')

    local DermaPanel = vgui.Create("DFrame")
    DermaPanel:SetSize(We(1100), He(730))
    DermaPanel:Center()
    DermaPanel:SetTitle("")
    DermaPanel:SetDraggable(false)
    DermaPanel:MakePopup()
    DermaPanel:ShowCloseButton(false)

    local sp = vgui.Create("DScrollPanel", DermaPanel)
    sp:SetSize(We(320), He(250))
    sp:SetPos(We(50),He(125))

    local List = vgui.Create("DIconLayout", sp)
    List:Dock(FILL)
    List:SetSpaceY(He(5))
    List:SetSpaceX(We(5)) 

    local icon = vgui.Create("DModelPanel", DermaPanel)
    icon:SetSize(We(160),He(250))
    icon:SetPos(We(390),He(125))
    icon:SetModel(modcur)
    local eyepos = icon.Entity:GetBonePosition(icon.Entity:LookupBone("ValveBiped.Bip01_Head1"))
    icon:SetCamPos(eyepos-Vector(40,0,20))
    icon:SetLookAt(eyepos-Vector(0,0,25))

    for k, v in pairs(player_manager.AllValidModels()) do
        if table.HasValue(SuR.Config.Blocked_PlayerModels, v) then continue end
        if #SuR.Config.Allowed_PlayerModels > 0 and not table.HasValue(SuR.Config.Allowed_PlayerModels, v) then continue end

        local SpawnI = List:Add("SpawnIcon")
        SpawnI:SetSize(We(72), He(72))
        SpawnI:SetModel(v)
        SpawnI:SetTooltip(k)
        SpawnI.DoClick = function(self)
            modcur = v
            surface.PlaySound("weapons/357/357_spin1.wav")
        end
    end

    DermaPanel.Paint = function(self, w, h)
        surface.SetMaterial(bg)
        surface.SetDrawColor(0,0,0,240)
        surface.DrawTexturedRect(0,0,w,h)

        surface.SetDrawColor(200,200,200,200)
        surface.DrawOutlinedRect(We(390),He(123),We(160),He(254),2)

        surface.SetDrawColor(200,200,200,200)
        surface.DrawOutlinedRect(We(48),He(123),We(324),He(254),2)

        draw.SimpleText(lang["main_menu_2"], "SuR_MediumFont1", We(50), He(50), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
        draw.SimpleText(lang["pmsettings_main"], "SuR_SmallFont2", We(50), He(100), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
        draw.SimpleText(lang["pmsettings_wep"], "SuR_SmallFont2", We(50), He(420), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)

        draw.SimpleText(lang["pmsettings_voice"], "SuR_SmallFont2", We(610), He(100), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)

        draw.SimpleText(lang["pmsettings_info"], "SuR_SmallFont1", We(810), He(520), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)

        icon:SetModel(modcur)
    end
    DermaPanel.OnRemove = function(self)
        if SuR.Data["WaveState"] and ply:Alive() then
            chat.AddText(Color(255,255,255), lang["pmsettings_mes1"])
            return
        else
            if modcur != ply:GetModel() or selectedweapon != ply:GetNWString('StartWeapon') or selectedvoice != ply:GetNWString('VoicePack') then
                chat.AddText(Color(255,255,255), lang["pmsettings_mes2"])
            end
        end
        net.Start("SuR.SelectPlayerModel")
        net.WriteString(modcur)
        net.SendToServer()
        net.Start("SuR.ChooseVoicePack")
        net.WriteString(selectedvoice)
        net.SendToServer()
    end

    local but = vgui.Create("DButton", DermaPanel)
    but:SetSize(We(30), He(30))
    but:SetPos(We(1000), He(40))
    but:SetText("")
    but.Paint = function(self, w, h)
        if self:IsHovered() then
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        else
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    but.DoClick = function()
        surface.PlaySound("surrounded/click.mp3")
        DermaPanel:Remove()
    end
    but.OnCursorEntered = function()
        surface.PlaySound("surrounded/tick.wav")
    end

    ---

	local tab = {}
	for _, wep in ipairs(SuR.Config.TierWeapons["Tier1"]) do
		local info = weapons.Get(wep)
		tab[info.PrintName] = {icon = Material("entities/"..wep..".png"), class = wep}
	end
    if ply:GetNWBool("ExtendedStartWeapons") then
        for _, wep in ipairs(SuR.Config.TierWeapons["Tier2"]) do
            local info = weapons.Get(wep)
            tab[info.PrintName] = {icon = Material("entities/"..wep..".png"), class = wep}
        end
    end

	local df1 = vgui.Create("DPanel", DermaPanel)
	df1:SetPos(We(49), He(400))
	df1:SetSize(We(500), He(350))
    df1.Paint = function(self, w, h)

	end
	df1.PaintOver = function(self, w, h)
		surface.SetDrawColor(200,200,200,100)
		surface.DrawOutlinedRect(0, He(40), We(500), He(240), 2)
	end
    df1.OnRemove = function()
        net.Start("SuR.ChooseStartWeapon")
        net.WriteString(selectedweapon)
        net.SendToServer()
    end

	local df2 = vgui.Create("DPanel", df1)
	df2:SetPos(0, He(40))
	df2:SetSize(We(500), He(240))
	df2.Paint = function(self, w, h) end

	local DScrollPanel = vgui.Create( "DScrollPanel", df2 )
	DScrollPanel:Dock( FILL )

	local down = -1
	for k, v in pairs(tab) do
		down = down+1
		local DButton = DScrollPanel:Add( "DButton" )
		DButton:SetText("")
		DButton:SetSize(We(500), He(60))
		DButton:SetPos(We(5), He(65)*down)
		DButton.Paint = function(self, w, h)
			surface.SetMaterial(radio_bgMat)
			surface.SetDrawColor(0,0,0,200)
			surface.DrawTexturedRect(0, 0, w, h)

			surface.SetDrawColor(255,255,255)
			surface.SetMaterial(v.icon)
			surface.DrawTexturedRect(12, 0, 64, 64)

			if selectedweapon == v.class then
				draw.SimpleText(k, "SuR_SmallFont2", We(96), h/2, Color(200,200,100), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			elseif self:IsHovered() then
				draw.SimpleText(k, "SuR_SmallFont2", We(96), h/2, Color(200,255,200), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			else
				draw.SimpleText(k, "SuR_SmallFont2", We(96), h/2, color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			end
		end
		DButton.DoClick = function(self)
			selectedweapon = v.class
			surface.PlaySound("surrounded/click.mp3")
		end
		DButton.OnCursorEntered = function()
			surface.PlaySound("surrounded/tick.wav")
		end
	end

    -----

    local tab = {}
	for k, v in pairs(SuR.Config.VoicePacks) do
		tab[#tab+1] = {k, v["enemyspotted"]}
	end

	local df1 = vgui.Create("DPanel", DermaPanel)
	df1:SetPos(We(609), He(83))
	df1:SetSize(We(400), He(300))
    df1.Paint = function(self, w, h) end
	df1.PaintOver = function(self, w, h)
		surface.SetDrawColor(200,200,200,100)
		surface.DrawOutlinedRect(0, He(40), We(400), He(254), 2)
	end

	local df2 = vgui.Create("DPanel", df1)
	df2:SetPos(0, He(40))
	df2:SetSize(We(400), He(254))
	df2.Paint = function(self, w, h) end

	local DScrollPanel = vgui.Create( "DScrollPanel", df2 )
	DScrollPanel:Dock( FILL )

	local down = -1
	for k, v in ipairs(tab) do
		down = down+1
		local DButton = DScrollPanel:Add( "DButton" )
		DButton:SetText("")
		DButton:SetSize(We(500), He(60))
		DButton:SetPos(We(5), He(65)*down)
		DButton.Paint = function(self, w, h)
			surface.SetMaterial(radio_bgMat)
			surface.SetDrawColor(0,0,0,200)
			surface.DrawTexturedRect(0, 0, w, h)

			surface.SetDrawColor(255,255,255)
			surface.SetMaterial(voiceMat)
			surface.DrawTexturedRect(16, 8, 48, 48)

			if selectedvoice == v[1] then
				draw.SimpleText(v[1], "SuR_SmallFont2", We(96), He(32), Color(200,200,100), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			elseif self:IsHovered() then
                draw.SimpleText(v[1], "SuR_SmallFont2", We(96), He(32), Color(200,255,200), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			else
                draw.SimpleText(v[1], "SuR_SmallFont2", We(96), He(32), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			end
		end
		DButton.DoClick = function(self)
			selectedvoice = v[1]
            if not self.Delay or self.Delay < CurTime() then
			    surface.PlaySound(SuR:GetRandomSound(v[2]))
                self.Delay = CurTime()+3
            end
		end
		DButton.OnCursorEntered = function()
			surface.PlaySound("surrounded/tick.wav")
		end
	end
end

function SuR:OpenPlayerClassMenu(tables)
    local ply = LocalPlayer()
    local selectedclass = ply:GetPlayerClass()

    local DermaPanel = vgui.Create("DFrame")
    DermaPanel:SetSize(We(600), He(730))
    DermaPanel:Center()
    DermaPanel:SetTitle("")
    DermaPanel:SetDraggable(false)
    DermaPanel:MakePopup()
    DermaPanel:ShowCloseButton(false)

    local sp = vgui.Create("DScrollPanel", DermaPanel)
    sp:SetSize(We(320), He(250))
    sp:SetPos(We(50),He(125))

    DermaPanel.Paint = function(self, w, h)
        surface.SetMaterial(bg)
        surface.SetDrawColor(0,0,0,240)
        surface.DrawTexturedRect(0,0,w,h)

        draw.SimpleText(lang["main_menu_3"], "SuR_MediumFont1", We(50), He(50), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)

        draw.SimpleText(lang["classmenu_current"]..lang["class_"..ply:GetPlayerClass()], "SuR_PixelFont", We(50), He(75), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
        draw.SimpleText(string.format(lang["classmenu_choose"], lang["class_"..selectedclass], lang["class_lvl_"..tables[selectedclass][1]]), "SuR_SmallFont1", w/2, He(310), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        draw.SimpleText(lang["classmenu_info"], "SuR_SmallFont2", w/2, He(390), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
    end
    DermaPanel.OnRemove = function(self)
        if selectedclass == ply:GetPlayerClass() then return end
        if ply:Alive() then
            chat.AddText(Color(255,255,255), lang["pmsettings_mes3"])
            return
        else
            chat.AddText(Color(255,255,255), lang["pmsettings_mes2"])
        end
        RunConsoleCommand("sur_selectclass", selectedclass)
    end

    local but = vgui.Create("DButton", DermaPanel)
    but:SetSize(We(30), He(30))
    but:SetPos(We(520), He(30))
    but:SetText("")
    but.Paint = function(self, w, h)
        if self:IsHovered() then
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        else
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    but.DoClick = function()
        surface.PlaySound("surrounded/click.mp3")
        DermaPanel:Remove()
    end
    but.OnCursorEntered = function()
        surface.PlaySound("surrounded/tick.wav")
    end

	local tab = {}
	for k, v in pairs(SuR.Config.ClassIcons) do
		local info = weapons.Get(wep)
		tab[k] = v
	end

	local df1 = vgui.Create("DPanel", DermaPanel)
	df1:SetPos(We(49), He(50))
	df1:SetSize(We(500), He(250))
    df1.Paint = function(self, w, h) end
	df1.PaintOver = function(self, w, h)
		surface.SetDrawColor(200,200,200,100)
		surface.DrawOutlinedRect(0, He(40), We(500), He(190), 2)
	end

	local df2 = vgui.Create("DPanel", df1)
	df2:SetPos(0, He(40))
	df2:SetSize(We(500), He(190))
	df2.Paint = function(self, w, h) end

	local DScrollPanel = vgui.Create( "DScrollPanel", df2 )
	DScrollPanel:Dock( FILL )

	local down = -1
	for k, v in pairs(tab) do
		down = down+1
		local DButton = DScrollPanel:Add( "DButton" )
		DButton:SetText("")
		DButton:SetSize(We(500), He(60))
		DButton:SetPos(We(5), He(65)*down)
		DButton.Paint = function(self, w, h)
			surface.SetMaterial(radio_bgMat)
			surface.SetDrawColor(0,0,0,200)
			surface.DrawTexturedRect(0, 0, w, h)

			surface.SetDrawColor(255,255,255)
			surface.SetMaterial(v)
			surface.DrawTexturedRect(16, 8, 48, 48)

			if selectedclass == k then
				draw.SimpleText(lang["class_"..k], "SuR_SmallFont2", We(96), He(25), Color(200,200,100), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                draw.SimpleText(lang["class_lvl_"..tables[k][1]], "SuR_SmallFont1", We(96), He(45), Color(200,200,100), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			elseif self:IsHovered() then
                draw.SimpleText(lang["class_"..k], "SuR_SmallFont2", We(96), He(25), Color(200,255,200), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                draw.SimpleText(lang["class_lvl_"..tables[k][1]], "SuR_SmallFont1", We(96), He(45), Color(200,255,200), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			else
                draw.SimpleText(lang["class_"..k], "SuR_SmallFont2", We(96), He(25), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
                draw.SimpleText(lang["class_lvl_"..tables[k][1]], "SuR_SmallFont1", We(96), He(45), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
			end
		end
		DButton.DoClick = function(self)
			selectedclass = k
			surface.PlaySound("surrounded/click.mp3")
		end
		DButton.OnCursorEntered = function()
			surface.PlaySound("surrounded/tick.wav")
		end
	end

    ---

    local df1 = vgui.Create("DPanel", DermaPanel)
	df1:SetPos(We(49), He(380))
	df1:SetSize(We(500), He(300))
    df1.Paint = function(self, w, h) end
	df1.PaintOver = function(self, w, h)
		surface.SetDrawColor(200,200,200,100)
		surface.DrawOutlinedRect(0, He(40), We(500), He(250), 2)
	end

	local df2 = vgui.Create("DPanel", df1)
	df2:SetPos(0, He(40))
	df2:SetSize(We(500), He(250))
	df2.Paint = function(self, w, h) end

	local DScrollPanel = vgui.Create( "DScrollPanel", df2 )
	DScrollPanel:Dock( FILL )

	local down = -1
	for i=1,5 do
		down = down+1
		local dp = DScrollPanel:Add( "DPanel" )
		dp:SetText("")
		dp:SetSize(We(475), He(100))
		dp:SetPos(We(5), He(105)*down)
        dp:SetContentAlignment(5)
        dp.id = i
		dp.Paint = function(self, w, h)
			surface.SetDrawColor(0,0,0,200)
			surface.DrawRect(0, 0, w, h)
            if self.label then
                self.label:SetText(lang["class_"..selectedclass.."_state_"..self.id])
            end
		end

        local dl = vgui.Create("DLabel", dp)
        dl:Dock(FILL)
        dl:DockMargin(5,5,5,5)
        dl:SetContentAlignment(5)
        dl:SetFont("SuR_SmallFont1")
        dl:SetWrap(true)
        local num = 1+(5*(i-1))
        dl:SetText("")
        dl.Paint = function(self, w, h)
            local lvl = tables[selectedclass][1]
            if lvl >= num then
                self:SetText("✓ "..lang["classmenu_state"]..lang["class_lvl_"..num].."\n\n"..lang["class_"..selectedclass.."_state_"..i])
                self:SetTextColor(Color(50,200,50))
            else
                self:SetText(lang["classmenu_state"]..lang["class_lvl_"..num])
                self:SetTextColor(Color(200,50,50))
            end
        end
	end
end
net.Receive("SuR.OpenClassMenu", function(len, ply)
    local tab = net.ReadTable()
    SuR:OpenPlayerClassMenu(tab)
end)

--------------------------MAIN MENU

local mainpanel = nil 

hook.Add("Think", "SuR_MainMenu", function(ply, but)
	if input.IsKeyDown(KEY_ESCAPE) then
		SuR:OpenMainMenu()
	end
end)

function SuR:OpenMainMenu()
	if IsValid(mainpanel) then return end

	mainpanel = vgui.Create("DFrame")
	mainpanel:SetSize(ScrW(), ScrH())
	mainpanel:SetTitle("")
	mainpanel:SetDraggable(false)
	mainpanel:MakePopup()
	mainpanel:ShowCloseButton(false)
    mainpanel:AlphaTo(0, 0)
    mainpanel:AlphaTo(255, 0.2)
	mainpanel.OnRemove = function()
		SuR.Data["DrawHUD"] = true
	end
	mainpanel.Paint = function(self, w, h) 
		surface.SetDrawColor(0,0,0,100)
		surface.DrawRect(0, 0, w, h)

        surface.SetDrawColor(0,0,0,200)
        surface.SetMaterial(radio_bgMat)
		surface.DrawTexturedRect(0, 0, We(550), h)

		SuR.Data["DrawHUD"] = false
	end

	local secpanel = vgui.Create("DPanel", mainpanel)
	secpanel:SetPos(0, 0)
	secpanel:SetSize(We(400), ScrH())
	secpanel.Paint = function(self, w, h)
		gui.HideGameUI()

		draw.SimpleText("Surrounded", "SuR_MediumFont2", We(200), He(20), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
	end

	local scr1 = vgui.Create("DScrollPanel", secpanel)
	scr1:Dock(FILL)

	local main_menu_buttons = {
		{
			Name = lang["main_menu_1"],
			CustomDock = {We(150),He(5)},
			ClickFunc = function(self) end,
		},
		{
			Name = lang["main_menu_2"],
			ClickFunc = function(self)
                SuR:OpenPMMenu()
			end,
		},
        {
			Name = lang["main_menu_3"],
			ClickFunc = function(self)
                net.Start("SuR.OpenClassMenu")
                net.SendToServer()
			end,
		},
		{
			Name = lang["main_menu_4"],
			ClickFunc = function(self)
                SuR:OpenSettingsMenu()
			end,
		},
		{
			Name = lang["main_menu_discord"],
			ClickFunc = function(self)
				gui.OpenURL("https://discord.gg/ACVMgAfKKt")
			end,
		},
		{
			Name = lang["main_menu_site"],
			ClickFunc = function(self)
				gui.OpenURL("https://refoseltw.ru")
			end,
		},
		{
			Name = lang["main_menu_5"],
			CustomDock = {We(50),He(5)},
			ClickFunc = function(self)
                timer.Simple(0.01, function()
				    gui.ActivateGameUI()
                end)
			end,
		},
	}

	for k, v in ipairs(main_menu_buttons) do
		local but = scr1:Add("DButton")
		but:SetText("")
		but:Dock( TOP )
		but:SetSize(0, He(40))
		but:DockMargin(We(10), 0, We(10), He(5))
		if v.CustomDock then
			but:DockMargin(We(10), He(v.CustomDock[1]), We(10), He(v.CustomDock[2]))
		end
		but.DoClick = function()
            surface.PlaySound("surrounded/click.mp3")
            mainpanel:AlphaTo(0, 0.2, 0, function(tab, pnl)
                mainpanel:Remove()
                v.ClickFunc()
            end)
        end
		but.Paint = function(self, w, h)
            surface.SetMaterial(bgMat)
            surface.SetDrawColor(0,0,0,200)
            surface.DrawTexturedRect(0, 0, w, h)

			local hov = self:IsHovered()
			if hov then
                draw.SimpleText(v.Name, "SuR_SmallFont2", w/2, h/2-He(2), Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
			else
                draw.SimpleText(v.Name, "SuR_SmallFont2", w/2, h/2-He(2), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
			end

		end
        but.OnCursorEntered = function()
            surface.PlaySound("surrounded/tick.wav")
        end
	end
end

function SuR:OpenDropMenu()
    local ply = LocalPlayer()
    local weapons = {}
    for _, w in ipairs(ply:GetWeapons()) do
        if w:GetClass() == SuR.Config.EmptyWeapon then continue end
        local name = w:GetPrintName() or w.PrintName
        if name then
            weapons[#weapons+1] = {w:GetClass(), name}
        end
    end
    if ply:GetNWFloat('Bandages') > 2 then
        weapons[#weapons+1] = {"bandage", lang["loot_bandages"].."   x"..ply:GetNWFloat('Bandages')}
    end
    if ply:GetNWFloat('Sprays') > 0 then
        weapons[#weapons+1] = {"antidote", lang["loot_antidote"].."   x"..ply:GetNWFloat('Sprays')}
    end
    if ply:GetNWFloat('SelfRevive') > 0 then
        weapons[#weapons+1] = {"selfrevive", lang["loot_selfrevive"].."   x"..ply:GetNWFloat('SelfRevive')}
    end
    if ply:GetNWFloat('CanCallSupport') > 0 then
        weapons[#weapons+1] = {"radio", lang["loot_radio"].."   x"..ply:GetNWFloat('CanCallSupport')}
    end

    local df = vgui.Create("DFrame")
    df:SetSize(We(380), He(90)+(#weapons*He(45)))
    df:Center()
    df:SetTitle("")
    df:MakePopup()
    df:ShowCloseButton(false)
    df.Paint = function(self, w, h)
        surface.SetMaterial(bg)
        surface.SetDrawColor(0,0,0,230)
        surface.DrawTexturedRect(0, 0, w, h)

        if #weapons > 0 then
            draw.SimpleText(lang["menu_drop"], "SuR_SmallFont1", We(25), He(25), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
        else
            draw.SimpleText(lang["menu_nodrop"], "SuR_SmallFont1", We(25), He(25), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
        end
    end

    local db = vgui.Create("DButton", df)
    db:SetSize(We(32), He(32))
    db:SetPos(We(305),He(25))
    db:SetText("")
    function db:Paint(w, h)
        if self:IsHovered() then
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        else
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    function db:DoClick()
        surface.PlaySound("surrounded/click.mp3")
        df:Remove()
    end
    function db:OnCursorEntered()
        surface.PlaySound("surrounded/tick.wav")
    end

    for k, v in ipairs(weapons) do
        local db = vgui.Create("DButton", df)
        db:SetSize(We(320), He(40))
        db:SetPos(We(25),He(20)+(k*He(45)))
        db:SetText("")
        function db:OnCursorEntered()
            surface.PlaySound("surrounded/tick.wav")
        end
        function db:DoClick()
            RunConsoleCommand("sur_dropweapon", v[1])
            df:Remove()
        end
        function db:Paint(w, h)
            surface.SetMaterial(bgMat)
            surface.SetDrawColor(0,0,0,200)
            surface.DrawTexturedRect(0, 0, w, h)
    
            if db:IsHovered() then
                draw.SimpleText("- "..v[2], "SuR_SmallFont1", We(25), h/2, color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            else
                draw.SimpleText(v[2], "SuR_SmallFont1", We(25), h/2, Color(150,150,150), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
            end
        end
    end
end

function SuR:OpenSpawnMenu()
    if !LocalPlayer():IsSuperAdmin() then return end
    
    local frame = vgui.Create("DFrame")
    frame:SetSize(400, 300)
    frame:SetTitle("Admin Spawnmenu")
    frame:SetVisible(true)
    frame:SetDraggable(true)
    frame:ShowCloseButton(true)
    frame:Center()
    frame:MakePopup()
    frame.Paint = function(self, w, h)
        surface.SetDrawColor(0,0,0,240)
        surface.DrawRect(0,0,w,h)
    end

    local categoryTabs = vgui.Create("DPropertySheet", frame)
    categoryTabs:Dock(FILL)

    local weaponPanel = vgui.Create("DPanel")
    categoryTabs:AddSheet("Weapons", weaponPanel, "icon16/gun.png")

    local weaponList = vgui.Create("DListView", weaponPanel)
    weaponList:Dock(FILL)
    weaponList:AddColumn("Weapons")

    local entityPanel = vgui.Create("DPanel")
    categoryTabs:AddSheet("Entities", entityPanel, "icon16/brick.png")

    local entityList = vgui.Create("DListView", entityPanel)
    entityList:Dock(FILL)
    entityList:AddColumn("Entities")

    local npcPanel = vgui.Create("DPanel")
    categoryTabs:AddSheet("NPCs", npcPanel, "icon16/user.png")

    local npcList = vgui.Create("DListView", npcPanel)
    npcList:Dock(FILL)
    npcList:AddColumn("NPCs")

    local function AddItemToCategory(pnl, name, cmd)
        local itemLine = pnl:AddLine(name)
        itemLine.OnSelect = function()
            surface.PlaySound("buttons/combine_button1.wav")
            LocalPlayer():ConCommand(cmd)
        end
    end

    for k, v in pairs(list.Get("NPC")) do
        if not v.Name or v.Name == "" then return end
        local cmd = "sur_spawn "..v.Class
        AddItemToCategory(npcList, v.Name, cmd)
    end

    for k, v in pairs(list.Get("Weapon")) do
        if not v.ClassName or v.ClassName == "" then return end
        local cmd = "sur_spawn "..v.ClassName
        AddItemToCategory(weaponList, v.PrintName, cmd)
    end

    for k, v in pairs(list.Get("SpawnableEntities")) do
        if not v.ClassName or v.ClassName == "" then return end
        local cmd = "sur_spawn "..v.ClassName
        AddItemToCategory(entityList, v.PrintName, cmd)
    end
end
concommand.Add("sur_spawnmenu", SuR.OpenSpawnMenu)

function SuR:OpenClassMenu()
    local ply = LocalPlayer()
    local tab = table.Copy(SuR.Config.Zombie_Unlock_Wave)

    local df = vgui.Create("DFrame")
    df:SetSize(ScrW(), He(300))
    df:SetPos(0, ScrH()/2-He(150))
    df:SetTitle("")
    df:MakePopup()
    df:ShowCloseButton(false)
    df:SetDraggable(false)
    df.Paint = function(self, w, h)
        surface.SetDrawColor(20,20,20,200)
        surface.DrawRect(0, 0, w, h)

        draw.SimpleText(lang["zombiemenu_choose"], "SuR_SmallFont1", w/2, He(25), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
    end

    local db = vgui.Create("DButton", df)
    db:SetSize(We(32), He(32))
    db:SetPos(ScrW()-We(200),He(25))
    db:SetText("")
    function db:Paint(w, h)
        if self:IsHovered() then
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        else
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    function db:DoClick()
        surface.PlaySound("surrounded/click.mp3")
        df:Remove()
    end
    function db:OnCursorEntered()
        surface.PlaySound("surrounded/tick.wav")
    end

    local num = -1
    for k, v in SortedPairsByValue(tab) do
        num = num+1
        local db = vgui.Create("DButton", df)
        db:SetSize(We(220), He(200))
        db:SetPos(We(50)+(num*We(230)),He(60))
        db:SetText("")
        function db:OnCursorEntered()
            surface.PlaySound("surrounded/tick.wav")
        end
        function db:DoClick()
            RunConsoleCommand("sur_zombieclass", k)
            df:Remove()
        end
        function db:Paint(w, h)
            local wave = SuR.Data["Wave"]
            if not SuR.Data["WaveState"] then
                wave = wave + 1
            end
            
            surface.SetMaterial(bgMat)
            surface.SetDrawColor(0,0,0,200)
            surface.DrawTexturedRect(0, 0, w, h)
            
            if v <= wave then
                draw.SimpleText(k, "SuR_SmallFont1", w/2, h-56, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM)
                draw.SimpleText(lang["zombiemenu_unlocked"], "SuR_SmallFont1", w/2, h-32, Color(150,150,150), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM)
            else
                draw.SimpleText(k, "SuR_SmallFont1", w/2, h-56, Color(255,0,0), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM)
                draw.SimpleText(string.format(lang["zombiemenu_unlock"], v), "SuR_SmallFont1", w/2, h-32, Color(150,150,150), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM)
            end
        end

        local mod = vgui.Create("SpawnIcon", db)
        mod:SetSize(We(72), He(72))
        mod:SetPos(We(76), He(36))
        mod:SetModel(table.Random(SuR.Config.Zombie_PlayerModels[k]))
        function mod:DoClick()
            RunConsoleCommand("sur_zombieclass", k)
            df:Remove()
        end
    end
end
concommand.Add("sur_zombiemenu", SuR.OpenClassMenu)

local showed_tips = {}
function SuR:ShowTip(type)
    if showed_tips[type] or GetConVar("sur_disable_hints"):GetBool() then return end
    showed_tips[type] = true
    SuR.Data["DrawHUD"] = false
    surface.PlaySound("surrounded/sfx/tips.mp3")
    local tip_mat = def_bg_mat
    local tip_title = lang["tip_"..type] or ""
    local tip_text = lang["tip_"..type.."_desc"] or ""
    if file.Exists("materials/surrounded/tips/"..type..".png", "GAME") then
        tip_mat = Material("surrounded/tips/"..type..".png", "noclamp smooth")
    end

    local df = vgui.Create("DFrame")
    df:SetSize(ScrW(), ScrH())
    df:SetTitle("")
    df:MakePopup()
    df:ShowCloseButton(false)
    df:AlphaTo(0, 0)
    df:AlphaTo(255, 0.2)
    df:SetDraggable(false)
    df.Paint = function(self, w, h)
        surface.SetDrawColor(0,0,0,150)
        surface.DrawRect(0, 0, w, h)
    end

    local sec = vgui.Create("DPanel", df)
    sec:SetSize(We(1800), He(1000))
    sec:Center()
    sec.Paint = function(self, w, h)
        surface.SetMaterial(bg2)
        surface.SetDrawColor(0,0,0,250)
        surface.DrawTexturedRect(0, 0, w, h)

        draw.SimpleText(lang["tip_main"], "SuR_SmallFont2", We(100), He(40), Color(200,200,0), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
        draw.SimpleText(tip_title, "SuR_MediumFont2", We(100), He(70), color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)

        --------------------

        surface.SetMaterial(tip_mat)
        surface.SetDrawColor(255,255,255)
        surface.DrawTexturedRect(We(320), He(150), We(1920*0.6), He(1080*0.6))

        surface.SetDrawColor(200,200,200,250)
        surface.DrawOutlinedRect(We(320), He(150), We(1920*0.6), He(1080*0.6))
    end

    local function dis()
        gui.EnableScreenClicker(false)
        df:AlphaTo(0, 0.2, 0, function()
            df:Remove()
            SuR.Data["DrawHUD"] = true
        end)
    end

    local dl = vgui.Create("DLabel", df)
    dl:SetPos(We(150), He(760))
    dl:SetSize(We(1600), He(200))
    dl:SetText(tip_text)
    dl:SetTextColor(color_white)
    dl:SetFont("SuR_SmallFont2")
    dl:SetWrap(true)
    dl:SetContentAlignment(1)

    local db = vgui.Create("DButton", sec)
    db:SetSize(We(32), He(32))
    db:SetPos(We(1650),He(70))
    db:SetText("")
    function db:Paint(w, h)
        if self:IsHovered() then
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, Color(200,255,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        else
            draw.SimpleText("X", "SuR_MediumFont1", w/2, h/2, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
    end
    function db:DoClick()
        surface.PlaySound("surrounded/click.mp3")
        dis()
    end
    function db:OnCursorEntered()
        surface.PlaySound("surrounded/tick.wav")
    end
end
net.Receive("SuR.ShowTips", function()
    local str = net.ReadString()
    SuR:ShowTip(str)
end)
