-- DeathSound - ElvUI_Skin.lua
-- Optionaler ElvUI-Skin. Greift nur wenn ElvUI aktiv ist.

if not ElvUI then return end

local ADDON_NAME = "DeathSound"
local E = unpack(ElvUI)
local S = E:GetModule("Skins")

local function ApplySkin()
    local panel = DeathSound_OptionsPanel
    if not panel then return end

    -- Panel-Hintergrund
    panel:SetTemplate("Transparent")

    -- Tab-Buttons
    local tabSound = _G["DeathSoundTabSound"]
    if tabSound then S:HandleButton(tabSound) end

    local tabImage = _G["DeathSoundTabImage"]
    if tabImage then S:HandleButton(tabImage) end

    local tabGroup = _G["DeathSoundTabGroup"]
    if tabGroup then S:HandleButton(tabGroup) end

    -- Checkboxen
    local cb = _G["DeathSoundEnabledCB"]
    if cb then S:HandleCheckBox(cb) end

    local fileCB = _G["DeathSoundFileCB"]
    if fileCB then S:HandleCheckBox(fileCB) end

    local randomCB = _G["DeathSoundRandomCB"]
    if randomCB then S:HandleCheckBox(randomCB) end

    local idCB = _G["DeathSoundIDCB"]
    if idCB then S:HandleCheckBox(idCB) end

    -- Dropdown
    local dd = _G["DeathSoundDropdown"]
    if dd then
        S:HandleDropDownBox(dd)
        UIDropDownMenu_SetWidth(dd, 160)

        -- ElvUI hat ~2px internes Padding statt Blizzards ~15px.
        -- Frame 13px nach rechts verschieben damit der sichtbare
        -- Rand buendig mit den Texten darueber und darunter ist.
        local point, relativeTo, relativePoint, x, y = dd:GetPoint(1)
        dd:ClearAllPoints()
        dd:SetPoint(point, relativeTo, relativePoint, x + 13, y)

        -- Text-FontString nach ElvUI-Skinning neu verankern
        if dd.Text then
            dd.Text:ClearAllPoints()
            dd.Text:SetPoint("LEFT",  dd, "LEFT",  8, 2)
            dd.Text:SetPoint("RIGHT", dd, "RIGHT", -30, 2)
            dd.Text:SetJustifyH("LEFT")
        end

        -- Overlay neu verankern
        local overlay = _G["DeathSoundDropdownOverlay"]
        if overlay then
            overlay:ClearAllPoints()
            overlay:SetPoint("TOPLEFT",     dd, "TOPLEFT",     2, -2)
            overlay:SetPoint("BOTTOMRIGHT", dd, "BOTTOMRIGHT", -2, 2)
            overlay:SetFrameLevel(dd:GetFrameLevel() + 2)
        end
    end

    -- Test-Buttons
    local testBtn = _G["DeathSoundTestButton"]
    if testBtn then S:HandleButton(testBtn) end

    local testIDBtn = _G["DeathSoundTestIDButton"]
    if testIDBtn then S:HandleButton(testIDBtn) end

    -- EditBox Sound ID
    local idBox = _G["DeathSoundIDBox"]
    if idBox then S:HandleEditBox(idBox) end

    -- Overlay Sektion
    local overlayCB = _G["DeathSoundOverlayCB"]
    if overlayCB then S:HandleCheckBox(overlayCB) end

    local popupCB = _G["DeathSoundPopupCB"]
    if popupCB then S:HandleCheckBox(popupCB) end

    local randomEmoteCB = _G["DeathSoundRandomEmoteCB"]
    if randomEmoteCB then S:HandleCheckBox(randomEmoteCB) end

    local testEmoteBtn = _G["DeathSoundTestEmoteButton"]
    if testEmoteBtn then S:HandleButton(testEmoteBtn) end

    local imageDD = _G["DeathSoundImageDropdown"]
    if imageDD then
        S:HandleDropDownBox(imageDD)
        UIDropDownMenu_SetWidth(imageDD, 160)

        -- Gleiche X-Korrektur wie beim Sound-Dropdown
        local point, relativeTo, relativePoint, x, y = imageDD:GetPoint(1)
        imageDD:ClearAllPoints()
        imageDD:SetPoint(point, relativeTo, relativePoint, x + 13, y)

        -- Text-FontString nach ElvUI-Skinning neu verankern
        if imageDD.Text then
            imageDD.Text:ClearAllPoints()
            imageDD.Text:SetPoint("LEFT",  imageDD, "LEFT",  8, 2)
            imageDD.Text:SetPoint("RIGHT", imageDD, "RIGHT", -30, 2)
            imageDD.Text:SetJustifyH("LEFT")
        end

        local imgOverlay = _G["DeathSoundImageDropdownOverlay"]
        if imgOverlay then
            imgOverlay:ClearAllPoints()
            imgOverlay:SetPoint("TOPLEFT",     imageDD, "TOPLEFT",     2, -2)
            imgOverlay:SetPoint("BOTTOMRIGHT", imageDD, "BOTTOMRIGHT", -2, 2)
            imgOverlay:SetFrameLevel(imageDD:GetFrameLevel() + 2)
        end
    end

    for _, size in ipairs({32, 64, 96, 128}) do
        local btn = _G["DeathSoundSize" .. size]
        if btn then S:HandleButton(btn) end
    end

    local sizeBox = _G["DeathSoundSizeBox"]
    if sizeBox then S:HandleEditBox(sizeBox) end

    local lockBtn = _G["DeathSoundLockButton"]
    if lockBtn then S:HandleButton(lockBtn) end

    local resetBtn = _G["DeathSoundResetButton"]
    if resetBtn then S:HandleButton(resetBtn) end

    -- Party Overlay Sektion
    local partyCB = _G["DeathSoundPartyCB"]
    if partyCB then S:HandleCheckBox(partyCB) end

    local partyDD = _G["DeathSoundPartyDropdown"]
    if partyDD then
        S:HandleDropDownBox(partyDD)
        UIDropDownMenu_SetWidth(partyDD, 160)

        local point, relativeTo, relativePoint, x, y = partyDD:GetPoint(1)
        partyDD:ClearAllPoints()
        partyDD:SetPoint(point, relativeTo, relativePoint, x + 13, y)

        if partyDD.Text then
            partyDD.Text:ClearAllPoints()
            partyDD.Text:SetPoint("LEFT",  partyDD, "LEFT",  8, 2)
            partyDD.Text:SetPoint("RIGHT", partyDD, "RIGHT", -30, 2)
            partyDD.Text:SetJustifyH("LEFT")
        end

        local partyDDOverlay = _G["DeathSoundPartyDropdownOverlay"]
        if partyDDOverlay then
            partyDDOverlay:ClearAllPoints()
            partyDDOverlay:SetPoint("TOPLEFT",     partyDD, "TOPLEFT",     2, -2)
            partyDDOverlay:SetPoint("BOTTOMRIGHT", partyDD, "BOTTOMRIGHT", -2, 2)
            partyDDOverlay:SetFrameLevel(partyDD:GetFrameLevel() + 2)
        end
    end

    local anchorDD = _G["DeathSoundPartyAnchorDropdown"]
    if anchorDD then
        S:HandleDropDownBox(anchorDD)
        UIDropDownMenu_SetWidth(anchorDD, 100)

        local point, relativeTo, relativePoint, x, y = anchorDD:GetPoint(1)
        anchorDD:ClearAllPoints()
        anchorDD:SetPoint(point, relativeTo, relativePoint, x + 13, y)

        if anchorDD.Text then
            anchorDD.Text:ClearAllPoints()
            anchorDD.Text:SetPoint("LEFT",  anchorDD, "LEFT",  8, 2)
            anchorDD.Text:SetPoint("RIGHT", anchorDD, "RIGHT", -30, 2)
            anchorDD.Text:SetJustifyH("LEFT")
        end

        local anchorDDOverlay = _G["DeathSoundPartyAnchorOverlay"]
        if anchorDDOverlay then
            anchorDDOverlay:ClearAllPoints()
            anchorDDOverlay:SetPoint("TOPLEFT",     anchorDD, "TOPLEFT",     2, -2)
            anchorDDOverlay:SetPoint("BOTTOMRIGHT", anchorDD, "BOTTOMRIGHT", -2, 2)
            anchorDDOverlay:SetFrameLevel(anchorDD:GetFrameLevel() + 2)
        end
    end

    local partyTestBtn = _G["DeathSoundPartyTestButton"]
    if partyTestBtn then S:HandleButton(partyTestBtn) end

    local partyRandomCB = _G["DeathSoundPartyRandomCB"]
    if partyRandomCB then S:HandleCheckBox(partyRandomCB) end

    local partyOffsetBox = _G["DeathSoundPartyOffsetBox"]
    if partyOffsetBox then S:HandleEditBox(partyOffsetBox) end
end

local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("OnEvent", function(self, event, arg1)
    if arg1 ~= ADDON_NAME then return end
    C_Timer.After(0, function()
        if E.private.skins and E.private.skins.blizzard and E.private.skins.blizzard.enable then
            ApplySkin()
        end
    end)
    self:UnregisterEvent("ADDON_LOADED")
end)
