require "ISUI/InventoryWindow/ISInventoryWindowControlHandler"
require "ISUI/InventoryWindow/ISInventoryWindowContainerControls"

ISInventoryWindowControlHandler_TransferAssignedContainers = ISInventoryWindowControlHandler:derive("ISInventoryWindowControlHandler_TransferAssignedContainers")
local Handler = ISInventoryWindowControlHandler_TransferAssignedContainers

function Handler:shouldBeVisible()
    if getCore():getGameMode() == "Tutorial" then return false end
    local playerObj = getSpecificPlayer(self.playerNum)
    if not ISInventoryPaneContextMenu.ValidNearbyContainers(playerObj) then return false end
    return not self.container:isEmpty()
end

function Handler:getControl()
    self.control = self:getImageButtonControl("media/ui/inventoryPanes/TransferToAssignedContainers.png")
    self.control:setTooltip(getText("ContextMenu_Put_Assigned_Containers"))
    self.control:setOnMouseOverFunction(self.onMouseOverButton)
    self.control:setOnMouseOutFunction(self.onMouseOutButton)
    return self.control
end

function Handler:perform()
    if isGamePaused() then return end
    ISInventoryPaneContextMenu.TransferItemsByMode(self.playerNum, "all")
end

function Handler:new()
    local o = ISInventoryWindowControlHandler.new(self)
    return o
end

-- Register the handler so it appears in the inventory controls
ISInventoryWindowContainerControls.AddHandler(ISInventoryWindowControlHandler_TransferAssignedContainers)
