local _, private = ...

-- Libs --
local oUF = private.oUF

-- RealUI --
local RealUI = private.RealUI
local UnitFrames = RealUI:GetModule("UnitFrames")
local FramePoint = RealUI:GetModule("FramePoint")

UnitFrames.boss = {
    create = function(dialog)

        dialog.Health.text:SetPoint("LEFT", dialog.Health, 1, 0)
        dialog.Power.displayAltPower = true
        -- B56: optionally hide the power strip unless the encounter supplies
        -- alternative power (oUF 14.0.1 displayAltPowerOnly). Off by default —
        -- always-visible power keeps boss mana readable on drain/interrupt
        -- fights. oUF re-Shows the element on the next update when cleared.
        local bossDB = UnitFrames.db.profile.boss
        dialog.Power.displayAltPowerOnly = (bossDB and bossDB.altPowerOnly) or nil

        dialog.Name = dialog.Health:CreateFontString(nil, "OVERLAY")
        dialog.Name:SetPoint("RIGHT", dialog.Health, -1, 0)
        dialog.Name:SetFontObject("SystemFont_Shadow_Med1")
        dialog.Name:SetJustifyH("RIGHT")
        dialog:Tag(dialog.Name, "[realui:name]")

        dialog.RaidTargetIndicator = dialog:CreateTexture(nil, "OVERLAY")
        dialog.RaidTargetIndicator:SetSize(20, 20)
        dialog.RaidTargetIndicator:SetPoint("CENTER", dialog)

        -- Boss Debuffs (oUF 14: AuraContainer elements; boss anchors are
        -- fixed, so positioning stays a direct SetPoint here)
        -- B34: boss frames sit at the right screen edge, so debuffs hang off
        -- the frame's LEFT edge and grow LEFT (toward screen center). The
        -- container auto-sizes to content with its RIGHT edge pinned, and
        -- wrapped rows stay vertically centered on the frame.
        local db = UnitFrames.db.profile

        local Debuffs = UnitFrames.CreateAuraElement(dialog, {
            filter = "HARMFUL",
            count = (db.boss and db.boss.debuffCount) or 16,
            size = (db.boss and db.boss.debuffSize) or 20,
            spacing = 2,
            growthX = "LEFT",
            growthY = "UP",
        })
        Debuffs:SetPoint("RIGHT", dialog, "LEFT", -4, 0)
        dialog.Debuffs = Debuffs

        -- Boss Buffs
        local Buffs = UnitFrames.CreateAuraElement(dialog, {
            filter = "HELPFUL",
            count = (db.boss and db.boss.buffCount) or 16,
            size = (db.boss and db.boss.buffSize) or 20,
            spacing = 2,
            growthX = "RIGHT",
            growthY = "DOWN",
        })
        Buffs:SetPoint("TOPLEFT", dialog, "BOTTOMLEFT", 0, -2)
        dialog.Buffs = Buffs
    end,
    health = {
        text = true,
    },
    power = {
    },
}

-- Init
_G.tinsert(UnitFrames.units, function()
    local db = UnitFrames.db.profile

    for i = 1, 5 do
        local boss = oUF:Spawn("boss" .. i, "RealUIBossFrame" .. i)
        if i == 1 then
            boss:SetPoint("RIGHT", "RealUIPositionersBossFrames", "LEFT", db.positions[UnitFrames.layoutSize].boss.x, db.positions[UnitFrames.layoutSize].boss.y)
            FramePoint:PositionFrame(UnitFrames, boss, {"profile", "units", "boss", "framePoint"})
        else
            boss:SetPoint("TOP", _G["RealUIBossFrame" .. i - 1], "BOTTOM", 0, -db.boss.gap)
        end
    end
end)
