--- @name         Pep talk
--- @description  A writer's words, for when the page is winning
--- @type         oneshot
--- @key          q
--- @requires     none
--- @caps         screen.draw

-- One quote, centred on the screen, chosen at random from the list
-- below. Press any key for another; Escape gives the page back. No
-- quote repeats until the whole list has been shown in a sitting.
--
-- Plain letters only in names and quotes: the device's fonts draw an
-- accented letter as a filled diamond.
--
-- Read-only. It never touches your document.

local QUOTES = {
  { "First forget inspiration. Habit is more dependable.", "Octavia E. Butler" },
  { "The scariest moment is always just before you start.", "Stephen King" },
  { "If you find a book you really want to read but it hasn't been written yet, then you must write it.", "Toni Morrison" },
  { "You sit down at the keyboard and you put one word after another until it's done. It's that easy, and that hard.", "Neil Gaiman" },
  { "Writing a book is a horrible, exhausting struggle, like a long bout of some painful illness.", "George Orwell" },
  { "You write until you come to a place where you still have your juice and know what will happen next.", "Ernest Hemingway" },
  { "Lose track of the 400 pages and write just one page for each day; it helps.", "John Steinbeck" },
  { "You must stay drunk on writing so reality cannot destroy you.", "Ray Bradbury" },
  { "I write entirely to find out what I'm thinking, what I'm looking at, what I see and what it means.", "Joan Didion" },
  { "Writing is an exploration. You start from nothing and learn as you go.", "E. L. Doctorow" },
  { "I call this draft zero, telling myself how the story is supposed to go.", "Terry Pratchett" },
  { "You don't write because you want to say something, you write because you have something to say.", "F. Scott Fitzgerald" },
  { "A small daily task, if it be really daily, will beat the labours of a spasmodic Hercules.", "Anthony Trollope" },
  { "Don't get it right, just get it written.", "James Thurber" },
  { "Write what should not be forgotten.", "Isabel Allende" },
  { "The difference between the almost right word and the right word is really a large matter: 'tis the difference between the lightning-bug and the lightning.", "Mark Twain" },
  { "The role of a writer is not to say what we all can say, but what we are unable to say.", "Anais Nin" },
  { "Go into yourself. Find out the reason that commands you to write.", "Rainer Maria Rilke" },
  { "Be regular and orderly in your life like a bourgeois, so that you may be violent and original in your work.", "Gustave Flaubert" },
  { "You just type and type, knowing that it's all conditional and temporary.", "George Saunders" },
  { "Protect the time and space in which you write. Keep everybody away from it, even the people who are most important to you.", "Zadie Smith" },
  { "Writing is work. It's also gambling.", "Margaret Atwood" },
  { "If it sounds like writing, I rewrite it.", "Elmore Leonard" },
  { "Turn up for work. Discipline allows creative freedom. No discipline equals no freedom.", "Jeanette Winterson" },
  { "Treat writing as a job. Be disciplined.", "Sarah Waters" },
  { "Write a book you'd like to read. If you wouldn't read it, why would anybody else?", "Hilary Mantel" },
  { "No tears in the writer, no tears in the reader. No surprise for the writer, no surprise for the reader.", "Robert Frost" },
  { "Write what disturbs you, what you fear, what you have not been willing to speak about. Be willing to be split open.", "Natalie Goldberg" },
  { "The art of writing is the art of applying the seat of the pants to the seat of the chair.", "Mary Heaton Vorse" },
  { "I would advise anyone who aspires to a writing career that before developing his talent he would be wise to develop a thick hide.", "Harper Lee" },
  { "Do not hoard what seems good for a later place in the book; give it, give it all, give it now.", "Annie Dillard" },
  { "The first sentence can't be written until the final sentence is written.", "Joyce Carol Oates" },
  { "You have to write the book that wants to be written.", "Madeleine L'Engle" },
  { "To write something you have to risk making a fool of yourself.", "Anne Rice" },
  { "Almost all good writing begins with terrible first efforts. You need to start somewhere.", "Anne Lamott" },
  { "Don't loaf and invite inspiration; light out after it with a club.", "Jack London" },
  { "Don't just plan to write; write. It is only by writing, not dreaming about it, that we develop our own style.", "P. D. James" },
  { "Good writing is essentially rewriting. I am positive of this.", "Roald Dahl" },
  { "Read, read, read. Read everything: trash, classics, good and bad, and see how they do it.", "William Faulkner" },
  { "You can't use up creativity. The more you use, the more you have.", "Maya Angelou" },
  { "I love deadlines. I love the whooshing noise they make as they go by.", "Douglas Adams" },
  { "The unread story is not a story; it is little black marks on wood pulp. The reader, reading it, makes it live.", "Ursula K. Le Guin" },
  { "Every character should want something, even if it is only a glass of water.", "Kurt Vonnegut" },
  { "When you can't create you can work.", "Henry Miller" },
  { "A writer who waits for ideal conditions under which to work will die without putting a word on paper.", "E. B. White" },
  { "Writing is hard work. A clear sentence is no accident.", "William Zinsser" },
  { "Soon I'll find the right words, they'll be very simple.", "Jack Kerouac" },
  { "Write each and every day of your life.", "Walter Mosley" },
  { "When I don't feel like writing, I force myself to somehow fulfil my quota.", "Haruki Murakami" },
  { "Writing was something one had to do every day, exercising the writing muscle.", "Jane Yolen" },
  { "A word after a word after a word is power.", "Margaret Atwood" },
  { "All you have to do is write one true sentence. Write the truest sentence that you know.", "Ernest Hemingway" },
  { "The pages are still blank, but there is a miraculous feeling of the words all being there, written in invisible ink and clamoring to become visible.", "Vladimir Nabokov" },
  { "The difficulty of literature is not to write, but to write what you mean.", "Robert Louis Stevenson" },
  { "What is written without effort is in general read without pleasure.", "Samuel Johnson" },
  { "You can always edit a bad page. You can't edit a blank page.", "Jodi Picoult" },
  { "Be your own editor/critic. Sympathetic but merciless!", "Joyce Carol Oates" },
  { "Proceed slowly and take care.", "Annie Proulx" },
  { "Be persistent.", "Ian Rankin" },
  { "Finish everything you start.", "Colm Toibin" },
  { "Think big and stay particular.", "Andrew Motion" },
  { "Never be satisfied with a first draft.", "Rose Tremain" },
  { "Don't panic.", "Sarah Waters" },
  { "Trust your creativity.", "Jeanette Winterson" },
  { "Regard every new page as a small triumph.", "Roddy Doyle" },
  { "The way to write a book is to actually write a book.", "Anne Enright" },
  { "Don't wait for inspiration. Discipline is the key.", "Esther Freud" },
  { "Writers write. On you go.", "A. L. Kennedy" },
  { "Try to think of others' good luck as encouragement to yourself.", "Richard Ford" },
  { "Style is the art of getting yourself out of the way, not putting yourself in it.", "David Hare" },
  { "It is the gestation time which counts.", "Michael Morpurgo" },
  { "Ignore all proffered rules and create your own, suitable for what you want to say.", "Michael Moorcock" },
  { "Don't look back until you've written an entire draft.", "Will Self" },
  { "Finish what you're writing. Whatever you have to do to finish it, finish it.", "Neil Gaiman" },
  { "All that matters is what you leave on the page.", "Zadie Smith" },
  { "If you get stuck, get away from your desk.", "Hilary Mantel" },
  { "Try to leave out the part that readers tend to skip.", "Elmore Leonard" },
  { "Write what you need to write, not what is currently popular or what you think will sell.", "P. D. James" },
  { "Forget talent. If you have it, fine. Use it. If you don't have it, it doesn't matter.", "Octavia E. Butler" },
  { "Write with the door closed, rewrite with the door open.", "Stephen King" },
  { "I write because I don't know what I think until I read what I say.", "Flannery O'Connor" },
  { "You can only see as far as your headlights, but you can make the whole trip that way.", "E. L. Doctorow" },
  { "There is no greater agony than bearing an untold story inside you.", "Maya Angelou" },
  { "Everything in life is writable about if you have the outgoing guts to do it.", "Sylvia Plath" },
  { "The best time for planning a book is while you're doing the dishes.", "Agatha Christie" },
  { "Writing, to me, is simply thinking through my fingers.", "Isaac Asimov" },
  { "A writer is someone for whom writing is more difficult than it is for other people.", "Thomas Mann" },
  { "We write to taste life twice, in the moment and in retrospection.", "Anais Nin" },
  { "Ideas are like rabbits. You get a couple and learn how to handle them, and pretty soon you have a dozen.", "John Steinbeck" },
  { "You can make anything by writing.", "C. S. Lewis" },
  { "Every sentence must do one of two things: reveal character or advance the action.", "Kurt Vonnegut" },
  { "When in doubt, have a man come through the door with a gun in his hand.", "Raymond Chandler" },
  { "Good prose is like a windowpane.", "George Orwell" },
  { "As to the Adjective: when in doubt, strike it out.", "Mark Twain" },
  { "Words are, of course, the most powerful drug used by mankind.", "Rudyard Kipling" },
  { "After nourishment, shelter and companionship, stories are the thing we need most in the world.", "Philip Pullman" },
  { "You want to write a sentence as clean as a bone. That is the goal.", "James Baldwin" },
  { "Talent is insignificant. I know a lot of talented ruins.", "James Baldwin" },
  { "I can shake off everything as I write; my sorrows disappear, my courage is reborn.", "Anne Frank" },
  { "Whatever you're meant to do, do it now. The conditions are always impossible.", "Doris Lessing" },
}

