local FONT_HGT_SMALL = getTextManager():getFontHeight(UIFont.Small)

function ISConfigureContainerWindow:initList()
	self.items = getAllItems();

	local top = self:titleBarHeight() + 5

	local btnHgt = math.max(25, FONT_HGT_SMALL + 3 * 2)
	local padBottom = 10
	self.advPanel = ISTabPanel:new(self.simpleViewWidth + 10, top, self.advanceViewWidth -10 * 2, self:getHeight() - padBottom - btnHgt - padBottom - top)
	self.advPanel:initialise()
	self.advPanel.borderColor = { r = 0, g = 0, b = 0, a = 0}
	self.advPanel.target = self
	self.advPanel.equalTabWidth = false
	self.advPanel.onActivateView = self.onTabActivated
	self.advPanel.target = self
	self:addChild(self.advPanel)

	-- we gonna separate items by module
	self.module = {}
	local moduleNames = {}
	local allItems = {}
	local itemDictionary = {}
	for i=0,self.items:size()-1 do
			local item = self.items:get(i)
			--The following code is used to generate a list of all items in the game
			--in a format that allows for easier conversion into an excel / google sheets
			--compatible layout. IN THE OUTPUT, replace <<<>>> with a tab.

			--if (item:getDisplayCategory() ~= nil) then
			--    print("<<<>>>" .. item:getName() .. "<<<>>>" .. item:getDisplayCategory())
			--else
			--    print("<<<>>>" .. item:getName() .. "<<<>>>")
			--end

			--The above code activates as soon as the item list viewer is activated.
			if not item:getObsolete() and not item:isHidden() then
					if not self.module[item:getModuleName()] then
							self.module[item:getModuleName()] = {}
							table.insert(moduleNames, item:getModuleName())
					end
					table.insert(self.module[item:getModuleName()], item);
					table.insert(allItems, item)
					itemDictionary[self:getItemID(item)] = item
			end
	end

	self.itemDictionary = itemDictionary;

	table.sort(moduleNames, function(a,b) return not string.sort(a, b) end)

	self.module["Container"] = {}
	table.insert(moduleNames, 1, "Container")

	self.module["Exclude"] = {}
	table.insert(moduleNames, 1, "Exclude")

	self.module["Include"] = {}
	table.insert(moduleNames, 1, "Include")

	local listBox = ISItemsIncludeExclude:new(0, 0, self.advPanel.width, self.advPanel.height - self.advPanel.tabHeight, self)
	self.advPanel:addView("All", listBox)
	listBox:initialise()
	listBox:initList(allItems)

	for _,moduleName in ipairs(moduleNames) do
			-- we ignore the "Moveables" module
			if moduleName ~= "Moveables" then
					local cat1 = ISItemsIncludeExclude:new(0, 0, self.advPanel.width, self.advPanel.height - self.advPanel.tabHeight, self)
					cat1:initialise()
					self.advPanel:addView(moduleName, cat1)
					cat1:initList(self.module[moduleName])
			end
	end
	self.advPanel:activateView("All");

	local test = self.advPanel:getView("Container").datas.items
end

function ISConfigureContainerWindow:getItemID(item)
    return item:getModuleName() .. "." .. item:getName()
end

function ISConfigureContainerWindow.onTabActivated(containerWindow, tabPanel)
	-- With tabPanel we can determine which view is selected and to clear/add items
	-- With containerWindow we can access the inventory of the container
	
	if tabPanel.activeView.name == "Container" then
		local listedItemIds = {}
		tabPanel.activeView.view.datas:clear()

		local activeIndex = containerWindow.panel:getActiveViewIndex()
		local containerItems = containerWindow.containers:get(activeIndex-1):getItems()
		for i=0, containerItems:size()-1 do
			local item = containerItems:get(i);
			local scriptItem = item:getScriptItem()
			local scriptItemID = scriptItem:getModuleName() .. "." .. scriptItem:getName()

			if listedItemIds[scriptItemID] == nil then
				listedItemIds[scriptItemID] = true
				tabPanel.activeView.view.datas:addItem(scriptItem.name, scriptItem)
			end
		end
	end
end