-- 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 DESCENT / SURFACE LIFT
-- ComputerCraft / CC:Tweaked startup script
-- Outputs redstone after solving.
--
-- pastebin get PASTE_CODE startup
-- reboot

local OUTPUT_SIDE = "back"
local STATE_FILE = ".titan_lift_ready"

local entries = {
  {
    title = "MAIL / LIFT PART",
    body = {
      "FROM: bram.v",
      "TO: surface-maint",
      "",
      "Stop calling it missing.",
      "I pulled it out myself.",
      "",
      "Lift came up empty again.",
      "Brake room heard it moving",
      "ten minutes before it arrived.",
      "",
      "If you want it running,",
      "put back the part marked:",
      "",
      "COUPLER"
    }
  },
  {
    title = "ROUTE NOTE",
    body = {
      "FROM: daan.v",
      "TO: intake-desk",
      "",
      "Surface console only gets one",
      "valid destination now.",
      "",
      "Not red.",
      "Do not try red from here.",
      "",
      "If the button shows red,",
      "kill power to the panel.",
      "",
      "Allowed route:",
      "",
      "GREEN"
    }
  },
  {
    title = "ALBEDO PATCH",
    body = {
      "LOCAL PATCH 14-N",
      "",
      "Voice auth removed.",
      "",
      "Daan kept joking into the mic.",
      "Albedo started answering him",
      "through closed terminals.",
      "",
      "New override is typed.",
      "One word.",
      "",
      "She chose:",
      "",
      "AWAKE"
    }
  },
  {
    title = "BRAKE ROOM",
    body = {
      "SHIFT NOTE",
      "",
      "Brake release sticks if you rush it.",
      "",
      "Count the first clunk.",
      "Ignore the second.",
      "",
      "If there is a third,",
      "you are not alone in the shaft.",
      "",
      "Operator input:",
      "",
      "WAIT"
    }
  },
  {
    title = "LAST CALL",
    body = {
      "AUTO-TRANSCRIPT",
      "",
      "CALL BUTTON PRESSED: GREEN",
      "OPERATOR: none",
      "",
      "Lift speaker opened.",
      "",
      "No voice for six seconds.",
      "",
      "Then someone breathed close",
      "to the microphone.",
      "",
      "Albedo deleted the audio.",
      "",
      "She kept the timestamp."
    }
  },
  {
    title = "SERVICE MODE",
    body = {
      "SURFACE LIFT SERVICE",
      "",
      "Manual witness mode active.",
      "",
      "Enter four confirmations.",
      "",
      "PART",
      "ROUTE",
      "OVERRIDE",
      "BRAKE",
      "",
      "Wrong input resets panel.",
      "",
      "Albedo added that part.",
      "No ticket found."
    }
  }
}

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.lightBlue)
  print("TITAN DESCENT / SURFACE LIFT")
  setColor(colors.gray)
  print("---------------------------------------")
  if title then
    setColor(colors.white)
    print(title)
    setColor(colors.gray)
    print("---------------------------------------")
  end
  setColor(colors.white)
end

local function armed()
  return fs.exists(STATE_FILE)
end

local function saveArmed()
  local f = fs.open(STATE_FILE, "w")
  f.write("READY")
  f.close()
end

local function bootOutput()
  redstone.setOutput(OUTPUT_SIDE, armed())
end

local function waitKey()
  setColor(colors.gray)
  print("")
  print("[key] back")
  os.pullEvent("key")
end

local function draw(line)
  if line == "COUPLER" or line == "GREEN" or line == "AWAKE" or line == "WAIT" then
    setColor(colors.lime)
  elseif line:find("Albedo") then
    setColor(colors.cyan)
  elseif line:find("red") or line:find("shaft") then
    setColor(colors.red)
  else
    setColor(colors.white)
  end
  print(line)
end

local function show(entry)
  header(entry.title)
  for _, line in ipairs(entry.body) do
    draw(line)
  end
  waitKey()
end

local function ask(label, answer)
  header("MANUAL WITNESS MODE")
  setColor(colors.white)
  print(label)
  print("")
  setColor(colors.gray)
  write("> ")
  setColor(colors.white)

  local input = string.upper(read() or "")

  if input == answer then
    setColor(colors.lime)
    print("")
    print("ACCEPTED")
    sleep(0.7)
    return true
  end

  setColor(colors.red)
  print("")
  print("REJECTED")
  sleep(1)
  return false
end

local function unlock()
  if armed() then
    header("READY")
    setColor(colors.lime)
    print("Lift drive already coupled.")
    waitKey()
    return
  end

  if not ask("PART", "COUPLER") then return end
  if not ask("ROUTE", "GREEN") then return end
  if not ask("OVERRIDE", "AWAKE") then return end
  if not ask("BRAKE", "WAIT") then return end

  header("COUPLING")
  setColor(colors.cyan)
  print("ALBEDO:")
  setColor(colors.white)
  print("")
  print("Fine.")
  print("")
  print("Surface lift restored.")
  print("Try not to make this historic.")
  print("History here is unpleasant.")

  saveArmed()
  redstone.setOutput(OUTPUT_SIDE, true)

  waitKey()
end

bootOutput()

while true do
  header()

  if armed() then
    setColor(colors.lime)
    print("STATUS: DRIVE COUPLED")
  else
    setColor(colors.orange)
    print("STATUS: DRIVE OPEN")
  end

  setColor(colors.gray)
  print("---------------------------------------")
  setColor(colors.white)

  print("1. Mail / lift part")
  print("2. Route note")
  print("3. Albedo patch")
  print("4. Brake room")
  print("5. Last call")
  print("6. Service mode")
  print("7. Enter witness mode")
  print("")
  print("Q. close")
  print("")

  setColor(colors.gray)
  write("> ")
  setColor(colors.white)

  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 n >= 1 and n <= 6 then
    show(entries[n])
  elseif input == "7" then
    unlock()
  else
    setColor(colors.red)
    print("Invalid.")
    sleep(0.6)
  end
end