setDefaultTab("Tools")

UI.Separator()
macro(10000, "Anti Kick",  function()
  local dir = player:getDirection()
  turn((dir + 1) % 4)
  turn(dir)
end)

UI.Separator()
UI.Label("Auto Follow Name")
addTextEdit("followleader", storage.followLeader or "player name", function(widget, text)
storage.followLeader = text
end)
--Code
local toFollowPos = {}
local followMacro = macro(20, "Follow", function()
local target = getCreatureByName(storage.followLeader)
if target then
local tpos = target:getPosition()
toFollowPos[tpos.z] = tpos
end
if player:isWalking() then return end
local p = toFollowPos[posz()]
if not p then return end
if autoWalk(p, 20, {ignoreNonPathable=true, precision=1}) then
delay(100)
end
end)
onCreaturePositionChange(function(creature, oldPos, newPos)
if creature:getName() == storage.followLeader then
toFollowPos[newPos.z] = newPos
end
end)

UI.Separator()

local bixos = macro(1, "Atacar bixos", function() 
  local battlelist = getSpectators();
  local closest = 12
  local lowesthpc = 101
  for key, val in pairs(battlelist) do
    if val:isMonster() then
      if getDistanceBetween(player:getPosition(), val:getPosition()) <= closest then
        closest = getDistanceBetween(player:getPosition(), val:getPosition())
        if val:getHealthPercent() < lowesthpc then
          lowesthpc = val:getHealthPercent()
        end
      end
    end
  end
  for key, val in pairs(battlelist) do
    if val:isMonster() then
      if getDistanceBetween(player:getPosition(), val:getPosition()) <= closest then
        if g_game.getAttackingCreature() ~= val and val:getHealthPercent() <= lowesthpc then 
          g_game.attack(val)
	delay(100)
          break
        end
      end
    end
  end
end)
addIcon("bixos", {item={id = 5972, count = 1}, text="Atacar Bixos"}, function(icon, isOn)
  bixos.setOn(isOn)
end)

UI.Separator()
local attacker
local targetTime

local attackPK = macro(100, "Anti PK - Attack", nil, function()
  if attacker then
    if attacker:getPosition() and attacker:getPosition().z == posz() then
      if g_game.isAttacking() then
        if g_game.getAttackingCreature():getName() ~= attacker:getName() then
          g_game.attack(attacker)
        end
      else
        g_game.attack(attacker)
      end
    end
  else
    if not g_game.isAttacking() then
      TargetBot.setOn()
      g_game.setSafeFight(0) return
    end
  end
  if targetTime then
    if now - targetTime > 2500 then
      attacker = nil
    end
  end
end)

onMissle(function(missle)
  if attackPK.isOn() then
    local src = missle:getSource()
    if src.z ~= posz() then
      return
    end
    local shooterTile = g_map.getTile(src)
    if shooterTile then
      local creatures = shooterTile:getCreatures()
      if creatures[1] then
        if creatures[1]:isPlayer() then
          local destination = missle:getDestination()
          if posx() == destination.x and posy() == destination.y then
            if player:getName() ~= creatures[1]:getName() then
              if creatures[1]:getSkull() ~= 0 and attacker ~= creatures[1] then
                attacker = creatures[1]
                targetTime = now
                TargetBot.setOff()
                g_game.setSafeFight() return
              end
            end
          end
        end
      end
    end
  end
end)

onMissle(function(missle)
  if attackPK.isOn() then
    local src = missle:getSource()
    if src.z ~= posz() then
      return
    end
    local shooterTile = g_map.getTile(src)
    if shooterTile then
      local creatures = shooterTile:getCreatures()
      if creatures[1] then
        if creatures[1]:isPlayer() then
          local destination = missle:getDestination()
          if posx() == destination.x and posy() == destination.y then
            if player:getName() ~= creatures[1]:getName() then
              if creatures[1]:getSkull() ~= 0 and attacker ~= creatures[1] then
                attacker = creatures[1]
                targetTime = now
                TargetBot.setOff()
                g_game.setSafeFight() return
              end
            end
          end
        end
      end
    end
  end
end)

