local Foods = {192002}

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.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"}
}, {
    str = "This one has become vestigial, useless.",
    events = {"PLAYER_LEFT_GUILD"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_town_dismiss_05.ogg"}
}, {
    str = "Suffer not the lame horse... nor the broken man.",
    events = {"PLAYER_LEFT_GUILD"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_town_dismiss_04.ogg"}
}, {
    str = "Another soul battered and broken, cast aside like a spent torch.",
    events = {"PLAYER_LEFT_GUILD"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_town_dismiss_01.ogg"}
}, {
    str = "Those without the stomach for this place must move on.",
    events = {"PLAYER_LEFT_GUILD"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_town_dismiss_02.ogg"}
}, {
    str = "Send this one to journey elsewhere, for we have need of sterner stock.",
    events = {"PLAYER_LEFT_GUILD"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_town_dismiss_03.ogg"}
}, {
    str = "Slumped shoulders, wild eyes, and a stumbling gait - this one is no more good to us.",
    events = {"PLAYER_LEFT_GUILD"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_town_dismiss_08.ogg"}
}, {
    str = "The task ahead is terrible, and weakness cannot be tolerated.",
    events = {"PLAYER_LEFT_GUILD"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_town_dismiss_06.ogg"}
}, {
    str = "A moment of respite. A chance to steel oneself against the coming horrors.",
    events = {"FOOD_EATEN"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_camp_01.ogg"}
}, {
    str = "Gathered close in tenuous firelight, and uneasy companionship.",
    events = {"FOOD_EATEN"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_town_dismiss_04.ogg"}
}, {
    str = "Huddled together, furtive and vulnerable. Rats in a maze.",
    events = {"FOOD_EATEN"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Vo_narr_good_camp_05.ogg"}
}, {
    str = "The requirements of survival cannot be met on an empty stomach.",
    events = {"FOOD_EXPIRED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_starve5.ogg"}
}, {
    str = "No force of will can overcome a failing body.",
    events = {"FOOD_EXPIRED"},
    sound = {"Interface/AddOns/AncestorQuotes/sounds/Narration_starve4.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");
frame:RegisterEvent("CHAT_MSG_SYSTEM");

local playerGUID = UnitGUID("player")
local function eventHandler(self, event, ...)
    if (onCooldown) then
        return
    end
    if (event == "ENCOUNTER_END") then
        local 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 = CombatLogGetCurrentEventInfo();
        if (subevent == "UNIT_DIED") then
            print(destGUID .. destName)
        end
        if (subevent == "UNIT_DIED" and strfind(destGUID, "Player") and not UnitIsFeignDeath(destName)) then
            if (UnitGroupRolesAssigned(destName) == "DAMAGER" or UnitGroupRolesAssigned(destName) == "HEALER" or
                UnitGroupRolesAssigned(destName) == "TANK") then
                local test = math.random()
                if (test > 0.70) then
                    processQuote({"PARTY_MEMBER_DIED"})
                    return
                end
            end
        end
        if (subevent == "SPELL_AURA_APPLIED" and destGUID == playerGUID) then
            local spellId, spellName = select(12, CombatLogGetCurrentEventInfo()) -- i hate lua
            if (spellName == "Well Fed") then
                processQuote({"FOOD_EATEN"})
                return
            end
            if (spellName == "Heroism" or spellName == "Time Warp" or spellName == "Bloodlust" or spellName ==
                "Power Infusion") then
                processQuote({"RECEIVED_BUFF"})
                return
            end
        end
        if (subevent == "SPELL_AURA_REMOVED" and destGUID == playerGUID) then
            local spellId, spellName = select(12, CombatLogGetCurrentEventInfo()) -- i hate lua
            if (spellName == "Well Fed") then
                processQuote({"FOOD_EXPIRED"})
                return
            end
        end
    end

    if (event == "CHAT_MSG_SYSTEM") then
        local msg = ...
        if (string.match(msg, string.gsub(ERR_GUILD_LEAVE_S, "%%s", "") or
            string.match(msg, string.gsub(ERR_GUILD_REMOVE_SS, "%%s", "")))) then -- i am so sorry
            processQuote({"PLAYER_LEFT_GUILD"})
            return
        end
    end
end

frame:SetScript("OnEvent", eventHandler);
