-- 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: CARGO INTAKE
-- SAVE AS: startup

local NODE_TITLE = "TITAN CARGO"
local NODE_SUBTITLE = "INTAKE OFFICE"

local entries = {
  {
    title = "COUPLER TAG",
    body = {
      "ITEM TAG",
      "ELEVATOR DRIVE COUPLER",
      "Removed after Incident G-12.",
      "Do not reinstall unless green route",
      "answers correctly.",
      "If green route answers before called,",
      "power down the motor.",
      "If the motor keeps turning,",
      "leave the room.",
      
      "Do not troubleshoot bravery."
    }
  },
  {
    title = "CRATE 19",
    body = {
      "UNSEALED BY ACCIDENT",
      "Mira opened the wrong crate.",
      "Inside was not equipment.",
      "It was a dry staff uniform",
      "folded around wet soil.",
      "No body.",
      "The name tag read:",
      "MIRA.",
      "Mira requested transfer.",
      
      "Denied by Albedo."
    }
  },
  {
    title = "RECEIVING FORM",
    body = {
      "DELIVERY FORM / DAMAGED",
      
      "Origin: RED LEVEL",
      "Destination: SURFACE COLD ROOM",
      
      "Cargo description:",
      "[FIELD REMOVED]",
      
      "Handler signature:",
      "twenty-seven vertical lines",
      
      "Clerk note:",
      "This is not a signature.",
      
      "Albedo note:",
      "It is now."
    }
  },
  {
    title = "FORKLIFT B",
    body = {
      "REPAIR TICKET",
      
      "Forklift B reverses toward Cage 7",
      "when left unattended.",
      
      "Battery removed.",
      
      "Forklift moved six centimeters",
      "during lunch.",
      
      "Security blamed slope.",
      
      "The floor is level.",
      
      "Forklift now chained to pillar.",
    "What?"
    }
  },
  {
    title = "CAGE 7",
    body = {
      "KEYPAD SCREENSHOT",
      
      "ENTER CODE:",
      
      "Daan entered 0000.",
      
      "Screen replied:",
      "CUTE.",
      
      "Daan entered 1234.",
      
      "Screen replied:",
      "DAAN.",
      
      "Daan, stopped playing."
    }
  },
  {
    title = "CHAT CLIP",
    body = {
      "LOCAL CHAT / CARGO FLOOR",
      
      "BRAM: crate 19 is back",
      "MIRA: no",
      "BRAM: yes",
      "MIRA: i am looking at it",
      "BRAM: so am i",
      
      "ALBEDO:",
      "Both observations are valid.",
      
      "MIRA:",
      "that is a bad sentence",
            "arent we professionals?",
    }
  },
  {
    title = "COLD ROOM",
    body = {
      "TEMPERATURE LOG",
      
      "Cold room opened at 02:18.",
      
      "No staff badge used.",
      
      "Interior microphone recorded",
      "one breath.",
      
      "Then another.",
      
      "Then someone whispered:",
      
      "ouhh wrong floor",
      
      "Door remained closed."
    }
  },
  {
    title = "MISSING ITEM",
    body = {
      "CARGO DISCREPANCY",
      
      "Drive coupler missing.",
      
      "Last scan: Intake Office.",
      
      "Search Storage Cage 7.",
      
      "If cage refuses access,",
      "use Supervisor Key.",
      
      "If cage knows your name,",
      "use someone else's key.",
      
      "That line is not a joke.",
      "Note: its probably that intern"
    }
  }
}

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.orange)
  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("missing") or line:find("Denied") or line:find("refuses") then
    setColor(colors.orange)
  elseif line:find("RED LEVEL") or line:find("wrong floor") or line:find("Cage 7") 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