------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------

local love        = require 'love'
local assets      = require 'src.utils.asstmngr'
local make_class  = require 'src.utils.classes'
local GameState   = require 'src.gstates.gstate'
local Fader       = require 'src.graphics.fader'
local Sprite      = require 'src.graphics.sprite'
local Cursor      = require 'src.ui.cursor'
local Font        = require 'src.ui.font'
local SoundEffect = require 'src.sound.sfx'
local VBox        = require 'src.ui.vbox'
local Color       = require 'src.utils.color'


------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------

local MainMenu = make_class(GameState)


------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------

function MainMenu:_init(name, index)
    GameState._init(self, name, index)
    self.next_state  = self.index
    self.fade        = Fader()

    -- Create sprites and buttons.
    self.background = Sprite('imgs/concb03.png')
    self.btn_font   = Font('fonts/Concrete.ttf')
    self.title_font = Font('fonts/BBrick.ttf', 35)

    -- Create UI elements.
    self.btns = VBox(15, 5)
    self.btns:add_label('LoveDOS', self.title_font, Color(215, 0, 0), true)
    self.btns:add_text_button('New Game', self.btn_font)
    self.btns:add_text_button('Load Game', self.btn_font)
    self.btns:add_text_button('Options', self.btn_font)
    self.btns:add_text_button('Help & Story', self.btn_font)
    self.btns:add_text_button('Quit', self.btn_font, function() self.next_state = -1 end)

    -- Create a mouse cursor object at the current mouse position.
    local mx, my = love.mouse.getPosition()
    self.cursor = Cursor(mx, my)

    -- Create sound effects.
    self.bgm = SoundEffect('bgm/eskisky.wav')

    -- Register all assets.
    assets:register(self.name, self.btn_font)
    assets:register(self.name, self.title_font)
    assets:register(self.name, self.background)
    assets:register(self.name, self.cursor)
    assets:register(self.name, self.bgm)
    assets:register(self.name, self.btns)
end


function MainMenu:update(dt)
    if assets:done(self.name) then
        if not self.bgm:isPlaying() then self.bgm:play() end

        -- Update the fader.
        self.fade:update(dt)
    else
        assets:update(self.name)
    end

    return self.next_state
end


function MainMenu:draw()
    if assets:done(self.name) then
        self.background:draw()

        self.btns:draw()
        self.cursor:draw()
        self.fade:draw()
    else
        assets:draw()
    end
end


function MainMenu:keypressed(_)
end


function MainMenu:mousemoved(x, y, dx, dy)
    self.btns:mousemoved(x, y, dx, dy)
    self.cursor:mousemoved(x, y, dx, dy)
end


function MainMenu:mousepressed(x, y, btn)
    self.btns:mousepressed(x, y, btn)
end


function MainMenu:mousereleased(x, y, btn)
    self.btns:mousereleased(x, y, btn)
end


------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------

return MainMenu
