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

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

    self.Scroll = vgui.Create("DScrollPanel", self)
    self.Scroll:Dock(FILL)
    
    local sbar = self.Scroll:GetVBar()
    sbar:SetWide(6)
    sbar:SetHideButtons(true)
    sbar.Paint = function(s, w, h) end
    sbar.btnGrip.Paint = function(s, w, h)
        draw.RoundedBox(4, 0, 0, w, h, Color(255, 255, 255, 30))
    end

    self:AddCategory(Binder.GetPhrase("general_settings"), {
        {type = "check", label = Binder.GetPhrase("show_feedback"), convar = "binder_show_feedback"},
        {type = "check", label = Binder.GetPhrase("dark_theme"), convar = "binder_dark_theme"},
    })

    self:AddCategory("Radial Menu Key", {
        {type = "keybind", label = "Activation Key", convar = "binder_radial_key"},
    })

    self:AddCategory(Binder.GetPhrase("import_defaults"), {
        {type = "button", label = Binder.GetPhrase("import_btn"), btnText = Binder.GetPhrase("import_btn"), 
         onClick = function() 
             Binder.Profiles = {}
             table.insert(Binder.Profiles, {Name = Binder.GetPhrase("raiding_profile"), Binds = {}, Radial = {}, Active = true})
             Binder.SaveToServer()
             if Binder.Frame and IsValid(Binder.Frame) then Binder.Frame:RefreshActiveTab() end
         end},
    })

    self:AddCategory(Binder.GetPhrase("appearance_settings"), {
        {type = "color", label = Binder.GetPhrase("accent_color")},
        {type = "presets", label = Binder.GetPhrase("preset_colors")},
    })
end

function PANEL:AddCategory(title, items)
    local cat = self.Scroll:Add("DPanel")
    cat:Dock(TOP)
    cat:DockMargin(0, 0, 0, 20)
    cat:SetHeight(40 + #items * 40)
    cat.Paint = function(s, w, h)
        draw.RoundedBox(4, 0, 0, w, 30, Binder.Config.AccentColor)
        draw.SimpleText(title, "Binder_Main", 10, 15, Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
        draw.RoundedBoxEx(4, 0, 30, w, h - 30, Color(40, 40, 40), false, false, true, true)
    end

    local itemContainer = vgui.Create("DPanel", cat)
    itemContainer:Dock(FILL)
    itemContainer:DockMargin(10, 35, 10, 5)
    itemContainer.Paint = function() end

    for _, item in ipairs(items) do
        local row = vgui.Create("DPanel", itemContainer)
        row:Dock(TOP)
        row:SetHeight(35)
        row.Paint = function() end

        if item.type == "check" and item.convar then
            local chk = vgui.Create("DCheckBox", row)
            chk:SetPos(0, 10)
            chk:SetConVar(item.convar)
            
            local lbl = vgui.Create("DLabel", row)
            lbl:SetText(item.label)
            lbl:SetFont("Binder_Small")
            lbl:SetPos(25, 8)
            lbl:SizeToContents()
            
        elseif item.type == "keybind" and item.convar then
             local lbl = vgui.Create("DLabel", row)
             lbl:SetText(item.label)
             lbl:SetFont("Binder_Small")
             lbl:SetPos(0, 8)
             lbl:SizeToContents()
            
             local binderBtn = vgui.Create("DBinder", row)
             binderBtn:SetSize(120, 20)
             binderBtn:SetPos(150, 8)
             binderBtn:SetConVar(item.convar)
             binderBtn.Paint = function(s, w, h)
                 draw.RoundedBox(4, 0, 0, w, h, Color(60, 60, 60))
                 draw.SimpleText(input.GetKeyName(s:GetValue()) or "NONE", "Binder_Small", w/2, h/2, Color(255,255,255), 1, 1)
             end
            
        elseif item.type == "button" then
            local btn = vgui.Create("DButton", row)
            btn:SetText(item.btnText)
            btn:SetFont("Binder_Small")
            btn:SetSize(150, 25)
            btn:SetPos(0, 5)
            btn:SetTextColor(Color(255, 255, 255))
            btn.Paint = function(s, w, h)
                draw.RoundedBox(4, 0, 0, w, h, Binder.Config.AccentColor)
            end
            if item.onClick then btn.DoClick = item.onClick end
            
        elseif item.type == "color" then
            local lbl = vgui.Create("DLabel", row)
            lbl:SetText(item.label)
            lbl:SetFont("Binder_Small")
            lbl:SetPos(0, 8)
            lbl:SizeToContents()

            local colPreview = vgui.Create("DPanel", row)
            colPreview:SetSize(30, 20)
            colPreview:SetPos(150, 8)
            colPreview.Paint = function(s, w, h)
                draw.RoundedBox(4, 0, 0, w, h, Binder.Config.AccentColor)
            end

            local btn = vgui.Create("DButton", row)
            btn:SetText(Binder.GetPhrase("change_color"))
            btn:SetSize(100, 20)
            btn:SetPos(190, 8)
            btn:SetFont("Binder_Small")
            btn:SetTextColor(Color(255, 255, 255))
            btn.Paint = function(s, w, h)
                draw.RoundedBox(4, 0, 0, w, h, Color(60, 60, 60))
            end
            btn.DoClick = function()
                local frame = vgui.Create("DFrame")
                frame:SetTitle("Select Theme Color")
                frame:SetSize(300, 250)
                frame:Center()
                frame:MakePopup()
                
                local mixer = vgui.Create("DColorMixer", frame)
                mixer:Dock(FILL)
                mixer:SetPalette(true)
                mixer:SetAlphaBar(false)
                mixer:SetWangs(true)
                mixer:SetColor(Binder.Config.AccentColor)
                
                local apply = vgui.Create("DButton", frame)
                apply:Dock(BOTTOM)
                apply:SetText("Apply")
                apply.DoClick = function()
                    Binder.Config.AccentColor = mixer:GetColor()
                    if IsValid(Binder.Frame) then Binder.Frame.AccentColor = Binder.Config.AccentColor end
                    frame:Remove()
                end
            end
            
        elseif item.type == "presets" then
            local lbl = vgui.Create("DLabel", row)
            lbl:SetText(item.label)
            lbl:SetFont("Binder_Small")
            lbl:SetPos(0, 8)
            lbl:SizeToContents()

            local presets = {Color(0, 67, 28), Color(52, 152, 219), Color(46, 204, 113), Color(231, 76, 60), Color(155, 89, 182), Color(230, 126, 34)}
            for i, col in ipairs(presets) do
                local p = vgui.Create("DButton", row)
                p:SetSize(20, 20)
                p:SetPos(150 + (i-1)*25, 8)
                p:SetText("")
                p.Paint = function(s, w, h)
                    draw.RoundedBox(4, 0, 0, w, h, col)
                    if Binder.Config.AccentColor == col then
                        surface.SetDrawColor(255, 255, 255)
                        surface.DrawOutlinedRect(0, 0, w, h, 2)
                    end
                end
                p.DoClick = function()
                    Binder.Config.AccentColor = col
                    if IsValid(Binder.Frame) then Binder.Frame.AccentColor = col end
                end
            end
        end
    end
end

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