-- CONFIG - NewTibia Cloud v10.98 (TFS 1.4.2 directo en puerto 7171)
APP_NAME = "newtibiacloud"
APP_VERSION = 1098
DEFAULT_LAYOUT = "retro"

-- Services deshabilitados (sin updater externo, sin tracker)
Services = {
  website = "",
  updater = "",
  stats = "",
  crash = "",
  feedback = "",
  status = ""
}

-- Lista de servidores visibles en el selector visual
-- Formato: "host:port:version" (version = 10.98 -> 1098)
Servers = {
  NewTibia_Cloud = "tibia.fanstia.eu:7171:1098"
}

-- Conexion por defecto al arrancar el cliente
Server = "tibia.fanstia.eu:7171:1098"

-- Permitir agregar servidores custom desde la UI
ALLOW_CUSTOM_SERVERS = true

g_app.setName("NewTibiaCloud")
-- CONFIG END

g_logger.info(os.date("== NewTibia Cloud started at %b %d %Y %X"))

if not g_resources.directoryExists("/data") then
  g_logger.fatal("Data dir doesn't exist.")
end
if not g_resources.directoryExists("/modules") then
  g_logger.fatal("Modules dir doesn't exist.")
end

g_configs.loadSettings("/config.otml")

local settings = g_configs.getSettings()
local layout = DEFAULT_LAYOUT
if g_app.isMobile() then
  layout = "mobile"
elseif settings:exists('layout') then
  layout = settings:getValue('layout')
end
g_resources.setLayout(layout)

g_modules.discoverModules()
g_modules.ensureModuleLoaded("corelib")

local function loadModules()
  g_modules.autoLoadModules(99)
  g_modules.ensureModuleLoaded("gamelib")
  g_modules.autoLoadModules(499)
  g_modules.ensureModuleLoaded("client")
  g_modules.autoLoadModules(999)
  g_modules.ensureModuleLoaded("game_interface")
  g_modules.autoLoadModules(9999)
  -- Step 11: disabled while TibiaAgentControl is the canonical Phase 3 runner.
  -- g_modules.ensureModuleLoaded("game_phase3bot")
end

loadModules()

-- Auto-conectar con credenciales de prueba si estan definidas en el entorno
if _G.NewTibia_AutoLogin then
  scheduleEvent(function()
    if EnterGame and EnterGame.setAccountName then
      EnterGame.setAccountName(_G.NewTibia_AutoLogin)
      EnterGame.setAccountPassword(_G.NewTibia_AutoPassword or "")
      scheduleEvent(function() EnterGame.connect() end, 1500)
    end
  end, 3000)
end

-- Optional local QA module. Never auto-load it in the normal player client.
if _G.EnableTibiaAgentControl and g_resources.directoryExists("/modules/game_tibiaagent_control") then
  g_modules.ensureModuleLoaded("game_tibiaagent_control")
end

