local PIbandicoot = {'PompsItems_Bandicoot.PICocoBandicootPlushie', 'PompsItems_Bandicoot.PICrashBandicootPlushie', 'PompsItems_Bandicoot.PINeoCortexPlushie', 'PompsItems_Bandicoot.PITawnaBandicootPlushie', 'PompsItems_Bandicoot.PI_AkuAku', 'PompsItems_Bandicoot.PIDingodilePlushie', 'PompsItems_Bandicoot.PINBrioPlushie', 'PompsItems_Bandicoot.PIRipperRooPlushie'}


---Replaces an item in a container with a random item from the item list.
---@param container ItemContainer
local function replaceBandicootBox(container)
    local dummies = container:getAllType("PompsItems_Bandicoot.PIBandicootBox") -- dummy item type
    for i = 0, dummies:size()-1 do
        container:Remove(dummies:get(i))
        local itemChoice = ZombRand(#PIbandicoot)+1
        local item = container:AddItem(PIbandicoot[itemChoice])
        sendAddItemToContainer(container, item)
    end
end

-- for items in containers
local function onFillContainer(_roomName, _containerType, container)
	--for some reason, the container object passed is sometimes not actually a container object
	--this is a quick and simple way to check (even if tostring maybe isnt the most optimal to call so often)
	if tostring(container):sub(1, 13) ~= "ItemContainer" then return end;
    replaceBandicootBox(container);
end

Events.OnFillContainer.Add(onFillContainer)


-- for items found on zombies
local function onRefreshInventoryWindowContainers(inventoryPage)
    if not inventoryPage.inventory or not instanceof(inventoryPage.inventory:getParent(), "IsoDeadBody") then return end
	--print("WindowContainerUpdate: ", inventoryPage.inventory);
    replaceBandicootBox(inventoryPage.inventory)
end


function SetBandicootDistributionData()
	print("-----------BandicootPlushieDistribution----------")
	-- Define a table to store the distribution data
	local BandicootDistributionData = {
		{ location = "BedroomDresserChild", weight = 1.0 },
		{ location = "BedroomSidetableChild", weight = 1.0 },
		{ location = "BedroomSidetable", weight = 0.2 },
		{ location = "CarnivalPrizes", weight = 5.0 },
		{ location = "CrateToys", weight = 3.4 },
		{ location = "DaycareCounter", weight = 0.8 },
		{ location = "DaycareShelves", weight = 0.8 },
		{ location = "DresserGeneric", weight = 0.2 },
		{ location = "Gifts", weight = 3.4 },
		{ location = "GiftStoreToys", weight = 4.0 },
		{ location = "GigamartToys", weight = 4.0 },
		{ location = "Hobbies", weight = 4.0 },
		{ location = "LivingRoomWardrobe", weight = 0.05 },
		{ location = "UniversityWardrobe", weight = 0.3 },
		{ location = "WardrobeChild", weight = 1.4 },
		{ location = "WardrobeGeneric", weight = 0.1 },
	}

	-- Loop through the data and insert items
	for _, data in ipairs(BandicootDistributionData) do
		if ProceduralDistributions["list"][data.location] then
			local list = ProceduralDistributions["list"][data.location].items
			table.insert(list, "PompsItems_Bandicoot.PIBandicootBox")
			table.insert(list, data.weight * SandboxVars.PompsItems.BandicootSpawnRate)
		else
			print("Warning: No items list found for location " .. data.location)
		end
	end
	
		
	local PonyZombieDistributionData = {
		{ location = "inventorymale", weight = 0.5 },
		{ location = "inventoryfemale", weight = 0.5 },
	}

	for _, data in ipairs(PonyZombieDistributionData) do
		if SuburbsDistributions["all"][data.location] then
			local list = SuburbsDistributions["all"][data.location].items
			table.insert(list, "PompsItems_Bandicoot.PIBandicootBox")
			table.insert(list, data.weight * SandboxVars.PompsItems.BandicootZombieSpawnRate)
		else
			print("Warning: No items list found for location " .. data.location)
		end
	end
	
	--idk what this is, i saw another mod do it.
	ItemPickerJava.Parse()
end

Events.OnInitGlobalModData.Add(SetBandicootDistributionData)