-- UI/WarBoard_ShardPill.lua
-- Shard readout pinned to the War Board frame's top-right corner (next to the close
-- button). Faction crest + "SHARD: N" (from WB.ShardDetector); the pill grows to fit.

local addonName, WB = ...

WB.ShardPill = WB.ShardPill or {}
local SP = WB.ShardPill

-- Faction crest atlases (from interface/glues/charactercreate/charactercreatetest),
-- with the addon's emblem atlases as a fallback if the test atlas isn't present.
local FACTION_LOGO = {
    Horde    = "charcreatetest-logo-horde",
    Alliance = "charcreatetest-logo-alliance",
}
local FALLBACK_LOGO = {
    Horde    = "HordeSymbol",
    Alliance = "AllianceSymbol",
}

local PILL_H   = 32   -- pill height
local ICON_H   = 22   -- crest height (width derived from its aspect ratio)
local PAD_L    = 9    -- gap before the crest
local ICON_GAP = 7    -- gap between crest and text
local PAD_R    = 14   -- gap after the text

local pill, iconTex, label

local function MyFaction()
    if WB.faction then return WB.faction end
    return (UnitFactionGroup("player") == "Alliance") and "Alliance" or "Horde"
end

local function ResolveLogo(faction)
    local name = FACTION_LOGO[faction]
    if name and C_Texture and C_Texture.GetAtlasInfo and C_Texture.GetAtlasInfo(name) then
        return name
    end
    return FALLBACK_LOGO[faction] or "HordeSymbol"
end

local function ApplyLogo(faction)
    local atlas = ResolveLogo(faction)
    iconTex:SetAtlas(atlas)
    local info = C_Texture.GetAtlasInfo(atlas)
    local aspect = (info and info.width and info.height and info.height > 0) and (info.width / info.height) or 1
    -- Keep the crest from getting absurdly wide if the atlas is a wordmark.
    aspect = math.min(aspect, 1.4)
    iconTex:SetSize(ICON_H * aspect, ICON_H)
end

-- Grow/shrink the pill to fit the current text (anchored top-right, so it extends left).
local function UpdateWidth()
    if not pill then return end
    local textW = (label:GetStringWidth() or 0)
    local iconW = iconTex:GetWidth() or ICON_H
    pill:SetWidth(PAD_L + iconW + ICON_GAP + textW + PAD_R)
end

-- Set the displayed shard. nil/empty shows the unresolved placeholder.
function SP.SetShard(shardID)
    if not label then return end
    if shardID ~= nil and tostring(shardID) ~= "" then
        label:SetText("SHARD: " .. tostring(shardID))
    else
        label:SetText("SHARD: --")
    end
    UpdateWidth()
end

-- Build the pill as a child of the War Board frame, pinned to its top-right corner.
function SP.Create(parent)
    if pill then return end
    parent = parent or WB.UI and WB.UI.frame
    if not parent then return end
    local faction = MyFaction()

    pill = CreateFrame("Frame", "WarBoardShardPill", parent, "BackdropTemplate")
    pill:SetHeight(PILL_H)
    pill:SetWidth(120)
    -- Centered over the War Effort Progress block (so it stays "even" with it) and
    -- pinned by its bottom, just above the block. The center anchor means the pill
    -- grows symmetrically outward from the middle as the shard number populates.
    local block = WB.UI and WB.UI.warEffortPanel
    if block then
        pill:SetPoint("BOTTOM", block, "TOP", 0, 3)
    else
        pill:SetPoint("TOPRIGHT", parent, "TOPRIGHT", -64, -35)
    end

    pill:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 14,
        insets   = { left = 4, right = 4, top = 4, bottom = 4 },
    })
    pill:SetBackdropColor(0.03, 0.03, 0.04, 0.92)
    pill:SetBackdropBorderColor(0.82, 0.66, 0.32, 0.95)  -- thin gold frame

    iconTex = pill:CreateTexture(nil, "ARTWORK")
    iconTex:SetPoint("LEFT", pill, "LEFT", PAD_L, 0)
    ApplyLogo(faction)

    label = pill:CreateFontString(nil, "OVERLAY")
    label:SetPoint("LEFT", iconTex, "RIGHT", ICON_GAP, 0)
    label:SetFont(STANDARD_TEXT_FONT, 12, "")
    label:SetTextColor(0.96, 0.92, 0.80)

    -- Seed from whatever the detector already resolved.
    local cur = WB.ShardDetector and WB.ShardDetector.GetShardID and WB.ShardDetector:GetShardID()
    SP.SetShard(cur)

    -- Honour saved visibility.
    if WarBoardCharDB and WarBoardCharDB.shardPill and WarBoardCharDB.shardPill.hidden then
        pill:Hide()
    end
end

-- Show / hide the pill (persisted).
function SP.Toggle()
    if not pill then return end
    WarBoardCharDB = WarBoardCharDB or {}
    WarBoardCharDB.shardPill = WarBoardCharDB.shardPill or {}
    if pill:IsShown() then
        pill:Hide()
        WarBoardCharDB.shardPill.hidden = true
    else
        pill:Show()
        WarBoardCharDB.shardPill.hidden = false
    end
end

function SP.IsShown()
    return pill and pill:IsShown()
end
