-- Garry's Mod Binder - Custom Frame
local PANEL = {}

function PANEL:Init()
    self:SetSize(ScrW() * 0.8, ScrH() * 0.8)
    self:Center()
    self:MakePopup()

    self.AccentColor = Binder.Config.AccentColor

    self.CurrentTab = "Profiles"

    self.HeaderHeight = 60
    self.TabHeight = 40

    -- Tabs definitions
    self.Tabs = {
        {id = "Profiles", name = Binder.GetPhrase("profiles")},
        {id = "Keybinds", name = Binder.GetPhrase("keybinds")},
        {id = "Radial", name = Binder.GetPhrase("radial")},
        {id = "Settings", name = Binder.GetPhrase("settings")},
    }

    -- Close Button
    self.CloseBtn = vgui.Create("DButton", self)
    self.CloseBtn:SetText("✕")
    self.CloseBtn:SetFont("Binder_Main")
    self.CloseBtn:SetTextColor(Color(255, 255, 255, 150))
    self.CloseBtn.DoClick = function() self:Remove() end
    self.CloseBtn.Paint = function(s, w, h)
        if s:IsHovered() then
            s:SetTextColor(Color(255, 255, 255, 255))
        else
            s:SetTextColor(Color(255, 255, 255, 150))
        end
    end

    -- Tab Content Container
    self.Content = vgui.Create("DPanel", self)
    self.Content:Dock(FILL)
    self.Content:DockMargin(0, self.HeaderHeight + self.TabHeight, 0, 0)
    self.Content.Paint = function(s, w, h) end

    self:SwitchTab("Profiles")
end

include("binder/vgui/cl_profiles_tab.lua")
include("binder/vgui/cl_keybind_tab.lua")
include("binder/vgui/cl_radial_tab.lua")
include("binder/vgui/cl_settings_tab.lua")

function PANEL:SwitchTab(id)
    self.CurrentTab = id
    self.Content:Clear()
    
    if id == "Profiles" then
        self.ActiveTab = vgui.Create("Binder_ProfilesTab", self.Content)
    elseif id == "Keybinds" then
        self.ActiveTab = vgui.Create("Binder_KeybindsTab", self.Content)
    elseif id == "Radial" then
        self.ActiveTab = vgui.Create("Binder_RadialTab", self.Content)
    elseif id == "Settings" then
        self.ActiveTab = vgui.Create("Binder_SettingsTab", self.Content)
    else
        local label = vgui.Create("DLabel", self.Content)


        label:SetText("Content for " .. id)
        label:SetFont("Binder_Title")
        label:SizeToContents()
        label:Center()
    end
end

function PANEL:RefreshActiveTab()
    if IsValid(self.ActiveTab) and self.ActiveTab.Refresh then
        self.ActiveTab:Refresh()
    elseif self.CurrentTab == "Profiles" and IsValid(self.ActiveTab) then
        -- Fallback if the tab logic doesn't have a specific refresh method, 
        -- though we should add :Refresh to all of them.
        self:SwitchTab(self.CurrentTab)
    end
end

function PANEL:PerformLayout(w, h)
    if IsValid(self.CloseBtn) then
        self.CloseBtn:SetSize(30, 30)
        self.CloseBtn:SetPos(w - 40, 15)
    end
end

function PANEL:Paint(w, h)
    -- Background with blur
    draw.RoundedBox(8, 0, 0, w, h, Color(30, 30, 30, 200))
    
    -- Header
    draw.RoundedBoxEx(8, 0, 0, w, self.HeaderHeight, self.AccentColor, true, true, false, false)
    draw.SimpleText(Binder.GetPhrase("title"), "Binder_Title", 20, self.HeaderHeight / 2, Color(255, 255, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)

    -- Tabs Bar
    draw.RoundedBox(0, 0, self.HeaderHeight, w, self.TabHeight, Color(45, 45, 45))
    
    local tabWidth = w / #self.Tabs
    for i, tab in ipairs(self.Tabs) do
        local x = (i - 1) * tabWidth
        local isActive = self.CurrentTab == tab.id
        
        if isActive then
            draw.RoundedBox(0, x, self.HeaderHeight, tabWidth, self.TabHeight, Color(255, 255, 255, 10))
            draw.RoundedBox(0, x + 20, self.HeaderHeight + self.TabHeight - 3, tabWidth - 40, 3, Color(255, 255, 255))
        end

        local isHovered = gui.MouseX() >= self:GetPos() + x and gui.MouseX() < self:GetPos() + x + tabWidth and
                          gui.MouseY() >= self:GetY() + self.HeaderHeight and gui.MouseY() < self:GetY() + self.HeaderHeight + self.TabHeight
        
        if isHovered and not isActive then
             draw.RoundedBox(0, x, self.HeaderHeight, tabWidth, self.TabHeight, Color(255, 255, 255, 5))
        end

        draw.SimpleText(tab.name, "Binder_Tab", x + tabWidth / 2, self.HeaderHeight + self.TabHeight / 2, 
            isActive and Color(255, 255, 255) or Color(150, 150, 150), 
            TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
            
        -- Hidden button for tab click
        if not self.TabButtons then self.TabButtons = {} end
        if not self.TabButtons[i] then
            local btn = vgui.Create("DButton", self)
            btn:SetText("")
            btn.Paint = function() end
            btn.DoClick = function() self:SwitchTab(tab.id) end
            self.TabButtons[i] = btn
        end
        self.TabButtons[i]:SetSize(tabWidth, self.TabHeight)
        self.TabButtons[i]:SetPos(x, self.HeaderHeight)
    end
end

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