
-- SITUATION #1 - ENTER COMBAT


local frameEnterCombat = CreateFrame("Frame")

frameEnterCombat:RegisterEvent("PLAYER_REGEN_DISABLED")

frameEnterCombat:SetScript("OnEvent", function(self, event, ...)
    local gender = UnitSex("player")
    local race = select(2, UnitRace("player"))
    local alive = not UnitIsDeadOrGhost("player")

    -- Check if character is a Gnome female and alive
    if race == "Gnome" and gender == 3 and alive then
        -- Add a 60% chance to play the sound
        if math.random() <= 0.6 then
            local soundFiles = {
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Attack01.ogg",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Attack02.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Attack03.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Attack04.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Attack05.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Attack06.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Attack07.mp3"
            }

            local randomIndex = math.random(1, #soundFiles)
            local soundFilePath = soundFiles[randomIndex]

            PlaySoundFile(soundFilePath, "Dialog")
        end
    end
end)











-- SITUATION #2 - QUEST UPDATE

local frameQuestUpdate = CreateFrame("Frame")
local lastSoundTime = 0
local cooldown = 3

frameQuestUpdate:RegisterEvent("QUEST_LOG_UPDATE")

frameQuestUpdate:SetScript("OnEvent", function(self, event, ...)
    local currentTime = GetTime()
    
    -- Check if it's been more than the cooldown period since the last sound played
    if currentTime - lastSoundTime >= cooldown then
        local gender = UnitSex("player")
        local race = select(2, UnitRace("player"))
        local alive = not UnitIsDeadOrGhost("player")

        -- Check if character is a Gnome female and alive
        if race == "Gnome" and gender == 3 and alive then
            local soundFiles = {
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate01.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate02.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate03.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate04.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate05.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate06.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate07.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate08.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate09.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\QuestUpdate10.mp3"     
            }

            local randomIndex = math.random(1, #soundFiles)
            local soundFilePath = soundFiles[randomIndex]

            PlaySoundFile(soundFilePath, "Dialog")
            
            lastSoundTime = currentTime
        end
    end
end)















-- SITUATION #3 COMBAT ENDED


local frameLeaveCombat = CreateFrame("Frame")

frameLeaveCombat:RegisterEvent("PLAYER_REGEN_ENABLED")

frameLeaveCombat:SetScript("OnEvent", function(self, event, ...)
    local gender = UnitSex("player")
    local race = select(2, UnitRace("player"))
    local alive = not UnitIsDeadOrGhost("player")
    local notMounted = not IsMounted()
    local notOnTaxi = not UnitOnTaxi("player")
    local notInVehicle = not UnitInVehicle("player")

    -- Check if character is a Gnome female, alive, not mounted, not on taxi, and not in a vehicle
    if race == "Gnome" and gender == 3 and alive and notMounted and notOnTaxi and notInVehicle then
        -- Add a 60% chance to play the sound
        if math.random() <= 0.6 then
            local soundFiles = {
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\CombatEnded01.ogg",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\CombatEnded02.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\CombatEnded03.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\CombatEnded04.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\CombatEnded05.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\CombatEnded06.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\CombatEnded07.mp3"
            }

            local randomIndex = math.random(1, #soundFiles)
            local soundFilePath = soundFiles[randomIndex]

            PlaySoundFile(soundFilePath, "Dialog")
        end
    end
end)








-- SITUATION #4 - PLAY VOICE LINE SOUND 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 -- time before considering the player actually idle
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
                PlayGnomeFemaleIdleSound()
                ShowIdleStatusMessage("I better get moving...")
                isIdle = true
            end
        end
    else
        timer = 0
        isIdle = false
    end
end)

function PlayGnomeFemaleIdleSound()
        local soundFiles = {
            "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Idle01.mp3",
            "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Idle02.mp3",
            "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Idle03.mp3"
        }
        local randomIndex = math.random(1, #soundFiles)
        local soundFilePath = soundFiles[randomIndex]
        PlaySoundFile(soundFilePath, "Combat")
end





-- SITUATION #5 - PLAY VOICE LINE SOUND WHEN SUMMONING A MOUNT ---------------------------------------------------------------------------------------

local frameMount = CreateFrame("Frame")
frameMount:RegisterEvent("PLAYER_MOUNT_DISPLAY_CHANGED")
frameMount:SetScript("OnEvent", function(self, event, ...)
    if UnitIsDeadOrGhost("player") then
        return
    end

    if isGnomeFemale then
        if IsMounted() then
            local function PlayDelayedSound()
                local soundFiles = {
                    "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Mounted01.mp3",
                    "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Mounted02.mp3",
                    "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Mounted03.mp3",
                    "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Mounted04.mp3"
                }
                local randomIndex = math.random(1, #soundFiles)
                local soundFilePath = soundFiles[randomIndex]
                PlaySoundFile(soundFilePath, "Combat")
            end
            
            C_Timer.After(1, PlayDelayedSound)
        end
    end
end)






-- SITUATION #6 - PLAY VOICE LINE SOUND WHEN PLAYER'S ON LOW HEALTH ---------------------------------------------------------------------------------------
local frameHealth = CreateFrame("Frame")
local soundPlayed = false

frameHealth:RegisterEvent("UNIT_HEALTH")
frameHealth:RegisterEvent("PLAYER_ENTERING_WORLD")

local function isGnomeFemale()
    local gender = UnitSex("player")
    local race = select(2, UnitRace("player"))
    return race == "Gnome" and gender == 3
end

frameHealth:SetScript("OnEvent", function(self, event, ...)
    if event == "PLAYER_ENTERING_WORLD" then
        if UnitIsDeadOrGhost("player") or IsMounted() or UnitOnTaxi("player") then
            return
        end

        if isGnomeFemale() 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\\VoicesGnomeFemale\\Audio\\Lowhp01.ogg",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Lowhp02.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Lowhp03.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Lowhp04.mp3"
            }
            local randomIndex = math.random(1, #soundFiles)
            local soundFilePath = soundFiles[randomIndex]
            PlaySoundFile(soundFilePath, "Dialog")

            soundPlayed = true
        elseif healthPercent >= 0.35 then
            soundPlayed = false
        end
    end
end)




-- SITUATION #7 - PLAY VOICE LINE SOUND UPON ENTERING COMBAT WITH A STRONGER ENEMY ---------------------------------------------------------------------------------------
local frameEnterCombat = CreateFrame("Frame")
frameEnterCombat:RegisterEvent("PLAYER_REGEN_DISABLED")
frameEnterCombat:SetScript("OnEvent", function(self, event, ...)
    if UnitIsDeadOrGhost("player") or IsMounted() or UnitOnTaxi("player") or UnitInVehicle("player") then
        return
    end

    if isNightborneFemale 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\\VoicesGnomeFemale\\Audio\\Engageboss01.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Engageboss02.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Engageboss03.mp3",
                "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Engageboss04.mp3"
            }
        
            local randomIndex = math.random(1, #soundFiles)
            local soundFilePath = soundFiles[randomIndex]
            PlaySoundFile(soundFilePath, "Error")
        end
    end
end)




-- SITUATION #8 - PLAY VOICE LINE SOUND UPON 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 then
                if UnitIsEnemy("player", "target") then
                    local targetGUID = UnitGUID("target")

                    if targetGUID == destGUID then
                        local soundFiles = {
                            "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Defeatboss_01.mp3",
                            "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Defeatboss_02.mp3",
                            "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Defeatboss_03.mp3",
                            "Interface\\AddOns\\VoicesGnomeFemale\\Audio\\Defeatboss_04.mp3"
                        }
                        local randomIndex = math.random(1, #soundFiles)
                        local soundFilePath = soundFiles[randomIndex]

                        PlaySoundFile(soundFilePath, "Dialog")
                    end
                end
            end
        end
    end
end)



-- SITUATION #9 - LOW MANA WARNING ---------------------------------------------------------------------------------------

-- LOW MANA WARNING

local frame = CreateFrame("Frame")
local lowManaSoundPlayed = false

local function PlayLowManaSound()
    -- List of sound files
    local soundFiles = {
        "Interface\\AddOns\\YourAddonFolder\\Lowmana01.ogg",
        "Interface\\AddOns\\YourAddonFolder\\Lowmana02.ogg",
        "Interface\\AddOns\\YourAddonFolder\\Lowmana03.ogg"
    }
    
    -- Select a random sound file
    local randomIndex = math.random(1, #soundFiles)
    local soundFilePath = soundFiles[randomIndex]
    
    -- Play the selected sound file
    PlaySoundFile(soundFilePath, "Dialog")
end

local function OnEvent(self, event, ...)
    if event == "UNIT_POWER_UPDATE" then
        local unit, powerType = ...
        if unit == "player" and powerType == "MANA" and not UnitIsDeadOrGhost("player") then
            local manaPercent = UnitPower("player", Enum.PowerType.Mana) / UnitPowerMax("player", Enum.PowerType.Mana)
            if manaPercent < 0.3 and not lowManaSoundPlayed then
                PlayLowManaSound()
                lowManaSoundPlayed = true
            elseif manaPercent >= 0.3 then
                lowManaSoundPlayed = false
            end
        end
    end
end

frame:RegisterEvent("UNIT_POWER_UPDATE")
frame:SetScript("OnEvent", OnEvent)
