local addonName, ZLAUTO = ...

local ZL = ZLAUTO.mainFrame

local isNotLoot
local isStart, lastTime, intervalTime, bossName

local function lootMsg(self, event, arg1, arg2, _, _, arg5)
    if event == "TRADE_ACCEPT_UPDATE" then
        if arg1 == 1 or arg2 == 1 then
            isNotLoot = true
        else
            isNotLoot = nil
        end
    elseif event == "TRADE_CLOSED" then
        isNotLoot = nil
    elseif event == "MERCHANT_UPDATE" or event == "QUEST_TURNED_IN" or event == "QUEST_FINISHED" then
        isNotLoot = true
        C_Timer.After(0.5, function()
            isNotLoot = nil
        end)
    elseif event == "PLAYER_REGEN_DISABLED" then
        C_Timer.After(0.5, function ()
            if isStart then return end
            intervalTime = GetTime()
            if bossName ~= "其他" then
                if (not lastTime) or (intervalTime - lastTime >= 30) then
                    bossName = "其他"
                end
            end
        end)
    elseif event == "ENCOUNTER_START" then
        isStart = true
    elseif event == "ENCOUNTER_END" then
        isStart = nil
        if arg5 == 1 then
            lastTime = GetTime()
            bossName = arg2
        else
            bossName = "其他"
        end
    elseif event == "CHAT_MSG_LOOT" then
        if isNotLoot then return end
        local lootplayer, itemlink, count
        itemlink, count = strmatch(arg1, string.gsub(string.gsub(LOOT_ITEM_SELF_MULTIPLE, "%%s", "(.+)"), "%%d", "(%%d+)"));
        if (not itemlink) then
            itemlink, count = strmatch(arg1, string.gsub(string.gsub(LOOT_ITEM_PUSHED_SELF_MULTIPLE, "%%s", "(.+)"), "%%d", "(%%d+)"));
            if (not itemlink) then
                itemlink = arg1:match(LOOT_ITEM_SELF:gsub("%%s", "(.+)"));
                if (not itemlink) then
                    itemlink = arg1:match(LOOT_ITEM_PUSHED_SELF:gsub("%%s", "(.+)"));
                    if (not itemlink) then
                        lootplayer, itemlink, count = strmatch(arg1, string.gsub(string.gsub(LOOT_ITEM_MULTIPLE, "%%s", "(.+)"), "%%d", "(%%d+)"));
                        if (not itemlink) then
                            lootplayer, itemlink, count = strmatch(arg1, string.gsub(string.gsub(LOOT_ITEM_PUSHED_MULTIPLE, "%%s", "(.+)"), "%%d", "(%%d+)"));
                            if (not itemlink) then
                                lootplayer, itemlink = arg1:match("^" .. LOOT_ITEM:gsub("%%s", "(.+)"));
                                if (not itemlink) then
                                    lootplayer, itemlink = arg1:match("^" .. LOOT_ITEM_PUSHED:gsub("%%s", "(.+)"));
                                end
                            end
                        end
                    end
                end
            end
        end
        if not itemlink then return end
        lootplayer = lootplayer or UnitName("player")
        lootplayer = ZL.GetClassColorName(lootplayer)
        count = count or 1
        count = tonumber(count)
        local _, _, quality, level, _, _, _, stackCount, _, Texture, _, typeID, subclassID, bindType = GetItemInfo(itemlink)
        if bindType == 4 then return end  --任务物品不记录
        local lootMinQuality = (ZL.opt.lootMinQuality == -1 and GetLootThreshold() or ZL.opt.lootMinQuality)
        if quality < lootMinQuality then return end
        local itemID = GetItemInfoInstant(itemlink)
        bossName = bossName or "其他"
        local _bossName
        if stackCount > 1 then
            _bossName = "其他"
        else
            if itemID == 47242 then --北伐奖章放到 其他 里面
                _bossName = "其他"
            else
                _bossName = bossName
            end
        end
        if not ZL_CURRENT.lootList then ZL_CURRENT.lootList = {type="拾取记录"} end
        if not ZL_CURRENT.lootList[_bossName] then ZL_CURRENT.lootList[_bossName] = {} end
        if not ZL_CURRENT.lootList[_bossName][itemlink] then ZL_CURRENT.lootList[_bossName][itemlink] = {} end
        if not ZL_CURRENT.lootList[_bossName][itemlink][lootplayer] then ZL_CURRENT.lootList[_bossName][itemlink][lootplayer] = {} end
        ZL_CURRENT.lootList[_bossName][itemlink][lootplayer]["counts"] = (ZL_CURRENT.lootList[_bossName][itemlink][lootplayer]["counts"] or 0) + count
    end
end

local f = CreateFrame("Frame")
f:RegisterEvent("TRADE_ACCEPT_UPDATE")
f:RegisterEvent("TRADE_CLOSED")
f:RegisterEvent("MERCHANT_UPDATE")
f:RegisterEvent("QUEST_TURNED_IN")
f:RegisterEvent("QUEST_FINISHED")
f:RegisterEvent("PLAYER_REGEN_DISABLED")
f:RegisterEvent("ENCOUNTER_START")
f:RegisterEvent("ENCOUNTER_END")
f:RegisterEvent("BOSS_KILL")
f:RegisterEvent("CHAT_MSG_LOOT")
f:SetScript("OnEvent", lootMsg)
