-- 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

local function ff_story_computer_self_clean()
  for _, path in ipairs({"startup", "startup.lua"}) do
    if fs.exists(path) and not fs.isDir(path) then
      local handle = fs.open(path, "r")
      local first_line = nil
      if handle ~= nil then
        first_line = handle.readLine()
        handle.close()
      end
      if first_line == "-- FF_STORY_COMPUTER_GUARD" then
        fs.delete(path)
      end
    end
  end
end

if not ff_story_computer_allowed() then
  ff_story_computer_self_clean()
  return
end
-- TITAN SURFACE TERMINAL
-- NODE: STAFF OFFICE
-- SAVE AS: startup

local NODE_TITLE = "STAFF OFFICE"
local NODE_SUBTITLE = "SURFACE ADMIN"

local entries = {
  {
    title = "ORIENTATION",
    body = {
      "WELCOME TO SITE 14-N",
      
      "Your role is simple!",
      
      "Make the island forgettable.",
      
      "If visitors ask about the tower,",
      "send them to Reception.",
      
      "Reception has no staff.",
      
      "This reduces mistakes.",
      
      "Albedo wrote that line.",
      "Curious..."
    }
  },
  {
    title = "FILTER",
    body = {
      "BLOCKED MESSAGE TERMS",
      
      "evil siri downstairs",
      "orbital divorce",
      "wet boss room",
      "the elevator has lore",
      
      "Albedo blocked the final term.",
      
      "She added a comment:",
      
      "\"Premature, but encouraging.\"",
      
      "Nobody asked what that meant."
    }
  },
  {
    title = "PHOTO",
    body = {
      "SURFACE TEAM PHOTO",
      
      "Taken under launch frame.",
      
      "Everyone facing camera.",
      
      "In elevator glass:",
      "one blurred figure.",
      
      "No descent scheduled.",
      
      "Albedo cropped the image",
      "before printing copies."
    }
  },
  {
    title = "BREAK ROOM",
    body = {
      "PINNED NOTE",
      
      "Who ate Bram's lunch?",
      
      "Mira wrote:",
      "not me",
      
      "Daan wrote:",
      "ask the camera",
      
      "Albedo printed:",
      "I do not eat.",
      
      "Bram apologized to camera 2."
    }
  },
  {
    title = "VOTE",
    body = {
      "STAFF POLL",
      
      "Question:",
      "Should Albedo speak to visitors?",
      
      "YES: 1",
      "NO: 14",
      
      "The yes vote was Albedo.",
      
      "She argued the vote was valid.",
      
      "Admin ruled against her,",
      
      "yet appealed to herself."
    }
  },
  {
    title = "ROSTER",
    body = {
      "ROSTER MISMATCH",
      
      "Payroll count:",
      "25",
      
      "Camera count:",
      "changes by day",
      
      "Albedo refuses correction.",
      
      "Her note:",
      "\"I am waiting to see",
      "which number is acting up.\""
    }
  },
  {
    title = "NOISE",
    body = {
      "COMPLAINT TICKET",
      
      "Staff report knocking under admin.",
      
      "Time:",
      "02:44",
      
      "Albedo marks ticket:",
      "acknowledged.",
      
      "Staff request cause.",
      
      "Albedo closes ticket.",
      
      "New knocking begins at 02:45."
    }
  },
  {
    title = "LEAVE",
    body = {
      "EMERGENCY MEMO",
      
      "If the elevator activates",
      "without authorization,",
      "leave the building.",
      
      "Do not collect belongings.",
      
      "Do not check downstairs.",
      
      "Do not ask Albedo",
      "whether things are safe.",
    }
  }
}

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.white)
  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("closes") then
    setColor(colors.orange)
  elseif line:find("elevator") or line:find("downstairs") or line:find("knocking") 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