
-- Fonts for pause menu
surface.CreateFont("PauseMenuTitle", {
    font = "Exo 2",
    size = 73,
    weight = 700,
    extended = true
})

surface.CreateFont("PauseMenuButton", {
    font = "Exo 2",
    size = 35,
    weight = 600,
    extended = true
})

-- Materials
local bgMaterial = Material("materials/ft_ui/military/vnu/charcreate/bg.png", "smooth")
local logoMaterial = Material("materials/ft_ui/military/vnu/charcreate/logo.png", "smooth")

-- Main pause menu frame
local pauseMenu = nil

-- Create button function
local function CreateMenuButton(parent, text, x, y)
    local btn = vgui.Create("DButton", parent)
    btn:SetPos(x, y)
    btn:SetSize(350, 66)
    btn:SetText("")
    
    btn.Paint = function(self, w, h)
        -- Background with border
        surface.SetDrawColor(13, 13, 13, 217) -- #0D0D0D with 85% opacity
        draw.RoundedBox(10, 0, 0, w, h, Color(13, 13, 13, 217))
        
        -- Border
        surface.SetDrawColor(1, 67, 29, 255) -- #01431D
        surface.DrawOutlinedRect(0, 0, w, h, 2)
        
        -- Text centered
        draw.SimpleText(text, "PauseMenuButton", w / 2, h / 2, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        
        -- Hover effect
        if self:IsHovered() then
            surface.SetDrawColor(1, 104, 44, 30) -- #01682C with low opacity
            draw.RoundedBox(10, 0, 0, w, h, Color(1, 104, 44, 30))
        end
    end
    
    return btn
end

-- Create pause menu
local function CreatePauseMenu()
    if IsValid(pauseMenu) then
        pauseMenu:Remove()
        return
    end
    
    pauseMenu = vgui.Create("DFrame")
    pauseMenu:SetSize(ScrW(), ScrH())
    pauseMenu:SetPos(0, 0)
    pauseMenu:SetTitle("")
    pauseMenu:SetDraggable(false)
    pauseMenu:ShowCloseButton(false)
    pauseMenu:MakePopup()
    
    -- Закрытие меню по Esc
    pauseMenu.OnKeyCodePressed = function(self, key)
        if key == KEY_ESCAPE then
            self:Close()
            gui.HideGameUI()
            return true
        end
    end
    
    pauseMenu.Paint = function(self, w, h)
        -- Background image
        surface.SetDrawColor(255, 255, 255, 255)
        surface.SetMaterial(bgMaterial)
        surface.DrawTexturedRect(0, 0, w, h)
        
        -- Dark overlay
        surface.SetDrawColor(10, 10, 10, 140) -- rgba(10, 10, 10, 0.55)
        surface.DrawRect(0, 0, w, h)
        
        -- Top bar
        surface.SetDrawColor(13, 13, 13, 255) -- #0D0D0D
        surface.DrawRect(0, 0, w, 100)
        
        -- Logo (centered at 885, 23 for 1920x1080)
        local logoX = (w / 2) - 75 -- Center logo (150px width / 2)
        local logoY = 23
        surface.SetDrawColor(255, 255, 255, 255)
        surface.SetMaterial(logoMaterial)
        surface.DrawTexturedRect(logoX, logoY, 150, 53)
        
        -- Title "Front Team" (767, 284)
        local titleX = (w / 2) - 192 -- Approximate center for "Front Team"
        local titleY = 284
        --draw.SimpleText("Front Team", "PauseMenuTitle", titleX, titleY, Color(255, 255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
    end
    
    -- Calculate center X position for buttons (785px for 1920px screen)
    local centerX = (ScrW() / 2) - 175 -- 350px button width / 2
    
    -- Button 1: Вернуться в игру (380px from top)
    local btnReturn = CreateMenuButton(pauseMenu, "Вернуться в игру", centerX, 380)
    btnReturn.DoClick = function()
        pauseMenu:Close()
        gui.HideGameUI()
    end
    
    -- Button 2: Правила (450px from top)
    local btnRules = CreateMenuButton(pauseMenu, "Правила", centerX, 450)
    btnRules.DoClick = function()
        -- Open rules URL or show rules frame
        gui.OpenURL("https://sites.google.com/view/frontteamsite/правила") -- Replace with actual URL
    end
    
    -- Button 3: Discord (520px from top)
    local btnDiscord = CreateMenuButton(pauseMenu, "Discord", centerX, 520)
    btnDiscord.DoClick = function()
        -- Open Discord invite link
        gui.OpenURL("https://discord.gg/paQdrP7aD7") -- Replace with actual Discord link
    end
    
    -- Button 4: Настройки (590px from top)
    local btnSettings = CreateMenuButton(pauseMenu, "Настройки", centerX, 590)
    btnSettings.DoClick = function()
        if IsValid(pauseMenu) then
            pauseMenu:Close()
        end
        
        gui.ActivateGameUI()
        
        timer.Simple(0.1, function()
            RunConsoleCommand("gamemenucommand", "openoptionsdialog")
        end)
    end

    
    -- Button 6: Выйти с сервера (730px from top)
    local btnDisconnect = CreateMenuButton(pauseMenu, "Выйти с сервера", centerX, 730)
    btnDisconnect.DoClick = function()
        RunConsoleCommand("disconnect")
    end
    
    -- Button 7: Config (800px from top) - только для superadmin
    if LocalPlayer():IsSuperAdmin() then
        local btnConfig = CreateMenuButton(pauseMenu, "Config", centerX, 800)
        btnConfig.DoClick = function()
            -- Создаем DFrame с конфигом Helix
            local configFrame = vgui.Create("DFrame")
            configFrame:SetSize(ScrW() * 0.8, ScrH() * 0.8)
            configFrame:Center()
            configFrame:SetTitle("Helix Config")
            configFrame:MakePopup()
            
            -- Создаем tabs панель
            local tabs = configFrame:Add("DPropertySheet")
            tabs:Dock(FILL)
            
            -- Вкладка Config
            local configPanel = vgui.Create("DPanel")
            configPanel:Dock(FILL)
            
            local configManager = configPanel:Add("ixConfigManager")
            
            tabs:AddSheet("Config", configPanel, "icon16/cog.png")
            
            -- Вкладка Plugins
            local pluginsPanel = vgui.Create("DPanel")
            pluginsPanel:Dock(FILL)
            
            local pluginManager = pluginsPanel:Add("ixPluginManager")
            ix.gui.pluginManager = pluginManager
            
            tabs:AddSheet("Plugins", pluginsPanel, "icon16/plugin.png")
            
            -- При закрытии фрейма убираем ссылку на pluginManager
            configFrame.OnClose = function()
                ix.gui.pluginManager = nil
            end
        end
    end
    
    return pauseMenu
end

-- Hook to show custom pause menu
hook.Add("OnPauseMenuShow", "ixShowCustomPauseMenu", function()
    if IsValid(ix.plugin.list["f4menu"].f4Menu) then
        ix.plugin.list["f4menu"].f4Menu:Remove()
        return false
    end

    CreatePauseMenu()
    return false -- Prevent default pause menu
end)

-- Console command to open pause menu (for testing)
concommand.Add("ix_pausemenu", function()
    CreatePauseMenu()
end)
