setDefaultTab("Tools")

local targetID = nil

-- escape when attacking will reset hold target
onKeyPress(function(keys)
    if keys == "Escape" and targetID then
        targetID = nil
    end
end)

macro(100, "Hold Target", function()
    -- if attacking then save it as target, but check pos z in case of marking by mistake on other floor
    if target() and target():getPosition().z == posz() and not target():isNpc() then
        targetID = target():getId()
    elseif not target() then
        -- there is no saved data, do nothing
        if not targetID then return end

        -- look for target
        for i, spec in ipairs(getSpectators()) do
            local sameFloor = spec:getPosition().z == posz()
            local oldTarget = spec:getId() == targetID
            
            if sameFloor and oldTarget then
                attack(spec)
            end
        end
    end
end) 

macro(200, "Inv Creatures", nil, function()
    for _, n in ipairs(getSpectators(false)) do
        if n:isMonster() and n:isInvisible() then
            local outfit = n:getOutfit()
            n:setOutfit(outfit)
        end
    end
end)

macro(250, "Pick-Up", function()
  local tiles = getNearTiles(pos())
  for _, tile in ipairs(tiles) do
    for _, item in ipairs(tile:getItems()) do
      if item:isPickupable() then
        if g_game.move(item, {x=65535, y=SlotBack, z=0}, item:getCount()) then
          delay(100)
        end
      end
    end
  end
end)

function reopenContainers()
  containers = getContainers()
  if #containers < 1 and containers[0] == nil then
    bpItem = getBack()
    if bpItem ~= nil then
      g_game.open(bpItem)
    end
  end
end

-- Adicione a chamada para a função no final do seu script principal
reopenContainers()

local resetCursorIcon = nil

resetCursorIcon = addIcon("resetCursorIcon", {item={id=5031, count=1}, text="Reset Cursor"}, macro(50, function(m)
  g_mouse.popCursor('target')
  m.setOff()
  schedule(50, function()
    resetCursorIcon.text:setColor("yellow")
  end)
  modules.game_textmessage.displayGameMessage("Cursor reseted :)")
end))

resetCursorIcon.text:setColor("yellow")

macro(5 * 60 * 1000, function()
  modules.game_bot.save()
end)

macro(1000, "Limpar Textos", function()
    modules.game_textmessage.clearMessages()
    g_map.cleanTexts()
end)
UI.Separator()

UI.Separator()
UI.Label("[Mana Treiner]")
if type(storage.manaTrain) ~= "table" then
  storage.manaTrain = {on=false, title="MP%", text="utevo lux", min=80, max=100}
end
local manatrainmacro = macro(1000, function()
  if TargetBot and TargetBot.isActive() then return end -- pause when attacking
  local mana = math.min(100, math.floor(100 * (player:getMana() / player:getMaxMana())))
  if storage.manaTrain.max >= mana and mana >= storage.manaTrain.min then
    say(storage.manaTrain.text)
  end
end)
manatrainmacro.setOn(storage.manaTrain.on)
UI.DualScrollPanel(storage.manaTrain, function(widget, newParams) 
  storage.manaTrain = newParams
  manatrainmacro.setOn(storage.manaTrain.on)
end)