-------------------------
-- AiB Co-Tank | Core
-- Namespace, Konstanten und Hilfsfunktionen
-- Wird als erstes im cotank-Ordner geladen
-------------------------

AiB.CoTank      = AiB.CoTank or {}
local ns        = {}
AiB.CoTank.ns   = ns

-- ── Konstanten (von allen Modulen über ns.K genutzt) ──────────────────────
ns.K = {
    BACKDROP_TEXTURE    = "Interface\\Buttons\\WHITE8X8",
    DEFAULT_FONT_FACE   = "Fonts\\ARIALN.TTF",
    UPDATE_THROTTLE     = 0.05,

    -- Frame-Größen
    DEFAULT_WIDTH       = 200,
    DEFAULT_HEIGHT      = 55,
    MIN_WIDTH           = 100,
    MAX_WIDTH           = 400,
    MIN_HEIGHT          = 30,
    MAX_HEIGHT          = 120,
    POWER_BAR_HEIGHT    = 5,
    HEALTH_BAR_INTENSITY = 0.7,

    -- Schriftgrößen
    DEFAULT_NAME_FONT_SIZE = 14,
    DEFAULT_HP_FONT_SIZE   = 12,
    MIN_FONT_SIZE          = 8,
    MAX_FONT_SIZE          = 28,

    -- Private Auras
    PRIVATE_AURA_CONTAINER_SIZE        = 30,
    PRIVATE_AURA_SIZE_DEFAULT          = 48,
    PRIVATE_AURA_SIZE_MIN              = 16,
    PRIVATE_AURA_SIZE_MAX              = 56,
    PRIVATE_AURA_BORDER_SCALE_DEFAULT  = 2,
    PRIVATE_AURA_BORDER_SCALE_MIN      = 0,
    PRIVATE_AURA_BORDER_SCALE_MAX      = 8,
    PRIVATE_AURA_STACK_SCALE_DEFAULT   = 2,  -- bei 48px passt x2 gut
    PRIVATE_AURA_STACK_SCALE_MIN       = 1,
    PRIVATE_AURA_STACK_SCALE_MAX       = 6,
    PRIVATE_AURA_GROW_DIRECTIONS = {
        {id="LEFT",  label="Left (default)"},
        {id="RIGHT", label="Right"},
        {id="UP",    label="Up"},
        {id="DOWN",  label="Down"},
    },
    PRIVATE_AURAS_DEFAULT_COUNT        = 4,
    PRIVATE_AURAS_MIN_COUNT            = 0,
    PRIVATE_AURAS_MAX_COUNT            = 10,
    PRIVATE_AURAS_DEFAULT_SCALE_FACTOR = 1.5,

    -- Debuffs
    DEBUFF_ICON_SIZE     = 26,
    DEBUFF_SPACING       = 4,
    DEBUFF_DEFAULT_COUNT = 5,
    DEBUFF_MIN_COUNT     = 0,
    DEBUFF_MAX_COUNT     = 10,
    DEBUFF_AURA_FILTER   = "HARMFUL",

    -- Defensives (0 = standardmäßig ausgeblendet)
    BIG_DEFENSIVES_DEFAULT_COUNT = 0,
    BIG_DEFENSIVES_MIN_COUNT     = 0,
    BIG_DEFENSIVES_MAX_COUNT     = 3,
    BIG_DEFENSIVE_AURA_FILTER    = "HELPFUL|BIG_DEFENSIVE",

    -- Icon-Abstand
    ICON_SPACING_MIN = 0,
    ICON_SPACING_MAX = 16,

    -- Stack / CD Label-Einstellungen
    STACK_COUNT_DEFAULT_SIZE = 9,
    STACK_COUNT_MIN_SIZE     = 0,
    STACK_COUNT_MAX_SIZE     = 18,
    CD_FONT_SCALE_DEFAULT    = 5,   -- gespeichert 0-10; /10 = tatsächliche Skalierung
    CD_FONT_SCALE_MIN        = 0,
    CD_FONT_SCALE_MAX        = 10,
    TIMER_TEXT_DEFAULT_SIZE  = 9,
    TIMER_TEXT_MIN_SIZE      = 6,
    TIMER_TEXT_MAX_SIZE      = 24,
    STACK_COUNT_POSITIONS    = {
        {id="BR_IN",  label="Inside: Bot-Right", point="BOTTOMRIGHT", px= 2, py= 1},
        {id="BL_IN",  label="Inside: Bot-Left",  point="BOTTOMLEFT",  px=-2, py= 1},
        {id="TR_IN",  label="Inside: Top-Right", point="TOPRIGHT",    px= 2, py=-2},
        {id="TL_IN",  label="Inside: Top-Left",  point="TOPLEFT",     px=-2, py=-2},
        {id="BR_OUT", label="Outside: Right",    point="BOTTOMRIGHT", px= 7, py=-2},
        {id="BL_OUT", label="Outside: Left",     point="BOTTOMLEFT",  px=-7, py=-2},
        {id="TOP_OUT", label="Above Icon",  point="BOTTOM", rel="TOP",    px= 0, py= 2},
        {id="BOT_OUT", label="Below Icon",  point="TOP",    rel="BOTTOM", px= 0, py=-2},
    },

    -- Mock-Vorschaudaten
    MOCK_PREVIEW_DEBUFF_ICONS = {
        132290, 132090, 135904, 136125, 132155,
        136207, 132104, 136147, 135974, 136015,
        132114, 136116, 135978,
    },
    MOCK_NAMES = {
        "Lord Doljonijiarnimorinar", "Lorem Ipsum Dolor",
        "M1 Abrams",                "Gøttlich",
        "TheArtistFormerlyKnownAsTank", "LineOfSighted",
    },
}

