local _, ZLAUTO = ...
local ZL = ZLAUTO.mainFrame

function ZL.Init_instanceInfo()
    ZL.instanceInfo = {
        -- TierList = {},
        -- JournalInstanceInfo = {},
        BossNameList = {},
        BossInfo = {},
        ItemSpecInfo = {},
        
    }
    -- local TierList = ZL.instanceInfo.TierList
    -- local JournalInstanceInfo = ZL.instanceInfo.JournalInstanceInfo
    local BossNameList = ZL.instanceInfo.BossNameList
    local BossInfo = ZL.instanceInfo.BossInfo
    local ItemSpecInfo = ZL.instanceInfo.ItemSpecInfo
    for i = 1, EJ_GetNumTiers()-1 do
        -- local tierName = EJ_GetTierInfo(i)
        EJ_SelectTier(i)
        local j = 1
        local journalInstanceID, instanceName, _, _, _, _, _, _, _, _, instanceID = EJ_GetInstanceByIndex(j, true)
        while journalInstanceID do
            EJ_SelectInstance(journalInstanceID)
            local k = 1
            local bossName, _, journalEncounterID = EJ_GetEncounterInfoByIndex(k, journalInstanceID)
            while bossName do
                local _, _, _, displayInfo, iconImage = EJ_GetCreatureInfo(1, journalEncounterID)
                if not BossInfo[bossName] then
                    tinsert(BossNameList, bossName)
                    BossInfo[bossName] = {}
                    -- BossInfo[bossName].displayInfo = displayInfo
                    BossInfo[bossName].iconImage = iconImage
                    -- BossInfo[bossName].journalEncounterID = journalEncounterID
                    -- BossInfo[bossName].instanceID = instanceID
                -- else
                --     print(instanceName .. "," .. bossName .. "|cffff0000已存在|r，journalEncounterID=" .. journalEncounterID .. ",displayInfo=" .. displayInfo.. ",iconImage=" .. iconImage .. "，instanceID=" .. instanceID)
                end
                k = k + 1
                bossName, _, journalEncounterID = EJ_GetEncounterInfoByIndex(k, journalInstanceID)
            end
            j = j + 1
            journalInstanceID, instanceName, _, _, _, _, _, _, _, _, instanceID = EJ_GetInstanceByIndex(j, true)
        end
    end

    C_EncounterJournal.SetSlotFilter(15) --全部部位
    EJ_SetDifficulty(16) --史诗难度
    local _, classID = UnitClassBase("player")
    local specializations = {} --0：所有专精
    for i = 1, GetNumSpecializations() do
        local specId = GetSpecializationInfo(i)
        if specId then
            tinsert(specializations, specId)
        end
    end
    -- specializations = {0}
    -- classID = 0
    -- ZL_CURRENT.itemtest = {}
    local function GetCurrentItemSpecInfo()
        wipe(ItemSpecInfo)
        -- wipe(ZL_CURRENT.itemtest)
        local i = EJ_GetNumTiers() - 1
        EJ_SelectTier(i)
        local itemInfo
        local j = 1
        local journalInstanceID, name = EJ_GetInstanceByIndex(j, true)
        while journalInstanceID do
            EJ_SelectInstance(journalInstanceID)
            for _, specId in ipairs(specializations) do
                EJ_SetLootFilter(classID, specId)
                for k = 1, EJ_GetNumLoot() do
                    itemInfo = C_EncounterJournal.GetLootInfoByIndex(k)
                    if itemInfo then
                        -- if itemInfo.name then
                        --     ZL_CURRENT.itemtest[itemInfo.name] = {itemInfo.itemID, itemInfo.encounterID}
                        -- end
                        ItemSpecInfo[itemInfo.itemID] = ItemSpecInfo[itemInfo.itemID] or {}
                        ItemSpecInfo[itemInfo.itemID][specId] = true
                    end
                end
            end
            j = j + 1
            journalInstanceID = EJ_GetInstanceByIndex(j, true)
        end
        -- EJ_SetDifficulty(23) --史诗难度
        -- EJ_SelectTier(13)
        -- j = 1
        -- journalInstanceID, name = EJ_GetInstanceByIndex(j, false)
        -- while journalInstanceID do
        --     EJ_SelectInstance(journalInstanceID)
        --     for _, specId in ipairs(specializations) do
        --         EJ_SetLootFilter(classID, specId)
        --         for k = 1, EJ_GetNumLoot() do
        --             itemInfo = C_EncounterJournal.GetLootInfoByIndex(k)
        --             if itemInfo and itemInfo.name then
        --                 if ZL_CURRENT.itemtest[itemInfo.name] and itemInfo.itemID ~= ZL_CURRENT.itemtest[itemInfo.name] then
        --                     print(itemInfo.name, itemInfo.itemID, ",ID重复：", ZL_CURRENT.itemtest[itemInfo.name])
        --                 else
        --                     ZL_CURRENT.itemtest[itemInfo.name] = {itemInfo.itemID, itemInfo.encounterID}
        --                 end
        --             end
        --         end
        --     end
        --     j = j + 1
        --     journalInstanceID, name = EJ_GetInstanceByIndex(j, false)
        -- end

    end
    GetCurrentItemSpecInfo()
    C_Timer.After(1, function ()
        GetCurrentItemSpecInfo()
        ZL.AuctionFrame2:Update()
    end)
end
