local function isScourgeFemaleAlive()
    local gender = UnitSex("player")
    local race = select(2, UnitRace("player"))
    local alive = not UnitIsDeadOrGhost("player")
    return race == "Scourge" and gender == 3 and alive
end



-- SITUATION #1 - ENTER COMBAT
local frameEnterCombat = CreateFrame("Frame")
frameEnterCombat:RegisterEvent("PLAYER_REGEN_DISABLED")
frameEnterCombat:SetScript("OnEvent", function(self, event, ...)
    if isScourgeFemaleAlive() then
        if math.random() <= 0.6 then
            local soundFiles = {
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Attack01.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Attack02.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Attack03.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Attack04.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Attack05.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Attack06.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Attack07.mp3"
            }
            local randomIndex = math.random(1, #soundFiles)
            PlaySoundFile(soundFiles[randomIndex], "Dialog")
        end
    end
end)


-- SITUATION #2 - QUEST UPDATE -- START


-- Store the initial quest log state
local lastQuestLogState = {}

local function getQuestLogState()
    local state = {}
    for i = 1, GetNumQuestLogEntries() do
        local title, _, _, _, _, _, _, questID = GetQuestLogTitle(i)
        if questID then
            state[questID] = title
        end
    end
    return state
end

local function hasQuestLogUpdated()
    local currentState = getQuestLogState()
    for questID, title in pairs(currentState) do
        if lastQuestLogState[questID] == nil then
            lastQuestLogState = currentState
            return true
        end
    end
    lastQuestLogState = currentState
    return false
end

-- SITUATION #2 - QUEST UPDATE -- MIDDLE
local frameQuestUpdate = CreateFrame("Frame")
local lastSoundTime = 0
local cooldown = 3

frameQuestUpdate:RegisterEvent("QUEST_LOG_UPDATE")
frameQuestUpdate:SetScript("OnEvent", function(self, event, ...)
    local currentTime = GetTime()
    if currentTime - lastSoundTime >= cooldown and isScourgeFemaleAlive() and hasQuestLogUpdated() then
        local soundFiles = {
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate01.mp3",
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate02.mp3",
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate03.mp3",
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate04.mp3",
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate05.mp3",
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate06.mp3",
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate07.mp3",
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate08.mp3",
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate09.mp3",
            "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\QuestUpdate10.mp3"
        }
        local randomIndex = math.random(1, #soundFiles)
        PlaySoundFile(soundFiles[randomIndex], "Dialog")
        lastSoundTime = currentTime
    end
end)

-- Initialize the last quest log state
lastQuestLogState = getQuestLogState()

-- SITUATION #3 - COMBAT ENDED
local frameLeaveCombat = CreateFrame("Frame")
frameLeaveCombat:RegisterEvent("PLAYER_REGEN_ENABLED")
frameLeaveCombat:SetScript("OnEvent", function(self, event, ...)
    local notMounted = not IsMounted()
    local notOnTaxi = not UnitOnTaxi("player")
    local notInVehicle = not UnitInVehicle("player")
    if isScourgeFemaleAlive() and notMounted and notOnTaxi and notInVehicle then
        if math.random() <= 0.5 then
            local soundFiles = {
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\CombatEnded01.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\CombatEnded02.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\CombatEnded03.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\CombatEnded04.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\CombatEnded05.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\CombatEnded06.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\CombatEnded07.mp3"
            }
            local randomIndex = math.random(1, #soundFiles)
            PlaySoundFile(soundFiles[randomIndex], "Dialog")
        end
    end
end)

-- SITUATION #4 - PLAYER IDLE
local idleFrame = CreateFrame("Frame", "IdleStatusFrame", UIParent)
idleFrame:SetSize(200, 50)
idleFrame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
local idleText = idleFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
idleText:SetPoint("CENTER", idleFrame, "CENTER", 0, 100)
idleText:SetText("")
idleText:SetFont("Fonts\\FRIZQT__.TTF", 20)
idleText:SetTextColor(1, 1, 1)

local function ShowIdleStatusMessage(message)
    idleText:SetText(message)
    idleFrame:Show()
    idleFrame:SetAlpha(1)
    idleFrame.fadeOutAnim = idleFrame:CreateAnimationGroup()
    idleFrame.fadeOutAnim.fadeOut = idleFrame.fadeOutAnim:CreateAnimation("Alpha")
    idleFrame.fadeOutAnim.fadeOut:SetDuration(3.0)
    idleFrame.fadeOutAnim.fadeOut:SetFromAlpha(1)
    idleFrame.fadeOutAnim.fadeOut:SetToAlpha(0)
    idleFrame.fadeOutAnim:SetScript("OnFinished", function() idleFrame:Hide() end)
    idleFrame.fadeOutAnim:Play()
end

local frameIdle = CreateFrame("Frame")
local timer
local idleThreshold = 60
local isIdle = false

frameIdle:SetScript("OnUpdate", function(self, elapsed)
    if UnitIsDeadOrGhost("player") or UnitAffectingCombat("player") or UnitOnTaxi("player") or UnitInVehicle("player") then
        return
    end

    if not IsPlayerMoving() and not IsMouselooking() then
        if not isIdle then
            timer = (timer or 0) + elapsed
            if timer >= idleThreshold then
                if isScourgeFemaleAlive() then
                    local soundFiles = {
                        "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Idle01.mp3",
                        "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Idle02.mp3",
                        "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Idle03.mp3"
                    }
                    local randomIndex = math.random(1, #soundFiles)
                    PlaySoundFile(soundFiles[randomIndex], "Combat")
                    ShowIdleStatusMessage("I better get moving...")
                    isIdle = true
                end
            end
        end
    else
        timer = 0
        isIdle = false
    end
end)

-- SITUATION #5 - SUMMONING A MOUNT
local frameMount = CreateFrame("Frame")
frameMount:RegisterEvent("PLAYER_MOUNT_DISPLAY_CHANGED")
frameMount:SetScript("OnEvent", function(self, event, ...)
    if isScourgeFemaleAlive() and IsMounted() then
        C_Timer.After(1, function()
            local soundFiles = {
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Mounted01.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Mounted02.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Mounted03.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Mounted04.mp3"
            }
            local randomIndex = math.random(1, #soundFiles)
            PlaySoundFile(soundFiles[randomIndex], "Combat")
        end)
    end
end)

-- SITUATION #6 - LOW HEALTH
local frameHealth = CreateFrame("Frame")
local soundPlayed = false

frameHealth:RegisterEvent("UNIT_HEALTH")
frameHealth:RegisterEvent("PLAYER_ENTERING_WORLD")
frameHealth:SetScript("OnEvent", function(self, event, ...)
    if event == "PLAYER_ENTERING_WORLD" then
        if isScourgeFemaleAlive() then
            self:RegisterEvent("UNIT_HEALTH")
        else
            self:UnregisterEvent("UNIT_HEALTH")
        end
    elseif event == "UNIT_HEALTH" then
        local healthPercent = UnitHealth("player") / UnitHealthMax("player")
        if healthPercent < 0.35 and not soundPlayed then
            local soundFiles = {
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Lowhp01.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Lowhp02.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Lowhp03.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Lowhp04.mp3"
            }
            local randomIndex = math.random(1, #soundFiles)
            PlaySoundFile(soundFiles[randomIndex], "Dialog")
            soundPlayed = true
        elseif healthPercent >= 0.35 then
            soundPlayed = false
        end
    end
end)



-- SITUATION #7 - ENTERING COMBAT WITH A STRONGER ENEMY
local frameEnterCombat = CreateFrame("Frame")
frameEnterCombat:RegisterEvent("PLAYER_REGEN_DISABLED")
frameEnterCombat:SetScript("OnEvent", function(self, event, ...)
    if isScourgeFemaleAlive() then
        local inInstance, instanceType = IsInInstance()
        if inInstance and (instanceType == "pvp" or instanceType == "arena") then
            return
        end
        local playerHealth = UnitHealth("player")
        local enemyHealth = UnitHealth("target")
        local healthThreshold = playerHealth * 7.0
        if enemyHealth > healthThreshold then
            local soundFiles = {
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Engageboss01.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Engageboss02.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Engageboss03.mp3",
                "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Engageboss04.mp3"
            }
            local randomIndex = math.random(1, #soundFiles)
            PlaySoundFile(soundFiles[randomIndex], "Error")
        end
    end
end)

-- SITUATION #8 - DEFEATING A STRONGER ENEMY OR BOSS
local frameDefeatBoss = CreateFrame("Frame")
frameDefeatBoss:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
frameDefeatBoss:SetScript("OnEvent", function(self, event, ...)
    local timestamp, eventType, _, sourceGUID, sourceName, _, _, destGUID, destName, _, _, spellID, spellName = CombatLogGetCurrentEventInfo()
    if eventType == "UNIT_DIED" then
        local _, _, _, _, _, npcID = strsplit("-", destGUID)
        local playerHealth = UnitHealthMax("player")
        local enemyMaxHealth = UnitHealthMax("target")
        if npcID and enemyMaxHealth then
            npcID = tonumber(npcID)
            local healthThreshold = playerHealth * 10.0
            if enemyMaxHealth >= healthThreshold and UnitIsEnemy("player", "target") and UnitGUID("target") == destGUID then
                local soundFiles = {
                    "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Defeatboss_01.mp3",
                    "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Defeatboss_02.mp3",
                    "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Defeatboss_03.mp3",
                    "Interface\\AddOns\\VoicesUndeadFemale\\Audio\\Defeatboss_04.mp3"
                }
                local randomIndex = math.random(1, #soundFiles)
                PlaySoundFile(soundFiles[randomIndex], "Dialog")
            end
        end
    end
end)