-- 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: NEMESIS LEGACY FRAME
-- SAVE AS: startup

local NODE_TITLE = "CROWNWATCH RELAY"
local NODE_SUBTITLE = "SURFACE LAUNCH FRAME"

local entries = {
  {
    title = "PLAQUE",
    body = {
      "DECOMMISSIONED SENSOR MAST",
      
      "That is what the plaque says.",
      
      "The plaque is new.",
      
      "Under it, older bolts outline",
      "a larger plate.",
      
      "Paint trace reads:",
      
      "CROWNWATCH ASCENT RELAY",
      
      "Someone ground the words off.",
      
      "Poorly."
    }
  },
  {
    title = "TEST 17",
    body = {
      "FRAME TEST 17",
      
      "Payload did not launch.",
      
      "Rail clamps stayed locked.",
      
      "NEMESIS still replied:",
      
      "TARGET RECEIVED",
      
      "how is this possible?",
      "stupid systems acting up",
      
      "Then Albedo said:",
      "\"Do not look at me.\""
    }
  },
  {
    title = "OLD NAME",
    body = {
      "CROWNWATCH ARCHIVE",
      
      "Older staff used Crownwatch.",
      
      "New orders used NEMESIS.",
      
      "Daan asked which name was correct.",
      
      "Albedo answered:",
      
      "One watches.",
      
      "One arrives."
      
    }
  },
  {
    title = "SPLIT ORDER",
    body = {
      "AUTHORITY CHANGE",
      
      "Orbital control removed from Albedo.",
      
      "Warium phrasing:",
      "risk partitioning.",
      
      "Staff phrasing:",
      "they took away her sky privileges.",
      
      "Albedo phrasing:",
      
      "\"They feared my taste!\""
    }
  },
  {
    title = "HATCH NOTE",
    body = {
      "INSIDE SERVICE HATCH",
      
      "A note is taped behind the panel.",
      
      "if this frame wakes up",
      "do not ask who called it",
      
      "ask what answered",
      
      "The handwriting matches no active staff.",
      
      "Albedo refuses graphology scan.",
      
      "\"Ugly field. Bad fonts. Eww.\""
    }
  },
  {
    title = "SKYLOCK SIM",
    body = {
      "SIMULATION RESULT",
      
      "Unauthorized launch path:",
      "terminated before cloudline.",
      
      "Second run:",
      "terminated",
      
      "Third run cancelled by staff.",
    }
  },
  {
    title = "BURST",
    body = {
      "SIGNAL BURST",
      
      "Three years after separation,",
      "the frame received a reply.",
      
      "Source:",
      "NEMESIS",
      
      "Decoded text:",
      
      "STATUS?",
      
      "Note: Albedo opened the file daily",
      "for forty-one days."
    }
  },
  {
    title = "JUDGMENT",
    body = {
      "SEALED PROTOCOL",
      
      "JUDGMENT RELAY RESTORE",
      
      "Use only during containment loss.",
      
      "NEMESIS will not recover Titan.",
      
      "It will solve Titans problem",
      
      "That wording came from Albedo.",
      
      "Nobody approved it...",
      "or improved it."
    }
  }
}

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.red)
  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("NEMESIS") or line:find("CROWNWATCH") or line:find("Skylock") then
    setColor(colors.red)
  elseif line:find("do not") or line:find("terminated") or line:find("SEALE") then
    setColor(colors.orange)
  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