local Quotes = {{
    str = "A trifling victory, but a victory nonetheless.",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_victoryfirst_03.ogg"}
}, {
    str = "These nightmarish creatures can be felled! They can be beaten!",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_victoryfirst_01.ogg"}
}, {
    str = "Seize this momentum! Push on to the task's end!",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_victoryfirst_02.ogg"}
}, {
    str = "This expedition, at least, promises success.",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_victoryfirst_04.ogg"}
}, {
    str = "As victories mount, so too will resistance.",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_victoryfirst_05.ogg"}
}, {
    str = "Success so clearly in view... or is it merely a trick of the light?",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_victoryfirst_09.ogg"}
}, {
    str = "Remind yourself that overconfidence is a slow and insidious killer.",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_victoryfirst_02.ogg"}
}, {
    str = "Be wary - triumphant pride precipitates a dizzying fall...",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_victoryfirst_06.ogg"}
}, {
    str = "Confidence surges as the enemy crumbles!",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_killfirst_02.ogg"}
}, {
    str = "Another abomination cleansed from our lands.",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_kill_weak_02.ogg"}
}, {
    str = "Prodigious size alone does not dissuade the sharpened blade.",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_kill_big_03.ogg"}
}, {
    str = "Their cursed champion falls!",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_kill_big_04.ogg"}
}, {
    str = "The bigger the beast, the greater the glory.",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_kill_big_01.ogg"}
}, {
    str = "A victory - perhaps a turning point.",
    events = {"BOSS_KILLED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_kill_big_02.ogg"}
}, {
    str = "A wise general cuts losses, and regroups.",
    events = {"BOSS_FAILED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_retreat_success_02.ogg"}
}, {
    str = "This skirmish may be lost, but the battle may yet be won.",
    events = {"BOSS_FAILED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_retreat_success_01.ogg"}
}, {
    str = "The sin is not in being outmatched, but in failing to recognize it.",
    events = {"BOSS_FAILED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_retreat_success_03.ogg"}
}, {
    str = "A setback, but not the end of things!",
    events = {"BOSS_FAILED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_expeditionfail1.ogg"}
}, {
    str = "Wounds to be tended; lessons to be learned.",
    events = {"BOSS_FAILED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_expeditionfail2.ogg"}
}, {
    str = "Regroup. Reassemble. Evil is timeless, after all.",
    events = {"BOSS_FAILED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_expeditionfail3.ogg"}
}, {
    str = "Failure tests the mettle of heart, brain, and body.",
    events = {"BOSS_FAILED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_expeditionfail4.ogg"}
}, {
    str = "You cannot learn a thing you think you know...",
    events = {"BOSS_FAILED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_bad_questfail_11.ogg"}
}, {
    str = "Do not ruminate on this fleeting failure - the campaign is long, and victory will come.",
    events = {"BOSS_FAILED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_bad_questfail_07.ogg"}
}, {
    str = "More blood soaks the soil, feeding the evil therein.",
    events = {"PARTY_MEMBER_DIED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_death2.ogg"}
}, {
    str = "This is no place for the weak, or the foolhardy.",
    events = {"PARTY_MEMBER_DIED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_death4.ogg"}
}, {
    str = "Another life wasted in the pursuit of glory and gold.",
    events = {"PARTY_MEMBER_DIED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_death3.ogg"}
}, {
    str = "More dust, more ashes, more disappointment.",
    events = {"PARTY_MEMBER_DIED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_death6.ogg"}
}, {
    str = "Anger is power - unleash it!",
    events = {"RECEIVED_BUFF"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_virtue_powerful.ogg"}
}, {
    str = "The blood pumps, the limbs obey.",
    events = {"RECEIVED_BUFF"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_healcrit_03.ogg"}
}, {
    str = "A time to perform beyond one's limits!",
    events = {"RECEIVED_BUFF"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_buff_03.ogg"}
}, {
    str = "Inspiration and improvement!",
    events = {"RECEIVED_BUFF"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_buff_04.ogg"}
}, {
    str = "The blood quickens!",
    events = {"RECEIVED_BUFF"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_buff_01.ogg"}
}, {
    str = "A brilliant confluence of skill and purpose!!",
    events = {"RECEIVED_BUFF"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_buff_02.ogg"}
}, {
    str = "Press this advantage, give them no quarter!",
    events = {"RECEIVED_BUFF"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_killfirst_03.ogg"}
}}

-- shamelessly stolen from https://stackoverflow.com/a/33511182
local function has_value(tab, val)
    for index, value in pairs(tab) do
        if value == val then
            return true
        end
    end
    return false
end

onCooldown = false;
cooldownTime = 8

local function processQuote(filter)
    -- Create a temporary table with the quotes matching the filters
    -- Not sure of the performance cost of this operation, might be worth it to generate only once
    local tmp = {}
    for index, quote in pairs(Quotes) do
        for _, event in pairs(quote.events) do
            if has_value(filter, event) then
                table.insert(tmp, quote)
            end
        end
    end
    if (#tmp == 0) then
        return -- no quote matches filters. outta here!
    end
    local quote = tmp[math.random(#tmp)]
    print(quote.str); -- print in chat for now, mostly debug purposes. maybe /say it in the future idk
    if (quote.sound) then
        PlaySoundFile(quote.sound[1], "Dialog");
    end
    onCooldown = true;
    C_Timer.After(cooldownTime, function()
        onCooldown = false
        print("Cooldown reset!")
    end);
end

local frame = CreateFrame("FRAME", "FooAddonFrame");
frame:RegisterEvent("ENCOUNTER_END");
frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");

local function eventHandler(self, event, ...)
    if (onCooldown) then
        return
    end
    if (event == "ENCOUNTER_END") then
        encounterID, encounterName, difficultyID, groupSize, success = ...;
        if (success == 1) then
            processQuote({"BOSS_KILLED"});
            return
        else
            processQuote({"BOSS_FAILED"});
            return
        end
    end

    if (event == "COMBAT_LOG_EVENT_UNFILTERED") then
        local timestamp, subevent, _, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName,
            destFlags, destRaidFlags = ... -- seriously lua ??
        if (subevent == "UNIT_DIED" and strfind(guid, "Player") and not UnitIsFeignDeath(sourceName)) then
            if (UnitGroupRolesAssigned(sourceName) == "DAMAGER" or UnitGroupRolesAssigned(sourceName) == "HEALER" or
                UnitGroupRolesAssigned(sourceName) == "TANK") then
                local test = math.random()
                if (test > 0.70) then
                    processQuote({"PARTY_MEMBER_DIED"})
                    return
                end
            end
        end
        if subevent == "SPELL_AURA_APPLIED" then
            local auraType, amount = select(15, ...) -- i hate lua
            if (auraType == "Power Infusion" or auraType == "Bloodlust" or auraType == "Heroism" or auraType ==
                "Timewarp") then
                processQuote({"RECEIVED_BUFF"})
                return
            end
        end
    end
end

frame:SetScript("OnEvent", eventHandler);
