return (function()

local md = require("debugFunctions")
local utils = require("utils")

md.printAlways("Initializing loadscreen tips...")

-- Load the config file.
local config = utils.loadConfig('tips.cfg')

local state = {}

local function mission()
  return mn.getMissionFilename():gsub('.fs2', '')
end

local function choices()
  local m = mission()
  local cfg = config[m] or {}
  if cfg.disable then
    return {{"", -1}}
  elseif m == state.reload then
    return cfg.reload or config.default
  else
    return cfg.override or config.default
  end
end

local function tip()
  if not state.tip then
    local c = choices()
    local i = ba.rand32(1, #c)
    local tip = c[i]
    state.tip = ba.replaceTokens(ba.XSTR(tip[1], tip[2]))
  end
  return state.tip
end

local function draw()
  local t = tip()
  gr.CurrentFont = gr.Fonts[config.font]
  gr.setColor(config.color[1], config.color[2], config.color[3], config.color[4])
  local x = gr.getScreenWidth() * config.position[1] 
  x = x - (gr.getStringWidth(t) * 0.5) -- Center it horizontally.
  local y = gr.getScreenHeight() * config.position[2]
  gr.drawString(t, x, y)
end

local function done()
  state.tip = nil
end

local function onMissionStart()
  state.tip = nil
  state.reload = mission()
end

return { draw = draw, done = done, onMissionStart = onMissionStart }

end)()
