Demo = {}
DemoPlayback = false
DemoRecording = false
DemoAction = nil -- Table of actions
CurrentDemoFrame = nil

function Demo:Draw()
  if DemoRecording then
    love.graphics.setColor(1,0,0,1)
  elseif DemoPlayback then
    love.graphics.setColor(0,0,1,1)
  end
  love.graphics.rectangle("line",0,0,game.width	,game.height)
end

function Demo:PlaybackStart()
  DemoPlayback = true
  CurrentDemoFrame = 0
  dofile("demos/play_demo.lua")
end

function Demo:PlaybackEnd()
  DemoPlayback = false
  DemoAction = nil
end

function Demo:RecordAction(action)
  if DemoRecording
  and action ~= nil
  then
    DemoFile:write("\""..action.."\",")
  end
end

function Demo:RecordStart()
    -- Make demo stuff
    os.execute( "mkdir \"./demos\"" )
    DemoFile = io.open("demos/play_demo.lua", "w+")
    --DemoFile = io.open("demo/mothbackDemo_"..os.date("%Y-%m-%d_%H-%M-%S")..".lua", "w+")
    DemoFile:write("main_Player.pos.x = "..main_Player.pos.x.."\n")
    DemoFile:write("main_Player.pos.y = "..main_Player.pos.y.."\n")
    DemoFile:write("DemoAction = {\n")
    DemoRecording = true
    CurrentDemoFrame = 1
end

function Demo:RecordEnd()
    DemoFile:write("}\n}")
    DemoFile:close()
    DemoFile = nil
    DemoRecording = false
end

function Demo:Step()
  if DemoRecording then
    if CurrentDemoFrame == 1 then
      DemoFile:write("\t{")
    else
      DemoFile:write("},\n\t{")
    end
  elseif DemoPlayback then
    if DemoAction[CurrentDemoFrame + 1] == nil then Demo:PlaybackEnd() end
  end
  CurrentDemoFrame = CurrentDemoFrame + 1
end
