-------------------------
-- AiB Don't Release
-------------------------

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

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

local DR = AiB.DontRelease
DR.keyPressed = nil
DR.lastPopup = nil

DR.INFO_TEXT = "Halte %s für %.1f Sekunden zum Releasen."
DR.INFO_TEXT_SAFE = "Halte die Taste und klicke den Button."

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

function DR:IsDifficultyEnabled()
    local _, _, diffID = GetInstanceInfo()
    if not diffID then return false end
    if not AiB_DontReleaseConfig.difficulties then
        AiB_DontReleaseConfig.difficulties = {
            [8] = true, [14] = true, [15] = true, [16] = true, [17] = true, [23] = true,
        }
    end
    return AiB_DontReleaseConfig.difficulties[diffID] or false
end

function DR:GetPopup()
    local _, popup = StaticPopup_Visible("DEATH")
    return popup
end

function DR:UpdateModifierStatus()
    local isModDown, modName = self:GetModifierKey()
    local timeout = AiB_DontReleaseConfig.timeout
    
    if timeout <= 0 then
        return 0, 0, modName
    end
    
    if isModDown() then
        if not self.keyPressed then
            self.keyPressed = GetTime() + timeout
        end
    else
        self.keyPressed = nil
    end
    
    if self.keyPressed then
        local remain = self.keyPressed - GetTime()
        return self.keyPressed, remain, modName
    end
    
    return nil, nil, modName
end

function DR:UpdateButton()
    if UnitIsGhost("player") or not UnitIsDead("player") then
        self.lastPopup = nil
        return
    end
    
    local popup = self:GetPopup()
    if not popup then
        self.lastPopup = nil
        return
    end
    
    local button = popup.button1 or popup:GetButton1()
    if not button then return end
    
    local blockingEnabled = self:IsDifficultyEnabled()
    local releaseAllowed = not blockingEnabled
    
    if blockingEnabled then
        local _, remain = self:UpdateModifierStatus()
        if remain and remain <= 0 then
            releaseAllowed = true
        end
    end
    
    if AiB_DontReleaseConfig.autoRelease and releaseAllowed then
        RepopMe()
        return
    end
    
    button:SetShown(releaseAllowed)
    
    if blockingEnabled and popup ~= self.lastPopup then
        self.lastPopup = popup
        
        -- Größeres Popup - ERZWINGE Breite mit Backdrop
        if not popup.DR_Backdrop then
            popup.DR_Backdrop = popup:CreateTexture(nil, "BACKGROUND")
            popup.DR_Backdrop:SetColorTexture(0, 0, 0, 0)
            popup.DR_Backdrop:SetPoint("TOPLEFT", popup, "TOPLEFT", 0, 0)
            popup.DR_Backdrop:SetPoint("BOTTOMRIGHT", popup, "BOTTOMRIGHT", 300, 0) -- 300px extra rechts
        end
        
        popup:SetWidth(550)
        popup:SetHeight(releaseAllowed and 180 or 140)
        
        local textFS = popup.text or popup.Text
        if textFS and not popup.DR_Hooked then
            popup.DR_Hooked = true
            popup.DR_BaseText = textFS:GetText()
            popup.DR_OriginalSetText = textFS.SetText
            
            textFS.SetText = function(self, text)
                if UnitIsGhost("player") then
                    popup.DR_OriginalSetText(self, text)
                    return
                end
                
                if text and (text:match("until release") or text:match("bis")) then
                    popup.DR_BaseText = text
                end
                
                local _, remain, modName = DR:UpdateModifierStatus()
                local fullText = popup.DR_BaseText or text or ""
                
                if remain and remain > 0 then
                    fullText = fullText .. "\n" .. string.format(DR.INFO_TEXT, modName, remain)
                elseif remain then
                    fullText = fullText .. "\n" .. DR.INFO_TEXT_SAFE
                else
                    fullText = fullText .. "\n" .. string.format(DR.INFO_TEXT, modName, AiB_DontReleaseConfig.timeout)
                end
                
                popup.DR_OriginalSetText(self, fullText)
            end
            
            textFS:SetText(popup.DR_BaseText)
        end
    end
end

local updateFrame = CreateFrame("Frame")
updateFrame:SetScript("OnUpdate", function(self, elapsed)
    self.elapsed = (self.elapsed or 0) + elapsed
    if self.elapsed >= 0.2 then
        self.elapsed = 0
        if AiB_DontReleaseConfig.enabled then
            DR:UpdateModifierStatus()
            DR:UpdateButton()
        end
    end
end)

