--[[
	MOD/lua/adminmenu.lua
	[Ӻ.ɧ] MeepDarknessMeep | (STEAM_0:0:68219133) | [05-08-13 10:09:14PM]
	===BadFile===
]]

S = {}

surface.CreateFont("Font222", {font = "BudgetLabel", size = 30, weight = 500, antialias = false, underline = false, shadow = true, outline = true})
surface.CreateFont("Font333", {font = "BudgetLabel", size = 22, weight = 500, antialias = false, underline = false, shadow = true, outline = true})

local function startup()
    local amod = false
    if util.tobool(ulx) then 
        print("** ULX is installed!")
        amod = "ulx"
    else
        print("** ULX not installed!")
    end
    
    return amod
end

S.mod = startup()
if not S.mod then print("** No admin mods detected! Stopping.") end
print("** Admin mod \""..S.mod.."\" detected! Proceeding.")

S.table = {}
S.y = 1
S.x = 1
S.enter = false
S.LastTime = CurTime()
S.fadeouttime = 5
local indentx = 4
local indenty = 2

function S.insert(name)
    if not S.table or S.table[name] then return false end
    if not type(name) == "string" then return false end
    local i = #S.table + 1
    S.table[i] = {}
    S.table[i].name = name
    S.table[i].options = {}
    return true
end

function S.exists(name)
    for i, v in pairs(S.table) do
        if v.name == name then return i end
    end
    return false
end

function S.insert2(name, name2, func)
    if not type(name) == "string" or not type(name2) == "string" then return false end
    if not S.table then return false end
    for i = 1, #S.table do 
        if S.table[i].name == name then
            local k = #S.table[i].options + 1
            S.table[i].options[k] = {}
            S.table[i].options[k].name = name2
            S.table[i].options[k].func = func
        end
    end
end

function S.exists2(name, name2)
    local i = S.exists(name)
    if not i then return false end
    for k = 1, #S.table[i].options do
        if S.table[i].options[k].name == name2 then 
            return true, k
        end
    end
    return false
end


if S.mod == "ulx" then
    local tbl = {}
    for k, cmddata in pairs(ULib.cmds.translatedCmds) do
        tbl[cmddata.category or "misc"] = tbl[cmddata.category or "misc"] or {}
        table.insert(tbl[cmddata.category or "misc"], cmddata.cmd)
    end
    for cat, tab in pairs(tbl) do
        for i, cmd in pairs(tab) do
            local b, lol = S.exists2(cmd)
            if not S.exists2(cat, cmd) and LocalPlayer():query(cmd) then
                if not S.exists(cat) then S.insert(cat); end
                S.insert2(cat, cmd, function() LocalPlayer():ConCommand(cmd.." ^"); print(cmd.." ^"); end)
            end
        end
    end
end

concommand.Add("left", function()
    if S.x > 1 then
    else
        S.y = S.y - 1
        if S.y < 1 then S.y = #S.table end
    end
    S.LastTime = CurTime()
end)

concommand.Add("right", function()
    if S.x > 1 then
    else
        S.y = S.y + 1
        if S.y > #S.table then S.y = 1 end
    end
    S.LastTime = CurTime()
end)

concommand.Add("down", function()
    S.x = S.x + 1
    S.LastTime = CurTime()
end)

concommand.Add("up", function()
    S.x = S.x - 1
    if S.x < 1 then S.x = 1 end
    S.LastTime = CurTime()
end)

concommand.Add("enter", function()
    if S.x > 1 then
        S.enter = true
    end
    S.LastTime = CurTime()
end)
    

hook.Add("HUDPaint", "Paintstuff", function()
    surface.SetFont("Font222")
    local fcurtime = S.LastTime + 5
    local acurtime = CurTime()
    local alpha = 255
    
    if acurtime > fcurtime then
        local dif = acurtime - fcurtime
        local numdif = dif / S.fadeouttime
        alpha = (1 - numdif) * 255
        if alpha < 40 then alpha = 40 end
    end
    
    local x = ScrW()
    local y = ScrH()
    local xsides = 0
    local yheight = y / 19
    
    surface.SetDrawColor(15,15,15,alpha)
    surface.DrawRect(0, 0, x, yheight)
    local tbl = S.table
    local tabx = (x - xsides * 2) / #tbl
    local num = 0
    for k,v in pairs(S.table) do 
        local color = Color(75,75,75,alpha)
        if k == S.y then 
            surface.SetDrawColor(0,0,51,alpha)
            color = Color(0,0,151,alpha)
        else
            surface.SetDrawColor(20,20,20,alpha)
        end
        local x2 = xsides + tabx * num + indentx
        surface.DrawRect(x2, indenty, tabx - indentx, yheight - indenty * 2)
        local w, h = surface.GetTextSize(v.name or "ERROR")
		local y2 = (yheight - indenty * 2) - h
        draw.DrawText(v.name or "ERROR", "Font222", xsides + tabx * num + indentx + tabx / 2, y2, color, TEXT_ALIGN_CENTER)
        surface.SetDrawColor(50,50,50,alpha)
        if k == S.y and S.x > 1 then
            if #v.options < S.x - 1 then
                S.x = #v.options + 1
            end
            if #v.options > 0 then
                local opty = yheight * #v.options
                surface.DrawRect(x2, indenty * 3 + yheight, tabx - indentx, opty)
                local num2 = 0
                
                local indentx = indenty
                
                for i = 1, #v.options do 
                    surface.SetDrawColor(20,20,20,alpha)
                    local color = Color(75,75,75,alpha)
                    if S.x - 1 == i then
                        surface.SetDrawColor(0,0,51,alpha)
                        if S.enter then
                            S.enter = false
                            v.options[i].func()
                        end
                    end
                    if S.x - 1 == i then 
                        color = Color(0,0,151,alpha)
                    end
                    surface.DrawRect(x2 + indentx, indenty * 4 + yheight * num2 + yheight, tabx - indentx * 4, yheight - indenty * 2)
                    draw.DrawText(v.options[i].name or "ERROR", "Font222", xsides + tabx * num + indentx * 2 + tabx / 2, yheight * num2 + y2 + indenty * 3 + yheight, color, TEXT_ALIGN_CENTER)
                   -- v.options[i].name
                   num2 = num2 + 1
                end
            end
        end
        num = num + 1
    end
    
end)