-- load all otui files, order doesn't matter
local configName = modules.game_bot.contentsPanel.config:getCurrentOption().text

local configFiles = g_resources.listDirectoryFiles("/bot/" .. configName .. "/vBot", true, false)
for i, file in ipairs(configFiles) do
  local ext = file:split(".")
  if ext[#ext]:lower() == "ui" or ext[#ext]:lower() == "otui" then
    g_ui.importStyle(file)
  end
end

local function loadScript(name)
  return dofile("/vBot/" .. name .. ".lua")
end
setDefaultTab("Main")
setDefaultTab("HP")
setDefaultTab("Cave")
setDefaultTab("Target")
setDefaultTab("Tools")
setDefaultTab("Main")
-- here you can set manually order of scripts
-- libraries should be loaded first
local luaFiles = {
  "main",
  "items",
  "vlib",
  "new_cavebot_lib",
  "configs", -- do not change this and above
  -- "extras",
  "cavebot",
  "playerlist",
  -- "BotServer",
  "alarms",
  "Conditions",
  "Equipper",
  "pushmax",
  -- "combo",
  "HealBot",
  -- "new_healer",
  "AttackBot", -- last of major modules
  "ingame_editor",
  "Dropper",
  "Containers",
  "quiver_manager",
  "quiver_label",
  "tools",
  -- "antiRs",
  -- "depot_withdraw",
  "eat_food",
  "equip",
  "exeta",
  "analyzer",
  "spy_level",
  "supplies",
  "depositer_config",
  "npc_talk",
  "xeno_menu",
  "hold_target",
  "cavebot_control_panel"
}

for i, file in ipairs(luaFiles) do
  loadScript(file)
end

setDefaultTab("Main")
UI.Separator()
UI.Label("Private Scripts:")
-- addIcon("dancing", {outfit={mount=0,feet=0,legs=0,body=176,type=128,auxType=0,addons=6,head=48}, text="ALTO"},
UI.Label("Market Doll Sells"):setColor("green")

-- Quantidade mínima do item para começar a vender
local minCount = 0

-- Garante que a configuração exista
storage.itemIdSell = storage.itemIdSell or 11100

-- Campo para editar o ID do Doll
UI.Label("ID do Doll atual:")
local dollIdEdit = UI.TextEdit(tostring(storage.itemIdSell), function(widget, text)
  local newId = tonumber(text)
  if newId then
    storage.itemIdSell = newId
    dollIdEdit:setText(tostring(storage.itemIdSell)) -- força mostrar o novo valor
  end
end)

-- Container de itens a vender
if type(storage.sellItems) ~= "table" then storage.sellItems = {} end
local sellContainer = UI.Container(function(widget, items) 
  storage.sellItems = items 
end, true)
sellContainer:setItems(storage.sellItems)

macro(500, "Market Doll Sells", function()
  if not storage.sellItems[1] then return end
  local itemCounts = {}

  for _, container in pairs(g_game.getContainers()) do
    for _, item in ipairs(container:getItems()) do
      for _, venderItem in ipairs(storage.sellItems) do
        if (not item:isStackable() or item:getCount() >= 1)
            and item:getId() == venderItem.id then
          itemCounts[item:getId()] = (itemCounts[item:getId()] or 0) + item:getCount()
        end
      end
    end
  end

  local maxCount, itemToSell = 0, nil
  for itemId, count in pairs(itemCounts) do
    if count > maxCount and count >= minCount then
      maxCount = count
      itemToSell = itemId
    end
  end

  if itemToSell then
    for _, container in pairs(g_game.getContainers()) do
      for _, item in ipairs(container:getItems()) do
        if item:getId() == itemToSell then
          return usewith(storage.itemIdSell, item)
        end
      end
    end
  end
end)
UI.Separator()
