local gui = require("gui")
local color = require("gui.core.color")
local menu = require("gui.core.menus")
local proc = gui:getProcessor():newProcessor("main-title")

local APP_VERSION = love.filesystem.read("version.txt")

local board = require("menus.board")

local titleLog = log:child("title")

function Menu(frame)
    local background = frame:newImageLabel("assets/images/background.jpg")
    background:fullFrame()
    local title = background:newTextLabel("Jeopardy",0,0,0,0,0,.1,1,.25)
    title:setFont("assets/fonts/gyparody.ttf",300)

    titleLog:debug("Setting title shader")
    title:setShader(gui.SHADERS.vignette,{
        intensity = .5,
        smoothness = .8
    })

    title.align = gui.ALIGN_CENTER
    title.visibility = 0
    title.textColor = color.title_font
    title.drawBorder = false

    titleLog:debug("Creating font thread")
    proc:newThread(function()
        local function enabled()
            return background.visible
        end
        while true do
            thread.hold(enabled)
            title:centerFont()
        end
    end)

    local items = 0
    local MenuOption = function(str)
        local item = background:newImageLabel("assets/images/CLOUD.png",-150,-63+(items*130),300,125,.5,.6)
        local text = item:newTextLabel(str)
        text:fullFrame()
        text.align = gui.ALIGN_CENTER
        thread:newThread(function()
            thread.skip(2)
            text:fitFont(nil,nil,{scale = 1/2})
            text:centerFont()
            text.visibility = 0
            text.textColor = color.new("#24269a")
        end)
        items = items + 1
        return item, text
    end
    local play = MenuOption("PLAY")
    local settings = MenuOption("SETTINGS")
    local quit = MenuOption("QUIT")

    gui.apply({
        OnEnter = function(self)
            self:setShader(gui.SHADERS.glow)
        end,
        OnExit = function(self)
            self:setShader()
        end,
        shaderTime = {true}
    },play,settings,quit)

    play:OnReleased(function()
        titleLog:debug("Play button pressed")
        if love.filesystem.getInfo("quiz") then
            menu.getMenu("board"):visible(true, "quiz")
            return
        else
            gui:newFilePicker("File picker test",nil, {".zip"}, function(file)
                titleLog:info("File picked: %s", file)
                menu.getMenu("board"):visible(true, file:gsub("\\","/"):gsub(love.filesystem.getSaveDirectory(),""):sub(2,-1))
            end)
        end
    end)

    quit:OnReleased(function()
        titleLog:debug("Quit button pressed")
        local f = frame:newFrame()
        f:fullFrame()
        f.visibility = .8
        f.color = color.black
        local cm = menu.getCurrentMenu()
        cm:setTag("visual")
        quit:setShader()
        gui:newMessageBox({
            title    = "Confirm",
            message  = "Are you sure you want to exit the game?",
            buttons  = { "Yes", "No" },
            onChoice = function(label, idx)
                if label == "Yes" then os.exit() end
                cm:removeTag("visual")
                f:destroy()
            end,
        })
    end)

    local version = frame:newTextLabel("Version: " .. APP_VERSION, -150,-30, 150, 30, 1, 1)
    thread:newThread(function()
        thread.skip(2)
        version:fitFont()
    end)
    version.textColor = color.new("#b16d24")
    version.visibility = 0
    
    return background
end

return menu.registerMenu("title", Menu)