local function RX(x) return x * (ScrW() / 1920) end
local function RY(y) return y * (ScrH() / 1080) end

local bladw_materials = {
    background = Material("xyoss/cMenu/background.png"),
    backgroundButton = Material("xyoss/cMenu/backgroundButton.png"),
    backgroundButtonHover = Material("xyoss/cMenu/backgroundButtonHover.png"),
    backpoliciers = Material("xyoss/cMenu/backpoliciers.png"),
    backpompiers = Material("xyoss/cMenu/backpompiers.png"),
    discord = Material("xyoss/cMenu/discordbutton.png"),
    steam = Material("xyoss/cMenu/steambutton.png"),
    website = Material("xyoss/cMenu/webButton.png"),
}

local CMenu

local function HoveredButton(button, mat, label, font)

    if type(mat) == "string" then
        mat = Material(mat, "noclamp smooth")
    end


    button.Paint = function(self, w, h)
        local bgcolor = Color(0, 0, 0, 215)
        local textcolor = Color(255, 255, 255, 255)

        if self:IsHovered() then
            bgcolor = Color(255, 255, 255, 111)
            textcolor = Color(0, 0, 0)
        end

        draw.RoundedBox(12, 0, 0, w, h, bgcolor)
        surface.SetDrawColor(255, 255, 255)
        surface.SetMaterial(mat)
        surface.DrawTexturedRect(15, 12.5, w / 4 , h / 2)
        draw.SimpleText(label, font, w / 1.55, h / 2.1, textcolor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
    end
end

local function HoveredButton2(button, label, font)

    button.Paint = function(self, w, h)
        local bgmat = bladw_materials.backgroundButton
        
        if self:IsHovered() then
            bgmat = bladw_materials.backgroundButtonHover
        end

        surface.SetDrawColor(255, 255, 255)
        surface.SetMaterial(bgmat)
        surface.DrawTexturedRect(0, 0, w, h)
        draw.SimpleText(label, font, w / 1.9, h / 2.1, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
    end
end

local function CreateMenuButton(parent, y, label, command)
    local btn = vgui.Create("DButton", parent)
    btn:SetSize(RX(400), RY(122))
    btn:SetPos(RX(0), RY(y))
    btn:SetText("")
    HoveredButton2(btn, label, "bladw_text")
    btn.DoClick = function()
        ply:ConCommand(command)
    end
    return btn
end

local function CreateLinkButton(parent, x, y, icon, label, url)
    local btn = vgui.Create("DButton", parent)
    btn:SetSize(RX(150), RY(51))
    btn:SetPos(RX(x), RY(y))
    btn:SetText("")
    HoveredButton(btn, icon, label, "bladw_text_Medium")
    btn.DoClick = function()
        gui.OpenURL(url)
    end
    return btn
end

local function CloseCMenu()
    if IsValid(CMenu) then
        CMenu:Remove()
        CMenu = nil
        gui.EnableScreenClicker(false)
    end
end

local cMenuButtons = {
    { y = 250, label = "3ème personne",    command = "bladw_3pers"  },
    { y = 325, label = "Stopsound",        command = "bladw_sound"  },
    { y = 400, label = "Appelez un staff", command = "bladw_staff"  },
    { y = 475, label = "Jeter de l'argent",command = "bladw_money"  },
    { y = 550, label = "Jeter une arme",   command = "bladw_weapon" },
}

local linkButtons = {
    { x = 50,  y = 942,  icon = bladw_materials.discord, label = "Discord", url = "https://discord.gg/KsE4Xef6vg" },
    { x = 207, y = 942,  icon = bladw_materials.steam,   label = "Steam",   url = "" },
    { x = 125, y = 1005, icon = bladw_materials.website, label = "Website", url = "" },
}

local function GetStats()
    local nbplayer = player.GetCount()
    local nbpolice = team.NumPlayers(TEAM_POLICE)
    local nbpompiers = team.NumPlayers(TEAM_POMPIERS)

    return nbplayer, nbpolice, nbpompiers
end

function OpenCMenu()
    if IsValid(CMenu) then return end
    local nbplayer, nbpolice, nbpompiers = GetStats()

    local ply = LocalPlayer()

    CMenu = vgui.Create("DFrame")
    CMenu:SetSize(RX(400), ScrH())
    CMenu:SetTitle("")
    CMenu:SetPos(ScrW() - RX(400), 0)
    CMenu:SetDraggable(false)
    CMenu:ShowCloseButton(false)
    CMenu:SetCursor("arrow")
    CMenu:SetAlpha(0)
    CMenu:AlphaTo(255, 0.4, 0)
    CMenu.Paint = function(self, w, h)
        surface.SetDrawColor(255, 255, 255)
        surface.SetMaterial(bladw_materials.background)
        surface.DrawTexturedRect(0, 0, w, h)
        
        surface.SetFont("bladw_text")
        local before = "Il y a "
        local count  = nbplayer
        local after  = " joueurs sur le serveur"
        
        local beforeW, _ = surface.GetTextSize(before)
        local countW,  _ = surface.GetTextSize(count)
        local totalW,  _ = surface.GetTextSize(before .. count .. after)
        local startX     = w / 2 - totalW / 2
        
        draw.SimpleText(before, "bladw_text", startX,  RY(900), Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
        draw.SimpleText(count,  "bladw_text", startX + beforeW, RY(900), Color(255, 165, 0),   TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
        draw.SimpleText(after,  "bladw_text", startX + beforeW + countW, RY(900), Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
    end

    for _, btnData in ipairs(cMenuButtons) do
        CreateMenuButton(CMenu, btnData.y, btnData.label, btnData.command)
    end

    PoliceButton = vgui.Create("DPanel", CMenu)
    PoliceButton:SetSize(RX(260), RY(51))
    PoliceButton:SetPos(RX(75), RY(732))
    PoliceButton.Paint = function(self, w, h)
        surface.SetDrawColor(255, 255, 255)
        surface.SetMaterial(bladw_materials.backpoliciers)
        surface.DrawTexturedRect(0, 0, w, h)

        surface.SetFont("bladw_text")
        local count   = nbpolice
        local after   = " Gendarmes en service"

        local countW,  _ = surface.GetTextSize(count)
        local totalW,  _ = surface.GetTextSize(count .. after)
        local startX     = w - totalW / 1.2 

        draw.SimpleText(count,  "bladw_text_Medium", startX, h / 2, Color(100, 180, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
        draw.SimpleText(after,  "bladw_text_Medium", startX + countW, h / 2, Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
    end

    PompiersButton = vgui.Create("DPanel", CMenu)
    PompiersButton:SetSize(RX(260), RY(51))
    PompiersButton:SetPos(RX(75), RY(790))
    PompiersButton.Paint = function(self, w, h)
        surface.SetDrawColor(255, 255, 255)
        surface.SetMaterial(bladw_materials.backpompiers)
        surface.DrawTexturedRect(0, 0, w, h)

        surface.SetFont("bladw_text")
        local count   = nbpompiers
        local after   = " Pompiers en service"

        local countW,  _ = surface.GetTextSize(count)
        local totalW,  _ = surface.GetTextSize(count .. after)
        local startX     = w - totalW / 1.11

        draw.SimpleText(count,  "bladw_text_Medium", startX,          h / 2, Color(255, 100, 50),  TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
        draw.SimpleText(after,  "bladw_text_Medium", startX + countW, h / 2, Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
    end

    for _, btnData in ipairs(linkButtons) do
        CreateLinkButton(CMenu, btnData.x, btnData.y, btnData.icon, btnData.label, btnData.url)
    end
end


hook.Add("PlayerButtonDown", "bladw_CMenu_open", function(ply, key)
    if key == KEY_C then
        OpenCMenu()
        gui.EnableScreenClicker(true)
    end
end)

hook.Add("PlayerButtonUp", "bladw_CMenu_close", function(ply, key)
    if key == KEY_C then
        CloseCMenu()
    end
end)