onMissle(function(missle)
  if attackPK.isOn() then
    local src = missle:getSource()
    if src.z ~= posz() then
      return
    end
    local shooterTile = g_map.getTile(src)
    if shooterTile then
      local creatures = shooterTile:getCreatures()
      if creatures[1] then
        if creatures[1]:isPlayer() then
          local destination = missle:getDestination()
          if posx() == destination.x and posy() == destination.y then
            if player:getName() ~= creatures[1]:getName() then
              if creatures[1]:getSkull() ~= 0 and attacker ~= creatures[1] then
                attacker = creatures[1]
                targetTime = now
                TargetBot.setOff()
                g_game.setSafeFight() return
              end
            end
          end
        end
      end
    end
  end
end)

UI.Separator()

UI.Label("Buff Spell:")
UI.TextEdit(storage.hasPartyBuff or "utito tempo san", function(widget, newText)
  storage.hasPartyBuff = newText
end)

macro(250, "Buff Spell", function()
  if not hasPartyBuff() then
    say(storage.hasPartyBuff)
  end
end)
UI.Separator()
UI.TextEdit(storage.buff1 or "utana vid", function(widget, newText)
  storage.buff1 = newText
end)

macro(250, "Mana Training 2", function()
    say(storage.buff1)
end)

UI.Separator()
UI.TextEdit(storage.buff2 or "utana vid", function(widget, newText)
  storage.buff2 = newText
end)

macro(250, "Mana Training 3", function()
    say(storage.buff2)
end)
UI.Separator()

UI.Separator()
local itemList = { 3364, 3366, 3414, 3420, 3392, 3386, 3079, 3360, 3436} -- item id here
--Make By Feetads
macro(1000, "Sell with spellwand", function()
    for _, container in pairs(g_game.getContainers()) do
        for _, item in ipairs(container:getItems()) do
            if table.find(itemList, item:getId()) then
                useWith(789, item) -- spellwand id
            end
        end
    end
    say("!depositall")

end)


UI.Separator()
function staminaItems(parent)
  if not parent then
    parent = panel
  end
  local panelName = "staminaItemsUser"
  local ui = setupUI([[
Panel
  height: 65
  margin-top: 2

  SmallBotSwitch
    id: title
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.top: parent.top
    text-align: center

  HorizontalScrollBar
    id: scroll1
    anchors.left: parent.left
    anchors.right: parent.horizontalCenter
    anchors.top: title.bottom
    margin-right: 2
    margin-top: 2
    minimum: 0
    maximum: 42
    step: 1
    
  HorizontalScrollBar
    id: scroll2
    anchors.left: parent.horizontalCenter
    anchors.right: parent.right
    anchors.top: prev.top
    margin-left: 2
    minimum: 0
    maximum: 42
    step: 1    

  ItemsRow
    id: items
    anchors.left: parent.left
    anchors.right: parent.right
    anchors.top: prev.bottom
  ]], parent)
  ui:setId(panelName)

  if not storage[panelName] then
    storage[panelName] = {
      min = 0,
      max = 40,
    }
  end

  local updateText = function()
    ui.title:setText("" .. storage[panelName].min .. " <= Stamina >= " .. storage[panelName].max .. "")  
  end
 
  ui.scroll1.onValueChange = function(scroll, value)
    storage[panelName].min = value
    updateText()
  end
  ui.scroll2.onValueChange = function(scroll, value)
    storage[panelName].max = value
    updateText()
  end
 
  ui.scroll1:setValue(storage[panelName].min)
  ui.scroll2:setValue(storage[panelName].max)
 
  ui.title:setOn(storage[panelName].enabled)
  ui.title.onClick = function(widget)
    storage[panelName].enabled = not storage[panelName].enabled
    widget:setOn(storage[panelName].enabled)
  end
 
  if type(storage[panelName].items) ~= 'table' then
    storage[panelName].items = { 11588 }
  end
 
  for i=1,5 do
    ui.items:getChildByIndex(i).onItemChange = function(widget)
      storage[panelName].items[i] = widget:getItemId()
    end
    ui.items:getChildByIndex(i):setItemId(storage[panelName].items[i])    
  end
 
  macro(500, function()
    if not storage[panelName].enabled or stamina() / 60 < storage[panelName].min or stamina() / 60 > storage[panelName].max then
      return
    end
    local candidates = {}
    for i, item in pairs(storage[panelName].items) do
      if item >= 100 then
        table.insert(candidates, item)
      end
    end
    if #candidates == 0 then
      return
    end    
    use(candidates[math.random(1, #candidates)])
  end)
end
staminaItems(toolsTab)
