function ISConfigureContainerWindow:showSavePopup()
	local saveText = self.textBoxName:getText()
	if saveText == "" then
		saveText = self.containerPresetDropdown.options[self.containerPresetDropdown.selected] or ""
	end
	
	self.inputModal = ISConfigureContainerWindow.saveModal(true, nil, nil, nil, nil, saveText, ISConfigureContainerWindow.savePreset, self);
	self.inputModal.backgroundColor.a = 0.9

	local joypadData = JoypadState.getMainMenuJoypad() or CoopCharacterCreation.getJoypad()
	if joypadData then
		self.inputModal.param1 = joypadData
		joypadData.focus = self.inputModal
		updateJoypadFocus(joypadData)
	end
end

function ISConfigureContainerWindow.saveModal(_centered, _width, _height, _posX, _posY, _text, _onclick, target, param1, param2) -- {{{
	-- based on luautils.okModal
	local posX = _posX or 0;
	local posY = _posY or 0;
	local width = _width or 230;
	local height = _height or 120;
	local centered = _centered;
	local txt = _text;
	local core = getCore();

	-- center the modal if necessary
	if centered then
		posX = core:getScreenWidth() * 0.5 - width * 0.5;
		posY = core:getScreenHeight() * 0.5 - height * 0.5;
	end

	-- ISModalDialog:new(x, y, width, height, text, yesno, target, onclick, player, param1, param2)
	local modal = ISTextBox:new(posX, posY, width, height, getText("UI_characreation_BuildSavePrompt"), _text or "", target, _onclick, param1, param2);
	modal:initialise();
	modal:setAlwaysOnTop(true)
	modal:setCapture(true)
	modal:addToUIManager();
	modal.yes:setTitle(getText("UI_btn_save"))
	modal.entry:focus()
	modal:setValidateFunction(self, ISConfigureContainerWindow.validatePresetName)

	return modal;
end

function ISConfigureContainerWindow.validatePresetName(target, text)
	return #text > 0 and #text <= 200 and ContainerPreset.isValidPresetName(text)
end

function ISConfigureContainerWindow:deletePresetConfirmation()
	local delBuild = self.containerPresetDropdown.options[self.containerPresetDropdown.selected]

	-- Nothing to delete
	if delBuild == nil then return end

	local screenW = getCore():getScreenWidth()
	local screenH = getCore():getScreenHeight()
	local modal = ISModalDialog:new((screenW - 230) / 2, (screenH - 120) / 2, 230, 120, getText("UI_characreation_BuildDeletePrompt", delBuild), true, self, ISConfigureContainerWindow.deletePreset);
	modal.backgroundColor.a = 0.9
	modal:initialise()
	modal:setCapture(true)
	modal:addToUIManager()
	modal:setAlwaysOnTop(true)
	if self.panel.joyfocus then
		modal.param1 = self.panel.joyfocus
		self.panel.joyfocus.focus = modal
		updateJoypadFocus(self.panel.joyfocus)
	end
end

function ISConfigureContainerWindow:savePreset(button)
	if button.internal == "CANCEL" then return end
	--print("Saving under:"..self.inputModal.entry:getText())

    local filterData = {}
	local includeData = {}
	local excludeData = {}

	for _,selCat in ipairs(self.categoryListBox:getSelectedItems()) do
		table.insert(filterData, selCat.item)
	end

	for itemName,_ in pairs(self.exclusiveSelection.Include) do
		table.insert(includeData, itemName)
	end

	for itemName,_ in pairs(self.exclusiveSelection.Exclude) do
		table.insert(excludeData, itemName)
	end

	local containerPresetData = {
		containersFilters = filterData,
		Include=includeData,
		Exclude=excludeData,
		containerName = self.textBoxName:getText(),
		advanceView= self.toggleAdvance:isSelected(1)
	}

	local presetString = ContainerPreset:serializedContainerPreset(containerPresetData)
	ContainerPreset:writePreset(self.inputModal.entry:getText(), self.inputModal.entry:getText() .. "=" .. presetString)

	self:refreshPresets()
end

function ISConfigureContainerWindow:loadPreset()
	--print("selecting:"..self.containerPresetDropdown:getSelectedText())
	local presetData = ContainerPreset:loadPreset(self.containerPresetDropdown:getSelectedText())

	if presetData == nil then
        print("Failed to load preset:", self.containerPresetDropdown:getSelectedText())
        return
    end

    -- Clear existing selection

	local includeItems = {}
	local excludeItems = {}

	for i, itemName in ipairs(presetData.Include) do
		table.insert(includeItems, {item=self.itemDictionary[itemName]})
	end

	for i, itemName in ipairs(presetData.Exclude) do
		table.insert(excludeItems, {item=self.itemDictionary[itemName]})
	end

	local activeView = self.advPanel:getActiveView()

	-- Clear existing selection
	activeView:clearSelection("Include")
	activeView:clearSelection("Exclude")

	-- Add loaded selection
	activeView:applySelection(includeItems, "Include")
	activeView:applySelection(excludeItems, "Exclude")

	self.categoryListBox:setSelectedByValues(presetData.containersFilters)
	self.textBoxName:setText(presetData.containerName)

	-- Might change my mind later, but for now i dont see the reason behind setting the adv mode
	--local stringtoboolean={ ["true"]=true, ["false"]=false }
	--local advView = stringtoboolean[presetData.advanceView]
	--self.toggleAdvance:setSelected(1, advView)
	--self:onToggleAdvance(1, advView)

	--print("preset container name:"..presetData.containerName)
end

function ISConfigureContainerWindow:deletePreset(button)
	if button.internal == "NO" then return end

	--print("deleting:"..self.containerPresetDropdown:getSelectedText())
	ContainerPreset:writePreset(self.containerPresetDropdown:getSelectedText(), "")
	table.remove(self.containerPresetDropdown.options, self.containerPresetDropdown.selected)
	self.containerPresetDropdown.selected = 0;

	self:refreshPresets()
end

function ISConfigureContainerWindow:refreshPresets()
	self.containerPresetDropdown:clear()
	local saved_containerPresets = ContainerPreset:getPresets()
	for i,preset in ipairs(saved_containerPresets) do
		self.containerPresetDropdown:addOption(preset)
	end
end