local GlobalAddonName, E = ...
----------------------------------------------------------------
-- /run opde(Enum.BagIndex)
-- [1] = {
----------------------------------------------------------------
--     ["Characterbanktab"] = -2,
--     ["CharacterBankTab_1"] = 6,
--     ["CharacterBankTab_6"] = 11,
--     ["CharacterBankTab_2"] = 7,
--     ["CharacterBankTab_3"] = 8,
--     ["CharacterBankTab_5"] = 10,
--     ["CharacterBankTab_4"] = 9,
----------------------------------------------------------------
--     ["Accountbanktab"] = -3,
--     ["AccountBankTab_1"] = 12,
--     ["AccountBankTab_2"] = 13,
--     ["AccountBankTab_3"] = 14,
--     ["AccountBankTab_4"] = 15,
--     ["AccountBankTab_5"] = 16,
----------------------------------------------------------------
--     ["Keyring"] = -1,
--     ["Backpack"] = 0,
--     ["Bag_1"] = 1,
--     ["Bag_2"] = 2,
--     ["Bag_3"] = 3,
--     ["Bag_4"] = 4,
--     ["ReagentBag"] = 5,
----------------------------------------------------------------
-- },
----------------------------------------------------------------
local OctoTable_PlayerBags = E.OctoTable_PlayerBags
local function Collect_Items_BAGS()
	----------------------------------------------------------------
	if not E:func_CanCollectData() then return end
	local collectMASLENGO = Octo_ToDo_DB_Levels[E.curGUID].MASLENGO
	local collectPlayerData = Octo_ToDo_DB_Levels[E.curGUID].PlayerData
	----------------------------------------------------------------
	if not collectPlayerData or not collectMASLENGO then return end
	local usedSlots_BAGS, totalSlots_BAGS = 0, 0
	local SL_Possible_Anima, SL_Possible_CatalogedResearch = 0, 0
	collectMASLENGO.Items = collectMASLENGO.Items or {}
	collectMASLENGO.Items.Bags = {}
	collectMASLENGO.Items.Bags_FULL = {}
	local OctoTable_itemID_ItemsUsable = E.OctoTable_itemID_ItemsUsable
	-- local seenSlots = {}
	for _, bagID in next, (OctoTable_PlayerBags) do
		-- for bagID = BACKPACK_CONTAINER, NUM_TOTAL_EQUIPPED_BAG_SLOTS do
		local numSlots = E.func_GetContainerNumSlots(bagID)
		if numSlots > 0 then
			totalSlots_BAGS = totalSlots_BAGS+numSlots
			local free, bagType = E.func_GetContainerNumFreeSlots(bagID)
			if bagType == 0 then
				usedSlots_BAGS = usedSlots_BAGS+(numSlots - free)
			end
			for slotIndex = 1, numSlots do
				-- local itemLocation = ItemLocation:CreateFromEquipmentSlot(slotIndex)
				local itemLocation = ItemLocation:CreateFromBagAndSlot(bagID, slotIndex)
				if itemLocation and C_Item.DoesItemExist(itemLocation) then
					local ItemName = C_Item.GetItemName(itemLocation)
					if ItemName then
						local current_Durability, maximum_Durability = GetInventoryItemDurability(slotIndex)
						local itemID = C_Item.GetItemID(itemLocation)
						local Icon = C_Item.GetItemIcon(itemLocation)
						local ItemLink = C_Item.GetItemLink(itemLocation)
						local Quality = C_Item.GetItemQuality(itemLocation)
						local CurrentItemLevel = C_Item.GetCurrentItemLevel(itemLocation)
						local ItemInventoryType = C_Item.GetItemInventoryType(itemLocation)
						local RequestLoadItemData = C_Item.RequestLoadItemData(itemLocation) -- or false
						collectMASLENGO.Items.Bags_FULL[itemID] = {
							itemID = itemID,
							ItemName = ItemName,
							Icon = Icon,
							ItemLink = ItemLink,
							Quality = Quality,
							CurrentItemLevel = CurrentItemLevel,
							ItemInventoryType = ItemInventoryType,
							RequestLoadItemData = RequestLoadItemData,
							-- ItemDataCached = ItemDataCached,
							current_Durability = current_Durability,
							maximum_Durability = maximum_Durability,
						}
					-- else
					-- 	-- данные не загружены, оставляем старые и помечаем для подгрузки
					-- 	local existing = collectMASLENGO.Items.Bags_FULL[itemID]
					-- 	if existing then
					-- 		existing.RequestLoadItemData = true
					-- 		seenSlots[slotID] = true
					-- 	end
					end
				end
				local info = E.func_GetContainerItemInfo(bagID, slotIndex)
				if info then
					local itemID = info.itemID
					local stack = info.stackCount or 1
					collectMASLENGO.Items.Bags[itemID] = (collectMASLENGO.Items.Bags[itemID] or 0)+stack
					-- Cataloged Research
					local researchValue = E.OctoTable_itemID_Cataloged_Research[itemID]
					if researchValue then
						SL_Possible_CatalogedResearch = SL_Possible_CatalogedResearch+(researchValue*stack)
					end
					if itemID == 169223 then
						local itemLocation = ItemLocation:CreateFromBagAndSlot(bagID, slotIndex)
						if itemLocation:IsValid() and C_Item.DoesItemExist(itemLocation) then
							local itemLink = C_Item.GetItemLink(itemLocation)
							local rank = E.GetItemRankFromLink(itemLink)
							collectPlayerData.cloak_lvl = rank
						end
					end
					-- Anima
					if E.func_IsAnimaItemByID(itemID) then
						local quality = info.quality
						if itemID == 183727 then
							SL_Possible_Anima = SL_Possible_Anima+(3*stack)
						elseif quality == 2 then
							SL_Possible_Anima = SL_Possible_Anima+(5*stack)
						elseif quality == 3 then
							SL_Possible_Anima = SL_Possible_Anima+(35*stack)
						elseif quality == 4 then
							SL_Possible_Anima = SL_Possible_Anima+(250*stack)
						end
					end
					-- Usable items cache
					if OctoTable_itemID_ItemsUsable and not OctoTable_itemID_ItemsUsable[itemID] then
						-- local _, _, _, _, _, itemType, itemSubType = E.func_GetItemInfo(itemID)
						local _, itemType, itemSubType = E.func_GetItemInfoInstant(itemID)
						if itemType == AUCTION_CATEGORY_HOUSING
						or itemType == BINDING_HEADER_HOUSING_SYSTEM
						or itemSubType == HOUSING_ITEM_TOAST_TYPE_DECOR
						or itemSubType == CATALOG_SHOP_TYPE_DECOR
						or itemSubType == MOUNTS then
							OctoTable_itemID_ItemsUsable[itemID] = 1
						end
					end
				end
			end
		end
	end
	-- wipe(collectMASLENGO.Items.Bags or {})
	collectPlayerData.SL_Possible_Anima = SL_Possible_Anima ~= 0 and SL_Possible_Anima or nil
	collectPlayerData.SL_Possible_CatalogedResearch = SL_Possible_CatalogedResearch ~= 0 and SL_Possible_CatalogedResearch or nil
	collectPlayerData.usedSlots_BAGS = usedSlots_BAGS
	collectPlayerData.totalSlots_BAGS = totalSlots_BAGS
	-- opde(Octo_ToDo_DB_Levels[E.curGUID].MASLENGO.Items)
end
----------------------------------------------------------------
function E.Collect_Items_BAGS()
	E.func_SpamBlock(Collect_Items_BAGS, true)
end