-- ============================================================================
-- Theme Management
-- ============================================================================
AIOS_CopyBox_ThemeManager = {
    currentThemeIndex = 1,
    availableThemes = {}
}

function AIOS_CopyBox_ThemeManager:Initialize()
    self:LoadAvailableThemes()
    self.currentThemeIndex = AIOS_CopyBox_Meta:GetDefaultThemeIndex()
    self:ApplyCurrentTheme()
end

function AIOS_CopyBox_ThemeManager:LoadAvailableThemes()
    self.availableThemes = AIOS_CopyBox_Meta:GetAvailableThemes()
end

function AIOS_CopyBox_ThemeManager:ApplyCurrentTheme()
    if not AIOS_CopyBox_MainFrame then return end

    local theme = self:GetCurrentTheme()
    if not theme then return end

    -- Update theme text in main frame
    if AIOS_CopyBox_MainFrame.themeText then
        AIOS_CopyBox_MainFrame.themeText:SetText("|cff66ccff" .. theme.name .. "|r")
    end

    -- Apply visual theme to frame
    self:ApplyVisualTheme(theme)

    if AIOS_CopyBox_Logger then
        AIOS_CopyBox_Logger:Debug("Applied theme: " .. theme.name .. " (Type: " .. (theme.type or "basic") .. ")", "Themes")
    end
end

function AIOS_CopyBox_ThemeManager:ApplyVisualTheme(theme)
    if not AIOS_CopyBox_MainFrame or not AIOS_CopyBox_MainFrame.frame then return end

    if theme.background.type == "texture" then
        -- Advanced theme with texture
        local scaleX = theme.scaleX or theme.scale or 1.0
        local scaleY = theme.scaleY or theme.scale or 1.0
        local position = theme.position or "CENTER"

        if AIOS_CopyBox_MainFrame.ApplyBackgroundTexture then
            AIOS_CopyBox_MainFrame:ApplyBackgroundTexture(theme.background.texture, scaleX, scaleY, position)
        end

        -- Hide border for advanced themes to show full texture
        if AIOS_CopyBox_MainFrame.SetBorderVisibility then
            AIOS_CopyBox_MainFrame:SetBorderVisibility(false)
        end

        if AIOS_CopyBox_Logger then
            AIOS_CopyBox_Logger:Debug("Applied texture scaling - X: " .. scaleX .. ", Y: " .. scaleY .. ", Position: " .. position, "Themes")
        end
    else
        -- Basic theme with color
        if AIOS_CopyBox_MainFrame.ApplyBackgroundColor then
            AIOS_CopyBox_MainFrame:ApplyBackgroundColor(
                theme.background.color.r,
                theme.background.color.g,
                theme.background.color.b,
                theme.background.color.a
            )
        end

        -- Show border for basic themes
        if AIOS_CopyBox_MainFrame.SetBorderVisibility then
            AIOS_CopyBox_MainFrame:SetBorderVisibility(true)
        end

        -- Set border color for basic themes
        if AIOS_CopyBox_MainFrame.SetBorderColor then
            AIOS_CopyBox_MainFrame:SetBorderColor(
                theme.border.r,
                theme.border.g,
                theme.border.b,
                theme.border.a
            )
        end
    end

    -- Update gloss texture
    if AIOS_CopyBox_MainFrame.SetGlossColor then
        if theme.type == "advanced" then
            -- Reduced gloss for advanced themes
            AIOS_CopyBox_MainFrame:SetGlossColor(
                theme.gloss.r,
                theme.gloss.g,
                theme.gloss.b,
                theme.gloss.a * 0.2
            )
        else
            -- Normal gloss for basic themes
            AIOS_CopyBox_MainFrame:SetGlossColor(
                theme.gloss.r,
                theme.gloss.g,
                theme.gloss.b,
                theme.gloss.a
            )
        end
    end
end

function AIOS_CopyBox_ThemeManager:CycleTheme(forward)
    if #self.availableThemes == 0 then return end

    if forward then
        self.currentThemeIndex = (self.currentThemeIndex % #self.availableThemes) + 1
    else
        self.currentThemeIndex = self.currentThemeIndex - 1
        if self.currentThemeIndex <= 0 then
            self.currentThemeIndex = #self.availableThemes
        end
    end

    self:ApplyCurrentTheme()

    -- Save theme to DB
    if AIOS_CopyBoxDB then
        AIOS_CopyBoxDB.themeIndex = self.currentThemeIndex
    end
end

function AIOS_CopyBox_ThemeManager:GetCurrentTheme()
    return self.availableThemes[self.currentThemeIndex]
end

function AIOS_CopyBox_ThemeManager:SetTheme(index)
    if index >= 1 and index <= #self.availableThemes then
        self.currentThemeIndex = index
        self:ApplyCurrentTheme()

        -- Save theme to DB
        if AIOS_CopyBoxDB then
            AIOS_CopyBoxDB.themeIndex = self.currentThemeIndex
        end

        return true
    end
    return false
end

function AIOS_CopyBox_ThemeManager:GetThemeCount()
    return #self.availableThemes
end