local MARGIN = 8

local function wrap(s, text, width)
  local out, line = {}, ""
  for word in text:gmatch("%S+") do
    local try = (line == "") and word or (line .. " " .. word)
    if s.textWidth(try) <= width then
      line = try
    else
      if line ~= "" then out[#out + 1] = line end
      line = word
    end
  end
  if line ~= "" then out[#out + 1] = line end
  return out
end

-- Lay the quote out in the writer's font; if it will not fit the
-- screen with its attribution, drop to a smaller one.
local function fit(s, quote)
  local w, h = s.width(), s.height()
  local width = w - MARGIN * 2
  for _, font in ipairs({ "default", "small" }) do
    s.setFont(font)
    local lines = wrap(s, '"' .. quote[1] .. '"', width)
    local lh = s.lineHeight()
    local needed = (#lines + 2) * lh          -- quote, a gap, the name
    if needed <= h - MARGIN or font == "small" then
      return lines, lh
    end
  end
end

local function paint(s, quote)
  local w, h = s.width(), s.height()
  s.clear()
  local lines, lh = fit(s, quote)
  local block = (#lines + 2) * lh
  local top = (h - block) // 2
  if top < 2 then top = 2 end

  for i, line in ipairs(lines) do
    local x = (w - s.textWidth(line)) // 2
    s.text(x, top + i * lh, line)
  end

  local who = "- " .. quote[2]
  local wx = w - MARGIN - s.textWidth(who)
  s.text(wx, top + (#lines + 2) * lh, who)
end

-- A shuffled deck, dealt from the top, so nothing repeats until the
-- whole list has been seen.
local function shuffled()
  local deck = {}
  for i = 1, #QUOTES do deck[i] = i end
  for i = #deck, 2, -1 do
    local j = math.random(i)
    deck[i], deck[j] = deck[j], deck[i]
  end
  return deck
end

function main()
  if not BYOK.screen.open() then return end
  local s = BYOK.screen
  math.randomseed()

  local deck, at = shuffled(), 0
  local function next_quote()
    at = at + 1
    if at > #deck then deck, at = shuffled(), 1 end
    return QUOTES[deck[at]]
  end

  local quote = next_quote()
  paint(s, quote)

  while s.isOpen() do
    local k = s.key(2000)
    if not s.isOpen() then break end
    if k ~= nil then
      quote = next_quote()
      paint(s, quote)
    elseif s.damaged() then
      paint(s, quote)
    end
  end
end
