local logger = require("utils/logger")
local lovehandler = require("utils/love_file_handler")

multi,  thread = require("multi"):init({priority=true})
multi.setClock(require("socket").gettime) -- When on linux os.clock doesn't reture actual seconds the program has elapsed for
GLOBAL, THREAD = require("multi.integration.loveManager"):init()



local fileHandler, logCtrl = lovehandler.new("logs/jeopardy.log", {
    max_size   = 512 * 1024,  -- rotate at 512 KB
    max_files  = 3,
    buffered   = false,       -- write every line immediately (safest for games)
})

lovehandler.auto_flush_on_quit(logCtrl)

log = logger.new("jeopardy", {
  level    = logger.LEVELS.DEBUG,
  handlers = {
    logger.handlers.ConsoleHandler({ colours = true }),
    fileHandler,
  },
})

local gui    = require("gui")
local color  = require("gui.core.color")
local title  = require("menus.title")
local menu   = require("gui.core.menus")

color.indexColor("bg","#2b348b")
color.indexColor("tile_bg","#5459e4")
color.indexColor("header_tile","#2a2e91")
color.indexColor("status_font", "#e4b82a")
color.indexColor("title_font","#b16d24")
color.indexColor("question_bg","#363ac8")
color.indexColor("tile_font","#ffffff")
color.indexColor("header_font","#fef6eeda")

require("gui.addons")

gui:setHotKey({"f11"})(function()
    fullscreen, fstype = love.window.getFullscreen()
	love.window.setFullscreen(not fullscreen, "desktop")
end)

local loadLog = log:child("load")
local old = multi.error

multi.error = function(self, err)
    loadLog:error("Error: %s", err)
    love.quit()
end

function love.load()
    loadLog:info("Starting Jeopardy Game")
    loadLog:debug("Setting game identity to jeopardy")
    love.filesystem.setIdentity("jeopardy")
    loadLog:trace("Creating save directory")
    success = love.filesystem.createDirectory("")
    if not success then
        loadLog:fatal("Unable to create save directory")
        love.quit()
    end
    loadLog:trace("Creating save directory")
    gui:cacheImage({"assets/images/checked.png","assets/images/unchecked.png","assets/images/speaker.png"})
    gui:setAspectSize(1920, 1080)
    gui.aspect_ratio = true
    loadLog:debug("Creating main background image")
    local bg = gui:newImageLabel("assets/images/background.png")
    bg:fullFrame()

    loadLog:debug("Init menu module")
    menu.init(bg)
    loadLog:debug("Displaying title menu")
    title:visible(true)
end

function love.update(dt)
    gui.update(dt) 
    multi:uManager(dt) 
end

function love.draw()
    gui.draw()
end

