-- Garry's Mod Binder - Profiles Tab
local PANEL = {}

function PANEL:Init()
    self:Dock(FILL)
    self:DockMargin(40, 20, 40, 20)
    self.Paint = function() end

    -- Title and Description
    self.Header = vgui.Create("DPanel", self)
    self.Header:Dock(TOP)
    self.Header:SetHeight(50)
    self.Header.Paint = function(s, w, h)
        draw.SimpleText(Binder.GetPhrase("profile_management"), "Binder_Title", 0, 0, Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP)
    end

    -- Profile Grid
    self.Grid = vgui.Create("DIconLayout", self)
    self.Grid:Dock(FILL)
    self.Grid:SetSpaceX(20)
    self.Grid:SetSpaceY(20)

    self:Refresh()
end

function PANEL:Refresh()
    self.Grid:Clear()

    local profiles = Binder.Profiles or {}

    for i, profile in ipairs(profiles) do
        local card = self.Grid:Add("DButton")
        card:SetSize(200, 150)
        card:SetText("")
        card.Paint = function(s, w, h)
            local bgColor = profile.Active and Binder.Config.AccentColor or Color(40, 40, 40)
            draw.RoundedBox(8, 0, 0, w, h, bgColor)
            
            draw.SimpleText(profile.Name, "Binder_Main", w / 2, h / 2 - 10, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
            
            local bindCount = table.Count(profile.Binds or {})
            draw.SimpleText(Binder.GetPhrase("binds_count", bindCount), "Binder_Small", w / 2, h / 2 + 15, Color(255, 255, 255, 100), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
            
            if profile.Active then
                draw.RoundedBox(4, w - 15, 10, 8, 8, Color(0, 255, 0)) -- Active dot
            end
        end
        card.DoClick = function()
            -- Set active
            for _, p in ipairs(Binder.Profiles) do p.Active = false end
            profile.Active = true
            Binder.SaveToServer()
            self:Refresh()
        end
        card.DoRightClick = function()
            -- Delete profile
            if #Binder.Profiles > 1 then
                local menu = DermaMenu()
                menu:AddOption(Binder.GetPhrase("delete"), function()
                    table.remove(Binder.Profiles, i)
                    -- ensure one is active
                    local hasActive = false
                    for _, p in ipairs(Binder.Profiles) do if p.Active then hasActive = true break end end
                    if not hasActive and #Binder.Profiles > 0 then Binder.Profiles[1].Active = true end
                    
                    Binder.SaveToServer()
                    self:Refresh()
                end):SetIcon("icon16/delete.png")
                menu:Open()
            end
        end
    end

    -- Create New Profile Buttons (to fill up to 6 as in screenshot)
    if #profiles < 6 then
        local card = self.Grid:Add("DButton")
        card:SetSize(200, 150)
        card:SetText("")
        card.Paint = function(s, w, h)
            draw.RoundedBox(8, 0, 0, w, h, Color(40, 40, 40))
            draw.SimpleText("+", "Binder_Title", w / 2, h / 2 - 15, Color(255, 255, 255, 150), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
            draw.SimpleText(Binder.GetPhrase("create_profile"), "Binder_Main", w / 2, h / 2 + 20, Color(255, 255, 255, 150), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
        end
        card.DoClick = function()
            Derma_StringRequest(
                Binder.GetPhrase("create_profile"),
                "Enter profile name:",
                "New Profile",
                function(text)
                    table.insert(Binder.Profiles, {
                        Name = text,
                        Binds = {},
                        Radial = {},
                        Active = false
                    })
                    Binder.SaveToServer()
                    self:Refresh()
                end
            )
        end
    end
end

vgui.Register("Binder_ProfilesTab", PANEL, "EditablePanel")
