-------------------------
-- AiB Don't Release
-- v2.1.0.0 – Blocker-Frame Ansatz
-------------------------

AiB = AiB or {}
AiB.DontRelease = {}

AiB_DontReleaseConfig = AiB_DontReleaseConfig or {
    enabled = true, modKey = 2, timeout = 1.0, autoRelease = false,
    difficulties = { [0]=true,[8]=true,[14]=true,[15]=true,[16]=true,[17]=true,[23]=true },
}

local DR = AiB.DontRelease

local blocker    = nil
local timerLabel = nil
local pressStart = 0
local isReady    = false

local function GetModifierKey()
    local modKey = AiB_DontReleaseConfig.modKey
    if modKey == 1 then return IsShiftKeyDown, "SHIFT" end
    if modKey == 3 then return IsAltKeyDown,   "ALT"   end
    return IsControlKeyDown, "STRG"
end

-- Einmalige Migration: neue Difficulty-Keys in bestehenden Profilen setzen
local DIFFICULTY_DEFAULTS = {
    [0]=true, [1]=false, [2]=false, [8]=true,
    [14]=true, [15]=true, [16]=true, [17]=true, [23]=true,
}

local function MigrateDifficulties()
    if not AiB_DontReleaseConfig then return end
    AiB_DontReleaseConfig.difficulties = AiB_DontReleaseConfig.difficulties or {}
    for id, default in pairs(DIFFICULTY_DEFAULTS) do
        if AiB_DontReleaseConfig.difficulties[id] == nil then
            AiB_DontReleaseConfig.difficulties[id] = default
        end
    end
end

local function IsDifficultyEnabled()
    local _, _, diffID = GetInstanceInfo()
    if diffID == nil then return false end
    MigrateDifficulties()
    local diffs = AiB_DontReleaseConfig.difficulties
    local val = diffs[diffID]
    if val == nil then val = diffs[tostring(diffID)] end
    if val == nil then val = DIFFICULTY_DEFAULTS[diffID] or false end
    return val
end

local function GetReleaseButton()
    local _, popup = StaticPopup_Visible("DEATH")
    if not popup then return nil end
    -- Alle möglichen Button-Accessor-Methoden probieren
    local btn = nil
    if popup.GetButton then
        btn = popup:GetButton(1)
    end
    if not btn and popup.Buttons then
        btn = popup.Buttons[1]
    end
    if not btn then
        btn = popup.button1
    end
    return btn
end

local function ClearState()
    pressStart = 0
    isReady    = false
    if blocker then
        blocker:SetScript("OnUpdate", nil)
        blocker:Hide()
    end
end

local function BuildBlocker()
    if blocker then return blocker end

    blocker = CreateFrame("Button", "AiB_DontReleaseBlocker", UIParent)
    blocker:SetFrameStrata("FULLSCREEN_DIALOG")
    blocker:EnableMouse(true)
    blocker:RegisterForClicks("AnyUp", "AnyDown")

    local bg = blocker:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0.08, 0.08, 0.08, 0.90)

    timerLabel = blocker:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    timerLabel:SetPoint("LEFT", blocker, "LEFT", 4, 0)
    timerLabel:SetPoint("RIGHT", blocker, "RIGHT", -4, 0)
    timerLabel:SetJustifyH("CENTER")
    timerLabel:SetNonSpaceWrap(false)

    blocker:SetScript("OnClick", function() end)
    return blocker
end

local function PositionBlockerOnButton(btn)
    BuildBlocker()
    blocker:ClearAllPoints()
    blocker:SetPoint("TOPLEFT",     btn, "TOPLEFT")
    blocker:SetPoint("BOTTOMRIGHT", btn, "BOTTOMRIGHT")
    -- Frame Level explizit über den Button setzen damit Klicks abgefangen werden
    blocker:SetFrameLevel(btn:GetFrameLevel() + 10)
end

local function TickTimer(self, elapsed)
    if not AiB_DontReleaseConfig.enabled then ClearState() return end
    if UnitIsGhost("player") or not UnitIsDead("player") then ClearState() return end
    if isReady then self:SetScript("OnUpdate", nil) self:Hide() return end

    local isModDown, modName = GetModifierKey()
    local timeout = AiB_DontReleaseConfig.timeout or 1.0

    if isModDown() then
        if pressStart == 0 then pressStart = GetTime() end
        local left = timeout - (GetTime() - pressStart)
        if left <= 0 then
            isReady = true
            self:SetScript("OnUpdate", nil)
            self:Hide()
            if AiB_DontReleaseConfig.autoRelease then RepopMe() end
        else
            timerLabel:SetText(string.format("|cFFFF8800%.1fs|r  |cFFFFD700%s|r", left, modName))
        end
    else
        pressStart = 0
        timerLabel:SetText(string.format("|cFFFFD700%s|r  %.1fs", modName, timeout))
    end
end

local function ActivateProtection()

    if not AiB_DontReleaseConfig.enabled then
        return
    end

    if not IsDifficultyEnabled() then
        return
    end

    local btn = GetReleaseButton()
    if not btn then
        C_Timer.After(0.3, ActivateProtection)
        return
    end

    PositionBlockerOnButton(btn)
    pressStart = 0
    isReady    = false

    local _, modName = GetModifierKey()
    local timeout = AiB_DontReleaseConfig.timeout or 1.0
    timerLabel:SetText(string.format("|cFFFFD700%s|r  %.1fs", modName, timeout))

    blocker:Show()
    blocker:SetScript("OnUpdate", TickTimer)
end

local watcher = CreateFrame("Frame")
watcher:RegisterEvent("PLAYER_DEAD")
watcher:RegisterEvent("PLAYER_ALIVE")
watcher:RegisterEvent("PLAYER_UNGHOST")

watcher:SetScript("OnEvent", function(self, event)
    if event == "PLAYER_DEAD" then
        C_Timer.After(0.1, ActivateProtection)
    else
        ClearState()
    end
end)

function AiB.DontRelease:GetModifierKey()      return GetModifierKey()      end
function AiB.DontRelease:IsDifficultyEnabled() return IsDifficultyEnabled() end
