local addonName, RF = ...

-------------------------------------------------------------------------------
-- Color Palette
-------------------------------------------------------------------------------
RF.Theme = {
    -- Backgrounds
    bgPrimary    = { 0.08, 0.08, 0.10, 0.95 },
    bgSecondary  = { 0.12, 0.12, 0.14, 0.95 },
    bgTertiary   = { 0.16, 0.16, 0.18, 0.90 },
    bgRow        = { 0.10, 0.10, 0.12, 1.00 },
    bgRowAlt     = { 0.13, 0.13, 0.15, 1.00 },
    bgRowHover   = { 0.18, 0.22, 0.30, 1.00 },
    bgRowSelected= { 0.15, 0.25, 0.40, 1.00 },

    -- Accent
    accent       = { 0.23, 0.51, 0.96, 1.00 }, -- #3B82F6
    accentDim    = { 0.23, 0.51, 0.96, 0.50 },
    accentDark   = { 0.15, 0.35, 0.70, 1.00 },

    -- Text
    textPrimary  = { 0.90, 0.90, 0.92, 1.00 },
    textSecondary= { 0.60, 0.60, 0.65, 1.00 },
    textMuted    = { 0.40, 0.40, 0.45, 1.00 },
    textHighlight= { 1.00, 1.00, 1.00, 1.00 },

    -- Borders
    border       = { 0.25, 0.25, 0.28, 1.00 },
    borderLight  = { 0.30, 0.30, 0.35, 1.00 },

    -- Status
    online       = { 0.30, 0.90, 0.30, 1.00 },
    away         = { 1.00, 0.80, 0.00, 1.00 },
    dnd          = { 0.90, 0.30, 0.30, 1.00 },
    offline      = { 0.50, 0.50, 0.50, 1.00 },

    -- Misc
    danger       = { 0.90, 0.25, 0.25, 1.00 },
    success      = { 0.25, 0.80, 0.40, 1.00 },
    warning      = { 1.00, 0.75, 0.20, 1.00 },
}

local T = RF.Theme

-------------------------------------------------------------------------------
-- High Contrast Mode
-------------------------------------------------------------------------------
function RF:ApplyHighContrast()
    local Th = RF.Theme
    if RF.charDB and RF.charDB.highContrastMode then
        Th.textMuted = { 0.55, 0.55, 0.58, 1.00 }
        Th.textSecondary = { 0.72, 0.72, 0.75, 1.00 }
    else
        Th.textMuted = { 0.40, 0.40, 0.45, 1.00 }
        Th.textSecondary = { 0.60, 0.60, 0.65, 1.00 }
    end
end

-- Font sizes
RF.FONT_SIZES = {
    small  = 10,
    normal = 12,
    medium = 13,
    large  = 16,
    header = 20,
}

-------------------------------------------------------------------------------
-- Backdrop Templates
-------------------------------------------------------------------------------
local MAIN_BACKDROP = {
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8",
    edgeSize = 1,
}

local TOOLTIP_BACKDROP = {
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8",
    edgeSize = 1,
}

-------------------------------------------------------------------------------
-- CreateStyledFrame: Main window factory
-------------------------------------------------------------------------------
function RF:CreateStyledFrame(name, parent, width, height, opts)
    opts = opts or {}
    parent = parent or UIParent

    local frame = CreateFrame("Frame", name, parent, "BackdropTemplate")
    frame:SetSize(width, height)
    frame:SetBackdrop(MAIN_BACKDROP)
    frame:SetBackdropColor(unpack(T.bgPrimary))
    frame:SetBackdropBorderColor(unpack(T.border))
    frame:SetFrameStrata(opts.strata or "HIGH")
    frame:SetClampedToScreen(true)

    -- Movable
    if opts.movable ~= false then
        frame:SetMovable(true)
        frame:EnableMouse(true)
    end

    -- Title Bar
    if opts.title then
        local titleBar = CreateFrame("Frame", nil, frame, "BackdropTemplate")
        titleBar:SetHeight(32)
        titleBar:SetPoint("TOPLEFT", 0, 0)
        titleBar:SetPoint("TOPRIGHT", 0, 0)
        titleBar:SetBackdrop(MAIN_BACKDROP)
        titleBar:SetBackdropColor(unpack(T.bgSecondary))
        titleBar:SetBackdropBorderColor(unpack(T.border))
        titleBar:EnableMouse(true)
        titleBar:RegisterForDrag("LeftButton")
        titleBar:SetScript("OnDragStart", function() frame:StartMoving() end)
        titleBar:SetScript("OnDragStop", function()
            frame:StopMovingOrSizing()
            RF:SaveFramePosition(frame)
        end)
        frame.titleBar = titleBar

        local titleText = titleBar:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        titleText:SetPoint("LEFT", 12, 0)
        titleText:SetFont(titleText:GetFont(), RF.FONT_SIZES.large, "")
        titleText:SetTextColor(unpack(T.textPrimary))
        titleText:SetText(opts.title)
        frame.titleText = titleText

        -- Close Button
        if opts.closable ~= false then
            local closeBtn = CreateFrame("Button", nil, titleBar)
            closeBtn:SetSize(24, 24)
            closeBtn:SetPoint("RIGHT", -6, 0)
            closeBtn:SetNormalFontObject("GameFontNormal")

            local closeTex = closeBtn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            closeTex:SetAllPoints()
            closeTex:SetText("X")
            closeTex:SetTextColor(unpack(T.textSecondary))
            closeBtn:SetScript("OnEnter", function()
                closeTex:SetTextColor(unpack(T.danger))
            end)
            closeBtn:SetScript("OnLeave", function()
                closeTex:SetTextColor(unpack(T.textSecondary))
            end)
            closeBtn:SetScript("OnClick", function()
                frame:Hide()
            end)
            frame.closeButton = closeBtn
        end
    end

    -- ESC to close
    if opts.escClose ~= false and name then
        tinsert(UISpecialFrames, name)
    end

    -- Hide in combat
    if opts.combatHide then
        frame:RegisterEvent("PLAYER_REGEN_DISABLED")
        frame:SetScript("OnEvent", function(self, event)
            if event == "PLAYER_REGEN_DISABLED" then
                self:Hide()
            end
        end)
    end

    -- Apply scale and opacity from settings
    if opts.applySettings then
        RF:RegisterCallback("ADDON_LOADED", function()
            if RF.charDB then
                frame:SetScale(RF.charDB.frameScale or 1.0)
                frame:SetAlpha(RF.charDB.frameOpacity or 0.95)
                RF:RestoreFramePosition(frame)
            end
        end)
    end

    return frame
