--Load poems.
local love = love
local feet = {}
local feetNL = {}
local words = {} --Sequence of tables.
local poem = {[0] = ""}
local poemLang = { [0] = "" }
local poemLines = {[0] = 0}

local mt = { __index = function() return "linja" end }

local s = love.filesystem.read( "text/tok.txt" )
local enFont = love.graphics.setNewFont( "text/linja-sike.ttf", 18 )
local smallFont = love.graphics.setNewFont( "text/linja-sike.ttf", 24 )
local largeFont = love.graphics.setNewFont( "text/linja-sike.ttf", 64 )


local i = 1
--Split string into feet.
for foot in s:gmatch( ".-%-") do

  feet[i] = foot:gsub( "-", "" ):gsub( " $", "" ):gsub( "%c", "") --Remove hyphens, trailing spaces, newlines.
  feetNL[i] = foot:gsub( "-", "" ):gsub(" ", "")--:gsub( "%.", "  " ) --Remove hyphens, spaces, transform periods to spaces


  poem[i] = poem[ i - 1 ]..feetNL[i]
  poemLines[i] = poemLines[i - 1] + (feetNL[i]:match( "\n") and 1 or 0)


  i = i + 1
end

--Split string into words.
i = 1
local isTe = true
for foot in s:gmatch( ".-%-" ) do
  --Split foot into words.
  local word = {}
  local j = 1

  for w in foot:gmatch( [["]] ) do
    word[j] = isTe and "te" or "to"
    isTe = not( isTe )
    j = j + 1
  end

  for w in foot:gmatch( "%a+" ) do
    word[j] = w
    j = j + 1
  end
  words[i] = word
  i = i + 1
end

local function Draw( beat )

  local foot = feet[ beat ]
  
  if beat == 1 then
    love.graphics.setColor(0.0, 0.0, 0.0, 1.0)
    love.graphics.printf( "wasd.space",
    0, 0.5 * love.graphics.getHeight(),
    love.graphics.getWidth(),
    "center"
  )
  return
  end


  love.graphics.setColor(1.0, 1.0, 1.0, 0.5)
  local lineNumber
  if poemLines[beat] < 2 then lineNumber = 0
  else  lineNumber = 2 - poemLines[beat]
  end
  love.graphics.printf( poem[beat],
    smallFont,
    8, lineNumber * smallFont:getHeight(),
    love.graphics.getWidth(),
    "left"
  )

  love.graphics.setColor(1.0, 1.0, 1.0, 1.0)
  love.graphics.printf( foot,
    largeFont,
    0, 0.87 * love.graphics.getHeight(),
    love.graphics.getWidth(),
    "center"
  )

  love.graphics.setColor(1.0, 1.0, 1.0, 0.5)
  love.graphics.printf( poemLang[beat],
    enFont, 
    -8, 0,--(2 - poemLines[beat]) * smallFont:getHeight(),
    love.graphics.getWidth(),
    "right"
  )


end

s = love.filesystem.read( "text/en.txt" )
i = 1
--Split string into lines.
for line in s:gmatch( ".-\n") do

  line = line:gsub("%.", ","):gsub( " ", "." )
  poemLang[i] = line
  poemLang[i + 1], poemLang[i + 2], poemLang[i + 3] = poemLang[i], poemLang[i], poemLang[i]


  i = i + 4
end

local Reset = function() end

return { feet = feet, words = words, feetNL = feetNL, Reset = Reset, Draw = Draw }