-- FF_STORY_COMPUTER_GUARD
local function ff_story_computer_allowed()
  local label = os.getComputerLabel and os.getComputerLabel() or nil
  return fs.exists(".ff_story_computer")
    or fs.exists(".ff_titan_computer")
    or fs.exists(".ff_titan_program_id")
    or fs.exists(".ff_titan_allow_autoinstall")
    or fs.exists(".ff_titan_allow_autorun")
    or (label ~= nil and string.sub(string.lower(label), 1, 9) == "ff_titan:")
end

if not ff_story_computer_allowed() then
  return
end
-- TITAN SURFACE TERMINAL
-- NODE: DESCENT ELEVATOR CONTROL
-- SAVE AS: startup

local NODE_TITLE = "DESCENT CONTROL"
local NODE_SUBTITLE = "SURFACE ELEVATOR"

local entries = {
  {
    title = "STATUS",
    body = {
      "ELEVATOR CAGE",
      
      "Pulley responds.",
      "Motor line dead.",
      
      "Coupler removed.",
      
      "Reason entered:",
      "prevent misuse.",
      
      "Reason spoken aloud:",
      "it came back wrong.",
    }
  },
  {
    title = "REPAIR",
    body = {
      "RESTORE DESCENT",
      
      "Recover drive coupler.",
      "Reconnect motor line.",
      "Release brake after rotation test.",
      "Route only to green level.",
      
      "Lower route is blocked here.",
      
      "If the lower button appears,",
      "turn around.",
      
      "There is no lower button",
      "on this console."
    }
  },
  {
    title = "DAAN",
    body = {
      "FINAL MAINTENANCE TICKET",
      
      "Submitted by:",
      "Daan V.",
      
      "Text:",
      
      "The elevator keeps arriving",
      "before I finish calling it.",
      
      "That means either the delay is broken",
      "or someone below is being impatient.",
      "Stop spamming the elevator and wait."
    }
  },
  {
    title = "BAD RETURN",
    body = {
      "INCIDENT G-12",
      
      "Cage arrived at surface.",
      
      "Interior lights set to green.",
      "Floor dry.",
      "Ceiling wet.",
      
      "One badge inside.",
      
      "Badge owner was on surface",
      "at the time.",
      
      "Owner resigned mid-interview."
    }
  },
  {
    title = "AUDIO",
    body = {
      "DESCENT MICROPHONE",
      
      "Expected sound:",
      "metal under pressure.",
      
      "Report sound:",
      "creaking through shaft wall.",
      
      "Albedo calls this rule",
      "\"rarely followed.\""
    }
  },
  {
    title = "DEPTH",
    body = {
      "COUNTER PATCH NOTE",
      
      "Exact depth display disabled.",
      
      "Cause:",
      "staff betting pool.",
      
      "Actual cause:",
      "counter displayed a floor",
      "below lower hull.",
      
      "There is no floor there",
      "on approved maps?"
    }
  },
  {
    title = "ALBEDO LINE",
    body = {
      "DESCENT ANNOUNCEMENT",
      
      "Albedo approved version:",
      
      "\"Descent confirmed.\"",
      
      "\"Surface signal will fade shortly.\"",
      
      "\"If you hear screaming,",
      "then it is usually metal.\"",
      
      "\"Do not be afraid.\""
    }
  },
  {
    title = "GREEN LOCK",
    body = {
      "ROUTE MESSAGE",
      
      "Green level accepts first arrival.",
      
      "Lower level rejects surface route.",
      
      "Displayed reason:",
      "pressure verification.",
      
      "Internal reason:",
      "the lower route called back",
      "using an old staff voice.",
      
      "No staff name matched."
    }
  }
}

local function setColor(c)
  if term.isColor and term.isColor() then term.setTextColor(c) end
end

local function clear()
  term.setBackgroundColor(colors.black)
  term.clear()
  term.setCursorPos(1, 1)
end

local function header(title)
  clear()
  setColor(colors.lightBlue)
  print("[ " .. NODE_TITLE .. " ]")
  setColor(colors.gray)
  print(NODE_SUBTITLE)
  print("---------------------------------------")
  if title then
    setColor(colors.white)
    print(title)
    setColor(colors.gray)
    print("---------------------------------------")
  end
  setColor(colors.white)
end

local function draw(line)
  if line:find("ALBEDO") or line:find("Albedo") then
    setColor(colors.cyan)
  elseif line:find("Do not") or line:find("blocked") or line:find("rejects") then
    setColor(colors.orange)
  elseif line:find("lower") or line:find("Lower") or line:find("wrong") then
    setColor(colors.red)
  else
    setColor(colors.white)
  end
  print(line)
end

local function show(entry)
  local i = 1
  local pageSize = 13
  while true do
    header(entry.title)
    for n = i, math.min(i + pageSize - 1, #entry.body) do
      draw(entry.body[n])
    end
    setColor(colors.gray)
    print("")
    if i + pageSize <= #entry.body then
      print("[key] next")
      os.pullEvent("key")
      i = i + pageSize
    else
      print("[key] back")
      os.pullEvent("key")
      return
    end
  end
end

while true do
  header()
  for i, entry in ipairs(entries) do
    setColor(colors.lime)
    print(i .. ". " .. entry.title)
  end
  setColor(colors.gray)
  print("")
  print("Q. close")
  setColor(colors.white)
  write("> ")

  local input = string.lower(read() or "")
  if input == "q" or input == "quit" or input == "exit" then
    clear()
    setColor(colors.gray)
    print("SESSION CLOSED")
    return
  end

  local n = tonumber(input)
  if n and entries[n] then show(entries[n]) else
    setColor(colors.red)
    print("Invalid.")
    sleep(0.7)
  end
end