end

-------------------------------------------------------------------------------
-- Frame Position Save/Restore
-------------------------------------------------------------------------------
function RF:SaveFramePosition(frame)
    if not RF.charDB then return end
    local point, _, relPoint, x, y = frame:GetPoint()
    RF.charDB.framePos = { point, relPoint, x, y }
end

function RF:RestoreFramePosition(frame)
    if not RF.charDB or not RF.charDB.framePos then return end
    local p = RF.charDB.framePos
    frame:ClearAllPoints()
    frame:SetPoint(p[1], UIParent, p[2], p[3], p[4])
end

-------------------------------------------------------------------------------
-- SkinButton: Style a button with theme colors
-------------------------------------------------------------------------------
function RF:SkinButton(btn, style)
    style = style or "default"

    if not btn.bg then
        btn.bg = btn:CreateTexture(nil, "BACKGROUND")
        btn.bg:SetAllPoints()
        btn.bg:SetColorTexture(0, 0, 0, 1)
    end

    if style == "accent" then
        btn.bg:SetColorTexture(unpack(T.accent))
        btn:SetScript("OnEnter", function(self)
            self.bg:SetColorTexture(0.33, 0.61, 1.00, 1.00)
        end)
        btn:SetScript("OnLeave", function(self)
            self.bg:SetColorTexture(unpack(T.accent))
        end)
    elseif style == "warning" then
        btn.bg:SetColorTexture(0.80, 0.55, 0.10, 1.00)
        btn:SetScript("OnEnter", function(self)
            self.bg:SetColorTexture(0.90, 0.65, 0.15, 1.00)
        end)
        btn:SetScript("OnLeave", function(self)
            self.bg:SetColorTexture(0.80, 0.55, 0.10, 1.00)
        end)
    elseif style == "danger" then
        btn.bg:SetColorTexture(unpack(T.danger))
        btn:SetScript("OnEnter", function(self)
            self.bg:SetColorTexture(1.00, 0.35, 0.35, 1.00)
        end)
        btn:SetScript("OnLeave", function(self)
            self.bg:SetColorTexture(unpack(T.danger))
        end)
    else
        btn.bg:SetColorTexture(unpack(T.bgTertiary))
        btn:SetScript("OnEnter", function(self)
            self.bg:SetColorTexture(0.22, 0.22, 0.25, 1.00)
        end)
        btn:SetScript("OnLeave", function(self)
            self.bg:SetColorTexture(unpack(T.bgTertiary))
        end)
    end
end

-------------------------------------------------------------------------------
-- CreateButton: Helper to create a themed button
-------------------------------------------------------------------------------
function RF:CreateButton(parent, text, width, height, style)
    local btn = CreateFrame("Button", nil, parent)
    btn:SetSize(width or 80, height or 24)

    local label = btn:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    label:SetPoint("CENTER")
    label:SetFont(label:GetFont(), RF.FONT_SIZES.normal, "")
    label:SetText(text or "")
    label:SetTextColor(unpack(T.textPrimary))
    btn.label = label

    RF:SkinButton(btn, style)
    return btn
end

-------------------------------------------------------------------------------
-- CreateDivider: Horizontal line
-------------------------------------------------------------------------------
function RF:CreateDivider(parent, offsetY)
    local div = parent:CreateTexture(nil, "ARTWORK")
    div:SetHeight(1)
    div:SetPoint("LEFT", 0, 0)
    div:SetPoint("RIGHT", 0, 0)
    if offsetY then
        div:SetPoint("TOP", 0, offsetY)
    end
    div:SetColorTexture(unpack(T.border))
    return div
end

-------------------------------------------------------------------------------
-- CreateLabel: Themed font string
-------------------------------------------------------------------------------
function RF:CreateLabel(parent, text, size, colorKey)
    local fs = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    local fontPath = fs:GetFont()
    fs:SetFont(fontPath, size or RF.FONT_SIZES.normal, "")
    fs:SetText(text or "")
    local c = colorKey and T[colorKey] or T.textPrimary
    fs:SetTextColor(unpack(c))
    return fs
end
