-- ============================================================================
-- Chat Frame Buttons
-- ============================================================================
AIOS_CopyBox_ChatButtons = {
    buttons = {},
    enabled = true
}

function AIOS_CopyBox_ChatButtons:Initialize()
    if not AIOS_CopyBox_Config.behavior.enableChatButtons then
        return
    end
    
    self:CreateChatButtons()
    
    if AIOS_CopyBox_Logger then
        AIOS_CopyBox_Logger:Debug("Chat buttons initialized", "ChatButtons")
    end
end

function AIOS_CopyBox_ChatButtons:CreateChatButtons()
    local maxFrames = math.min(NUM_CHAT_WINDOWS, AIOS_CopyBox_Config.chat.maxChatFrames)
    
    for i = 1, maxFrames do
        self:CreateChatButton(i)
    end
end

function AIOS_CopyBox_ChatButtons:CreateChatButton(chatFrameId)
    local cf = _G["ChatFrame"..chatFrameId]
    if not cf then
        if AIOS_CopyBox_Logger then
            AIOS_CopyBox_Logger:Warn("ChatFrame" .. chatFrameId .. " not found", "ChatButtons")
        end
        return
    end
    
    -- Remove existing button if it exists
    if self.buttons[chatFrameId] then
        self.buttons[chatFrameId]:Hide()
        self.buttons[chatFrameId] = nil
    end
    
    -- Create new button
    local btn = CreateFrame("Button", "AIOS_CopyBox_Button"..chatFrameId, cf)
    btn:SetSize(AIOS_CopyBox_Config.ui.buttonSize, AIOS_CopyBox_Config.ui.buttonSize)
    btn:SetPoint("BOTTOMRIGHT", cf, "BOTTOMRIGHT", -2, 2)
    btn:SetFrameLevel(cf:GetFrameLevel() + 5)
    btn:EnableMouse(true)
    btn:RegisterForClicks("AnyUp")
    
    -- Button texture
    local tex = btn:CreateTexture(nil, "ARTWORK")
    tex:SetAllPoints()
    self:SetButtonIcon(tex)
    tex:SetAlpha(0.3)
    
    -- Tooltip and interactions
    btn:SetScript("OnEnter", function(self)
        tex:SetAlpha(0.8)
        if AIOS_CopyBox_Config.behavior.enableTooltips then
            GameTooltip:SetOwner(self, "ANCHOR_TOP")
            GameTooltip:SetText(AIOS_CopyBox_Config.tooltips.copyBox.title)
            GameTooltip:Show()
        end
    end)
    
    btn:SetScript("OnLeave", function()
        tex:SetAlpha(0.3)
        GameTooltip:Hide()
    end)
    
    btn:SetScript("OnClick", function()
        if AIOS_CopyBox_MainFrame and AIOS_CopyBox_MainFrame.frame and AIOS_CopyBox_MainFrame.frame:IsShown() then
            AIOS_CopyBox_MainFrame:Close()
        else
            if AIOS_CopyBox_ChatManager then
                AIOS_CopyBox_ChatManager:OpenCopyBox(chatFrameId)
            end
        end
    end)
    
    -- Only show button for first chat frame by default
    btn:SetShown(chatFrameId == 1)
    self.buttons[chatFrameId] = btn
    
    if AIOS_CopyBox_Logger then
        AIOS_CopyBox_Logger:Debug("Created button for ChatFrame" .. chatFrameId, "ChatButtons")
    end
end

function AIOS_CopyBox_ChatButtons:SetButtonIcon(tex)
    if not tex then return end
    
    local foundTexture = false
    for _, path in ipairs(AIOS_CopyBox_Config.textures.logo) do
        tex:SetTexture(path)
        local currentTexture = tex:GetTexture()
        if currentTexture and currentTexture ~= "" then
            foundTexture = true
            break
        end
    end
    
    if not foundTexture then
        tex:SetTexture(AIOS_CopyBox_Config.textures.fallback.logo)
    end
    
    tex:SetDesaturated(false)
    tex:SetTexCoord(0, 1, 0, 1)
end

function AIOS_CopyBox_ChatButtons:ShowAllButtons()
    for id, btn in pairs(self.buttons) do
        if btn then
            btn:Show()
        end
    end
end

function AIOS_CopyBox_ChatButtons:HideAllButtons()
    for id, btn in pairs(self.buttons) do
        if btn then
            btn:Hide()
        end
    end
end

function AIOS_CopyBox_ChatButtons:ToggleButtons()
    self.enabled = not self.enabled
    if self.enabled then
        self:ShowAllButtons()
    else
        self:HideAllButtons()
    end
end