-- Create and register the event frame
local initFrame = CreateFrame("Frame")
initFrame:RegisterEvent("ADDON_LOADED")
initFrame:RegisterEvent("QUEST_DETAIL")
initFrame:RegisterEvent("QUEST_TURNED_IN")
initFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
initFrame:RegisterEvent("PLAYER_LOGOUT")
initFrame:RegisterEvent("PLAYER_DEAD")

if WOW_PROJECT_ID == WOW_PROJECT_CLASSIC then
    initFrame:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
end

if WOW_PROJECT_ID == WOW_PROJECT_MAINLINE then
    initFrame:RegisterEvent("LOOT_READY")
    initFrame:RegisterEvent("LOOT_OPENED")
end

local function OnLootReadyOrOpened(event)
    for i = 1, GetNumLootItems() do
        local itemLink = GetLootSlotLink(i)
        if itemLink then
            local itemName, _, _, _, _, itemType = GetItemInfo(itemLink)
            if itemType == "Tradeskill" then
                local subType = select(7, GetItemInfo(itemLink))
                if subType == "Herb" or subType == "Metal & Stone" or subType == "Leather" or subType == "Cooking" then
                    WoWStudyFunction.ShowFlashcard()
                end
            end
        end
    end
end

-- Initalise everything for the addon
local function OnEvent(_, event, ...)
    if event == "ADDON_LOADED" then
        local addonName = ...
        if addonName == "WoWStudy" then
            WoWStudySaved = WoWStudySaved or {}
            WoWStudySaved.decks = WoWStudySaved.decks or {}
            WoWStudySaved.userInformation = WoWStudySaved.userInformation or {
                lifetimeCorrect = 0,
                lifetimeAnswered = 0,
                lifetimeIncorrect = 0,
                totalDecks = 0,
                totalCards = 0,
            }
            WoWStudySaved.sessions = WoWStudySaved.sessions or {}
            WoWStudySaved.settings = WoWStudySaved.settings or {}

            WoWStudyFunction.Initialise()
        end
    elseif event == "PLAYER_LOGOUT" then
        WoWStudyFunction.EndSession()    
    elseif event == "PLAYER_DEAD" then
        if WoWStudySaved.settings.enableOnDeath then
            WoWStudyFunction.ShowFlashcard()
        end   
    elseif event == "QUEST_DETAIL" or event == "QUEST_TURNED_IN" then
        if WoWStudySaved.settings.enableQuests then
            WoWStudyFunction.ShowFlashcard() -- Trigger flashcard on quest-related events
        end
    elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then
        local _, subEvent, _, sourceGUID = CombatLogGetCurrentEventInfo()
        if subEvent == "PARTY_KILL" then
            local playerGUID = UnitGUID("player")
            if sourceGUID == playerGUID then
                WoWStudyGlobal.kills = WoWStudyGlobal.kills + 1
                if WoWStudySaved.settings.enableKills and WoWStudyGlobal.kills % WoWStudySaved.settings.killInterval == 0 then
                    WoWStudyFunction.ShowFlashcard() -- Trigger flashcard on every nth kill
                end
            end
        end
    elseif event == "UNIT_SPELLCAST_SUCCEEDED" then
        local unit, _, spellID = ...
        if unit == "player" then
            local spellName = GetSpellInfo(spellID)
            if spellName == "Mining" or spellName == "Herbalism" or spellName == "Skinning" or spellName == "Fishing" then
                if WoWStudySaved.settings.gatheringCheck then
                    WoWStudyFunction.ShowFlashcard() -- Trigger flashcard on gathering
                end
            end
        end
    elseif event == "LOOT_READY" or event == "LOOT_OPENED" then
        if WoWStudySaved.settings.gatheringCheck then
            OnLootReadyOrOpened(event)
        end
    end
end

initFrame:SetScript("OnEvent", OnEvent)