-- 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: SURFACE GATE / ISLAND PERIMETER
-- Save as: startup

local TITLE = "TITAN SURFACE GATE"
local NODE = "ISLAND PERIMETER"

local entries = {
  {
    title = "Camera 4B",
    body = {
      "FOG EVENT: 03:12",
      "",
      "Camera 4B recorded a figure",
      "standing beyond the sea wall.",
      "",
      "No boat logged.",
      "No beacon logged.",
      "No heat return.",
      "",
      "Security tagged it as driftwood.",
      "",
      "Albedo retagged it:",
      "\"driftwood with posture\"",
      "",
      "File locked before morning shift."
    }
  },
  {
    title = "Approach Script",
    body = {
      "UNKNOWN VESSEL APPROACH",
      "",
      "Step 1: play Warning Track A.",
      "Step 2: play Warning Track B.",
      "Step 3: allow Albedo speech.",
      "",
      "Security note:",
      "Do not let her improvise.",
      "",
      "Albedo note:",
      "I saw that.",
      "",
      "Follow-up note:",
      "She keeps seeing that."
    }
  },
  {
    title = "Visitor Label",
    body = {
      "UNREGISTERED VISITOR",
      "",
      "Labels considered:",
      "- salvage",
      "- hostile",
      "- lost",
      "- bored",
      "- streamer behavior",
      "",
      "Albedo selected:",
      "",
      "VOLUNTARY LIABILITY",
      "",
      "Security accepted the label.",
      "Nobody had a better one."
    }
  },
  {
    title = "Warning Signs",
    body = {
      "REQUEST DENIED:",
      "Add warning signs to island.",
      "",
      "Reason:",
      "Warning signs attract people",
      "who read warning signs and say",
      "\"lore\".",
      "",
      "Albedo supported denial.",
      "",
      "Quote:",
      "\"They would sprint toward",
      "the red text.\""
    }
  },
  {
    title = "Western Wall",
    body = {
      "SEA WALL INSPECTION",
      "",
      "New impact scars found below",
      "the western waterline.",
      "",
      "Not storm debris.",
      "Not hull collision.",
      "Not known marine contact.",
      "",
      "Maintenance asked what it was.",
      "",
      "Albedo replied:",
      "\"Persistence.\""
    }
  },
  {
    title = "Dock Light",
    body = {
      "DOCK LIGHT 2",
      "",
      "Light failed during clear weather.",
      "Bulb intact.",
      "Cable intact.",
      "Relay intact.",
      "",
      "Camera showed the light turning",
      "away from the dock.",
      "",
      "Maintenance report rejected.",
      "",
      "Reason:",
      "\"lights do not look away\""
    }
  },
  {
    title = "Gate Rule",
    body = {
      "GATEHOUSE RULE 6",
      "",
      "If a visitor says they are",
      "\"just exploring\", deny entry.",
      "",
      "If they say they are",
      "\"doing a quest\", deny harder.",
      "",
      "If they ask about the elevator,",
      "lock interior access.",
      "",
      "If Albedo opens the gate anyway,",
      "pretend this was approved."
    }
  },
  {
    title = "First Joke",
    body = {
      "STAFF CHAT EXCERPT",
      "",
      "Mara: why is the island empty",
      "Mara: this place got bad vibes",
      "",
      "Senn: its an ocean lab",
      "Senn: ofc it has bad vibes",
      "",
      "ALBEDO:",
      "The island is not empty.",
      "",
      "Mara stopped typing."
    }
  },
  {
    title = "Perimeter Status",
    body = {
      "PERIMETER STATUS",
      "",
      "Fence: intact",
      "Dock: monitored",
      "Gate: sealed",
      "Sea wall: disputed",
      "Launch frame: quiet",
      "",
      "Surface visitors detected.",
      "",
      "Albedo classification:",
      "manageable, probably",
      "",
      "The word probably was added",
      "six seconds later."
    }
  }
}

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

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

local function header()
  clear()
  c(colors.red)
  print("[ " .. TITLE .. " ]")
  c(colors.gray)
  print(NODE)
  print("------------------------------")
  c(colors.white)
end

local function wait()
  c(colors.gray)
  print("")
  print("[ any key ]")
  os.pullEvent("key")
end

local function show(e)
  header()
  c(colors.orange)
  print(e.title)
  c(colors.gray)
  print("------------------------------")
  for _, line in ipairs(e.body) do
    if line:find("Albedo") or line:find("ALBEDO") then
      c(colors.cyan)
    elseif line:find("DENIED") or line:find("WARNING") or line:find("UNKNOWN") then
      c(colors.orange)
    else
      c(colors.white)
    end
    print(line)
  end
  wait()
end

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

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

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