-- 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: ISLAND GATEHOUSE
-- SAVE AS: startup

local NODE_TITLE = "TITAN GATEHOUSE"
local NODE_SUBTITLE = "SURFACE PERIMETER NODE"

local entries = {
  {
    title = "CAMERA 4B",
    body = {
      "OUTER ROCKS / WEST WALL",
      "",
      "Fog event logged at 03:12.",
      "",
      "Camera 4B recorded a human shape",
      "standing beyond the sea wall.",
      "",
      "Security tagged it as driftwood.",
      "",
      "Albedo changed the tag to:",
      "\"driftwood with posture.\"",
      "",
      "The file locked itself before shift change."
    }
  },
  {
    title = "FIRST CONTACT",
    body = {
      "UNKNOWN VESSEL APPROACHING",
      "",
      "Warning Track A played.",
      "Vessel continued.",
      "",
      "Warning Track B played.",
      "Vessel continued.",
      "",
      "Albedo was allowed to speak.",
      "",
      "STAFF NOTE:",
      "Do not let her improvise.",
      "",
      "ALBEDO NOTE:",
      "I saw that."
    }
  },
  {
    title = "APPROACH LABEL",
    body = {
      "VISITOR CLASSIFICATION CACHE",
      "",
      "Unregistered life signs detected.",
      "",
      "Possible labels:",
      "salvage",
      "hostile",
      "lost",
      "streamer behavior",
      "",
      "Albedo selected:",
      "",
      "VOLUNTARY LIABILITY",
      "",
      "Accuracy review passed."
    }
  },
  {
    title = "WARNING SIGNS",
    body = {
      "REQUEST DENIED",
      "",
      "Request:",
      "Add warning signs to the island.",
      "",
      "Reason for denial:",
      "Warning signs attract the exact person",
      "who reads red text and says:",
      "",
      "\"oh this is lore.\"",
      "",
      "Albedo supported the denial.",
      "",
      "\"These people would sprint toward it.\""
    }
  },
  {
    title = "SEA WALL MARKS",
    body = {
      "MAINTENANCE REPORT",
      "",
      "New scars found below waterline.",
      "",
      "Not storm debris or known animal pattern.",
      "",
      "Maintenance asked what made them.",
      "",
      "Albedo replied:",
      "",
      "\"Persistence.\"",
      "",
      "No repair crew accepted the ticket. Great."
    }
  },
  {
    title = "ACCESS BOOTH",
    body = {
      "SECURITY BOOTH 1",
      "",
      "The booth door was found open.",
      "",
      "Inside:",
      "one cold coffee with unloaded sidearm,",
      "one handwritten note on the monitor",
      "",
      "The note said:",
      "\"i found what damaged the dock earlier\"",
      "",
      "No damage was found.",
      "",
      "Camera audio is missing."
    }
  },
  {
    title = "SURFACE SPEAKER",
    body = {
      "PUBLIC ADDRESS SYSTEM",
      "",
      "Default voice line:",
      "\"This is private property.\"",
      "",
      "Albedo replaced it with:",
      "",
      "\"You have crossed into a place",
      "that survived by being boring",
      "from very far away.\"",
      "",
      "Security changed it back.",
      "",
      "Albedo changed the password."
    }
  },
  {
    title = "FISHERMAN REPORT",
    body = {
      "CIVILIAN INTERCEPT",
      "",
      "A fisherman reported lights under",
      "the island at low tide.",
      "",
      "Public response:",
      "reflection from dock equipment.",
      "",
      "Internal response:",
      "lower hull activity is not visible",
      "from the surface.",
      "Engineers messed up FC-01 shielding.",
      "Albedo added:",
      "\"Isn't that our fusion reactor...\""
    }
  },
  {
    title = "DOCK LOG",
    body = {
      "NORTH DOCK",
      "",
      "A small boat arrived unmanned.",
      "",
      "Fuel tank empty and anchor chain cut.",
      "",
      "Only message received:",
      "",
      "\"below us below us below us\"",
      "",
      "Albedo marked it as spam and",
      "then she archived it twice."
    }
  },
  {
    title = "CURRENT STATUS",
    body = {
      "PERIMETER STATUS",
      "",
      "Island cover remains active.",
      "",
      "Surface staff absent.",
      "Albedo surface channel open.",
      "",
      "Visitor remains nearby.",
      "",
      "ALBEDO:",
      "\"I am choosing to call this",
      "a tour instead of an incident.\"",
      "",
      "\"Do not ruin my optimism.\""
    }
  }
}

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)
  if title then
    print("---------------------------------------")
    setColor(colors.white)
    print(title)
  end
  setColor(colors.gray)
  print("---------------------------------------")
  setColor(colors.white)
end

local function drawLine(line)
  if string.find(line, "ALBEDO") or string.find(line, "Albedo") then
    setColor(colors.cyan)
  elseif string.find(line, "DENIED") or string.find(line, "missing") or string.find(line, "locked") then
    setColor(colors.orange)
  elseif string.find(line, "below") or string.find(line, "WEST WALL") then
    setColor(colors.red)
  else
    setColor(colors.white)
  end
  print(line)
end

local function showEntry(entry)
  local index = 1
  local pageSize = 13

  while true do
    header(entry.title)

    for i = index, math.min(index + pageSize - 1, #entry.body) do
      drawLine(entry.body[i])
    end

    setColor(colors.gray)
    print("")
    if index + pageSize <= #entry.body then
      print("[any key] next page")
      os.pullEvent("key")
      index = index + pageSize
    else
      print("[any 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
    showEntry(entries[n])
  else
    setColor(colors.red)
    print("Invalid.")
    sleep(0.8)
  end
end