-- =========================================================================
-- FILE: UI_MiniMode.lua (TBC)
-- PURPOSE: Compact mini frame for recruitment tracking
-- =========================================================================
local addonName, NS = ...
local GR = NS.GR
local CreateBg = NS.CreateBg
local CreateBorder = NS.CreateBorder

-- =========================================================================
-- UI: MINI MODE LOGIC (COMPACT 125x75)
-- =========================================================================

function GR:BuildMiniFrame()
    if self.MiniFrame then return end
    
    local f = CreateFrame("Frame", "GRS_MiniFrame", UIParent)
    f:SetSize(155, 45)
    f:SetPoint("CENTER")
    f:SetFrameStrata("DIALOG")
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)
    f:Hide()
    
    CreateBg(f, 0, 0, 0, 1)
    CreateBorder(f, 0, 0, 0, 1)
    
    -- CONTROLS
    local btnClose = CreateFrame("Button", nil, f)
    btnClose:SetSize(12, 12); btnClose:SetPoint("TOPRIGHT", -2, -2)
    btnClose:SetNormalFontObject("GameFontNormalSmall"); btnClose:SetText("X")
    btnClose:SetScript("OnClick", function() GR:Hide() end)
    
    local btnMax = CreateFrame("Button", nil, f)
    btnMax:SetSize(12, 12); btnMax:SetPoint("RIGHT", btnClose, "LEFT", -2, 0)
    btnMax:SetNormalFontObject("GameFontNormalSmall"); btnMax:SetText("+")
    btnMax:SetScript("OnClick", function() GR:ToggleMiniMode(false) end)
    
    -- SINGLE LINE TEXT LAYOUT
    local container = CreateFrame("Frame", nil, f)
    container:SetPoint("LEFT", 5, 0)
    container:SetPoint("RIGHT", -5, 0)
    container:SetHeight(20)
    container:SetPoint("CENTER")
    
    -- AXIOM
    local t1 = container:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    t1:SetPoint("LEFT", 0, 0)
    t1:SetText("|cff66CCFFA.X.I.O.M.|r")
    
    -- SEPARATOR
    local t2 = container:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    t2:SetPoint("LEFT", t1, "RIGHT", 2, 0)
    t2:SetText("|cffffd100|||r")
    
    -- ACTIVE LABEL (Dynamic Color)
    local t3 = container:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    t3:SetPoint("LEFT", t2, "RIGHT", 2, 0)
    t3:SetText("Active:")
    f.ActiveLabel = t3
    
    -- COUNT
    local t4 = container:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    t4:SetPoint("LEFT", t3, "RIGHT", 2, 0)
    t4:SetText("0/0")
    f.CountLabel = t4
    
    -- ANIMATION LOGIC (Green <-> Gold Fade over 3s)
    local totalTime = 0
    f:SetScript("OnUpdate", function(self, elapsed)
        totalTime = totalTime + elapsed
        local phase = totalTime % 6
        local r, g, b
        
        if phase < 3 then
            local p = phase / 3
            r = 0 + (1 - 0) * p
            g = 1 + (0.82 - 1) * p
            b = 0
        else
            local p = (phase - 3) / 3
            r = 1 + (0 - 1) * p
            g = 0.82 + (1 - 0.82) * p
            b = 0
        end
        
        if f.ActiveLabel then f.ActiveLabel:SetTextColor(r, g, b) end
    end)
    
    self.MiniFrame = f
end

function GR:ToggleMiniMode(enabled)
    if enabled then
        self:Hide()
        if not self.MiniFrame then self:BuildMiniFrame() end
        self.MiniFrame:Show()
        self:UpdateMiniStatus()
    else
        if self.MiniFrame then self.MiniFrame:Hide() end
        self:Show()
    end
end

function GR:UpdateMiniStatus()
    if not self.MiniFrame then return end
    local sent = self.batchSent or 0
    local total = self.batchTotal or 0
    self.MiniFrame.CountLabel:SetText(string.format("%d/%d", sent, total))
end