-- ── Addon-weite Objekte ────────────────────────────────────────────────────
ns.ADDON_COLOR = CreateColorFromHexString("ffff3333")  -- AiB Rot
ns.isTestMode  = false

-- ── Datenbank-Zugriff ─────────────────────────────────────────────────────
function ns.DB()
    if AiB.db and AiB.db.profile then
        AiB.db.profile.coTank = AiB.db.profile.coTank or {}
        return AiB.db.profile.coTank
    end
    -- Fallback während des frühen Ladens (sollte nach PLAYER_LOGIN nicht mehr vorkommen)
    _AIB_CoTankFallback = _AIB_CoTankFallback or {}
    return _AIB_CoTankFallback
end

function ns.SettingNumber(key, default)
    local val = ns.DB()[key]
    return tonumber(val) or default
end

-- ── Hilfsfunktionen ───────────────────────────────────────────────────────
function ns.IsInEditMode()
    if C_EditMode and type(C_EditMode.IsEditModeActive) == "function" then
        return C_EditMode.IsEditModeActive()
    end
    return false
end

function ns.CalculateScaleFactor()
    return ns.DB().scaleFactorPrivateAuras or ns.K.PRIVATE_AURAS_DEFAULT_SCALE_FACTOR
end

function ns.FindCoTank()
    if ns.isTestMode then return "player" end
    local groupType = IsInRaid() and "raid" or (IsInGroup() and "party")
    if not groupType then return nil end
    local count = GetNumGroupMembers()
    -- Falls ein Tank gepinnt ist, diesen bevorzugen
    local pinned = ns.DB().pinnedTank
    if pinned then
        for i = 1, count do
            local unit = groupType .. i
            if UnitExists(unit) and UnitName(unit) == pinned then return unit end
        end
        -- Gepinnter Spieler nicht in der Gruppe → Fallback auf Auto-Erkennung
    end
    for i = 1, count do
        local unit = groupType .. i
        if UnitExists(unit) and not UnitIsUnit(unit, "player") then
            if UnitGroupRolesAssigned(unit) == "TANK" then return unit end
        end
    end
    return nil
end

function ns.Print(msg)
    AiB.Config:Log("CoTank", tostring(msg))
end

-- ── Positionierung ────────────────────────────────────────────────────────
function ns.SavePosition(frame)
    local point, _, relPoint, x, y = frame:GetPoint()
    local db = ns.DB()
    db.point = point; db.relPoint = relPoint
    db.x     = x;     db.y        = y
end

function ns.ApplyPosition(frame)
    local db = ns.DB()
    frame:ClearAllPoints()
    frame:SetPoint(db.point or "CENTER", UIParent, db.relPoint or "CENTER",
                   db.x or 0, db.y or 0)
end

function ns.ResetPosition(frame)
    local db = ns.DB()
    db.point = "CENTER"; db.relPoint = "CENTER"; db.x = 0; db.y = 0
    ns.ApplyPosition(frame)
end
