local LSM = LibStub and LibStub('LibSharedMedia-3.0', true)
if not LSM then return end

local PACK_NAME = 'EXBOSS German TTS Pack'
local sounds = {
    ['转阶段'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\phase_transition.mp3',
    ['坦克击退'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\tank_knockback.mp3',
    ['集合分摊'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\soak.mp3',
    ['强化防骑'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\emp_prot.mp3',
    ['特殊技能'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\special.mp3',
    ['准备AOE'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\incoming_aoe.mp3',
    ['快踩陷阱'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\trigger_trap.mp3',
    ['54321'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown.mp3',
    ['你是黑色'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\you_black.mp3',
    ['准备钩人'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\hook.mp3',
    ['靠近分散'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\close_spread.mp3',
    ['点名放水'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\drop_pool.mp3',
    ['拉断连线'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\break_tether.mp3',
    ['注意点名'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\targeted.mp3',
    ['远离大怪'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\away_big_add.mp3',
    ['注意挡球'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\intercept_orbs.mp3',
    ['准备驱散'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\dispel.mp3',
    ['强化奶骑'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\emp_holy.mp3',
    ['Boss狂暴'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\enrage.mp3',
    ['准备AOE(断条)'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\aoe_interrupt.mp3',
    ['转火大怪'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\focus_big_add.mp3',
    ['快找光柱'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\light_pillar.mp3',
    ['准备拉人'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\pull.mp3',
    ['控断钢条'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\cc_interrupt.mp3',
    ['准备打断'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\ready_interrupt.mp3',
    ['注意击退'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\watch_knockback.mp3',
    ['快卡视角'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\los.mp3',
    ['转火小怪'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\focus_adds.mp3',
    ['易伤爆发'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\burst.mp3',
    ['注意减伤'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\defensives.mp3',
    ['去撞分身'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\illusions.mp3',
    ['注意头前'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\watch_frontal.mp3',
    ['小心击飞'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\knockup.mp3',
    ['躲开头前'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\dodge_frontal.mp3',
    ['小心冲击波'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\shockwave.mp3',
    ['点名消线'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\clear_line.mp3',
    ['注意躲圈'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\dodge_puddles.mp3',
    ['快分散'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\spread_quickly.mp3',
    ['去接小怪'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\pickup_adds.mp3',
    ['准备踩箭'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\intercept_arrows.mp3',
    ['准备连线'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\watch_tether.mp3',
    ['准备挡线'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\intercept_beam.mp3',
    ['坦克尖刺'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\tank_hit.mp3',
    ['准备追人'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\fixate.mp3',
    ['注意击飞'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\watch_knockup_2.mp3',
    ['准备吸球'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\soak_orbs.mp3',
    ['准备点名'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\incoming_target.mp3',
    ['Boss易伤'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\boss_vulnerable.mp3',
    ['快救人'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\save_quickly.mp3',
    ['远离BOSS'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\away_boss.mp3',
    ['射线点你'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\beam_on_you.mp3',
    ['快打断'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\interrupt_quickly.mp3',
    ['准备引线'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\bait_line.mp3',
    ['准备集合'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\ready_stack.mp3',
    ['快进罩子'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\dome.mp3',
    ['注意治疗'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\healing.mp3',
    ['快破盾'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\break_shield.mp3',
    ['出去放水'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\move_out_drop.mp3',
    ['准备消层'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\reset_stacks.mp3',
    ['准备接圈'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\soak_puddle.mp3',
    ['召唤小怪'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\adds_spawning.mp3',
    ['你是白色'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\you_white.mp3',
    ['准备跑圈'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\run_puddles.mp3',
    ['准备进圈'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\step_puddle.mp3',
    ['风筝小怪'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\kite_adds.mp3',
    ['强化惩戒骑'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\emp_ret.mp3',
    ['快去消水'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\clear_puddles.mp3',
    ['注意躲避'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\watch_dodge.mp3',
    ['集合放圈'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\stack_drop.mp3',
    ['点名头前'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\frontal_on_you.mp3',
    ['准备送球'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\deliver_orb.mp3',
    ['注意消球'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\clear_orbs.mp3',
    ['countdown-5'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-5.mp3',
    ['倒数5'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-5.mp3',
    ['countdown-4'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-4.mp3',
    ['倒数4'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-4.mp3',
    ['countdown-3'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-3.mp3',
    ['倒数3'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-3.mp3',
    ['countdown-2'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-2.mp3',
    ['倒数2'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-2.mp3',
    ['countdown-1'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-1.mp3',
    ['倒数1'] = 'Interface\\AddOns\\EXBoss_Voice_German\\Sounds\\countdown-1.mp3',
}

-- English label translations (matching EXwindtools_EXboss_EnglishPatch)
local ENGLISH_LABELS = {
    ['转阶段'] = 'Phase Change',
    ['坦克击退'] = 'Tank Knockback',
    ['集合分摊'] = 'Stack Share',
    ['强化防骑'] = 'Empower Protection Paladin',
    ['特殊技能'] = 'Special Mechanic',
    ['准备AOE'] = 'Prepare AOE',
    ['快踩陷阱'] = 'Step Trap',
    ['你是黑色'] = 'You Are Black',
    ['准备钩人'] = 'Prepare Hook',
    ['靠近分散'] = 'Spread Close',
    ['点名放水'] = 'Target Drop Water',
    ['拉断连线'] = 'Break Link',
    ['注意点名'] = 'Watch Target',
    ['远离大怪'] = 'Away Add',
    ['注意挡球'] = 'Block Orb',
    ['准备驱散'] = 'Prepare Dispel',
    ['强化奶骑'] = 'Empower Holy Paladin',
    ['Boss狂暴'] = 'Boss Enrage',
    ['准备AOE(断条)'] = 'Prepare AOE (Break)',
    ['转火大怪'] = 'Switch Boss',
    ['快找光柱'] = 'Find Beacon',
    ['准备拉人'] = 'Prepare Grip',
    ['控断钢条'] = 'Interrupt Add',
    ['准备打断'] = 'Prepare Interrupt',
    ['注意击退'] = 'Watch Knockback',
    ['快卡视角'] = 'Fix Camera',
    ['转火小怪'] = 'Switch Add',
    ['易伤爆发'] = 'Burst Vulnerability',
    ['注意减伤'] = 'Use Defensive',
    ['去撞分身'] = 'Hit Clone',
    ['注意头前'] = 'Watch Frontal',
    ['小心击飞'] = 'Watch Launch',
    ['躲开头前'] = 'Dodge Frontal',
    ['小心冲击波'] = 'Watch Shockwave',
    ['点名消线'] = 'Target Clear Line',
    ['注意躲圈'] = 'Watch Circle',
    ['快分散'] = 'Spread Now',
    ['去接小怪'] = 'Intercept Add',
    ['准备踩箭'] = 'Prepare Arrow',
    ['准备连线'] = 'Prepare Link',
    ['准备挡线'] = 'Prepare Line Block',
    ['坦克尖刺'] = 'Tank Buster',
    ['准备追人'] = 'Prepare Chase',
    ['注意击飞'] = 'Watch Launch',
    ['准备吸球'] = 'Prepare Absorb Orb',
    ['准备点名'] = 'Prepare Target',
    ['Boss易伤'] = 'Boss Vulnerability',
    ['快救人'] = 'Rescue Now',
    ['远离BOSS'] = 'Away Boss',
    ['射线点你'] = 'Beam On You',
    ['快打断'] = 'Interrupt Now',
    ['准备引线'] = 'Prepare Beam',
    ['准备集合'] = 'Prepare Stack',
    ['快进罩子'] = 'Enter Bubble',
    ['注意治疗'] = 'Watch Healing',
    ['快破盾'] = 'Break Shield',
    ['出去放水'] = 'Drop Water',
    ['准备消层'] = 'Prepare Clear Stack',
    ['准备接圈'] = 'Prepare Soak',
    ['召唤小怪'] = 'Summon Add',
    ['你是白色'] = 'You Are White',
    ['准备跑圈'] = 'Watch Circle',
    ['准备进圈'] = 'Prepare Enter Circle',
    ['风筝小怪'] = 'Kite Add',
    ['强化惩戒骑'] = 'Empower Retribution Paladin',
    ['快去消水'] = 'Clear Water',
    ['注意躲避'] = 'Watch Dodge',
    ['集合放圈'] = 'Stack Drop Circle',
    ['点名头前'] = 'Target Frontal',
    ['准备送球'] = 'Prepare Orb Pass',
    ['注意消球'] = 'Clear Orb',
}

-- Register under our own pack name (Chinese + English labels)
for label, path in pairs(sounds) do
    LSM:Register('sound', '[' .. PACK_NAME .. ']' .. label, path)
    -- Also register with English label so it works with EnglishPatch
    local enLabel = ENGLISH_LABELS[label]
    if enLabel then
        LSM:Register('sound', '[' .. PACK_NAME .. ']' .. enLabel, path)
    end
end

-- Welcome Screen
local function ShowWelcomeScreen()
    local frame = CreateFrame("Frame", "TTSGermanWelcomeFrame", UIParent, "BackdropTemplate")
    frame:SetSize(400, 200)
    frame:SetPoint("CENTER")
    frame:SetFrameStrata("DIALOG")
    
    frame:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
        tile = true, tileSize = 32, edgeSize = 32,
        insets = { left = 8, right = 8, top = 8, bottom = 8 }
    })
    
    local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOP", 0, -20)
    title:SetText("EXBOSS German TTS Pack")
    
    local desc = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightMedium")
    desc:SetPoint("TOP", title, "BOTTOM", 0, -20)
    desc:SetWidth(360)
    desc:SetText("Willkommen! Um das Voice Pack zu konfigurieren, gib |cff00ff00/exboss|r im Chat ein und wähle unter 'Voice Pack' den Eintrag |cff00ff00EXBOSS German TTS Pack|r aus.")
    
    local btn = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
    btn:SetSize(100, 30)
    btn:SetPoint("BOTTOM", 0, 20)
    btn:SetText("Verstanden")
    btn:SetScript("OnClick", function()
        frame:Hide()
    end)
    
    frame:Show()
end

local loader = CreateFrame("Frame")
loader:RegisterEvent("ADDON_LOADED")
loader:SetScript("OnEvent", function(self, event, addonName)
    if addonName == "EXBoss_Voice_German" then
        TTSVoiceDB_German = TTSVoiceDB_German or {}
        if not TTSVoiceDB_German.shownOnce then
            ShowWelcomeScreen()
            TTSVoiceDB_German.shownOnce = true
        end
        self:UnregisterEvent("ADDON_LOADED")
    end
end)
