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

local make_class    = require 'src.utils.classes'
local constants     = require 'src.gstates.menus.const'
local settings      = require 'src.utils.settings'
local sound_manager = require 'src.sound.sndmngr'
local assets        = require 'src.utils.asstmngr'
local BaseMenu      = require 'src.gstates.menus.base'
local Color         = require 'src.utils.color'
local HBox          = require 'src.ui.hbox'
local Label         = require 'src.ui.label'
local TextButton    = require 'src.ui.textbtn'
local TextInput     = require 'src.ui.textinpt'
local Checkbox      = require 'src.ui.chkbox'
local Bar           = require 'src.ui.bar'
local Dialog        = require 'src.ui.dialog'
local Font          = require 'src.ui.font'


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

local OptionsMenu = make_class(BaseMenu)


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

function OptionsMenu:_init(parent, title_font, button_font)
    BaseMenu._init(self, parent, "Options Menu", constants.OPTIONS_MENU, 'imgs/cpu.png')

    self.subtitle_font = Font('fonts/BBrick.ttf', 25)

    -- Create UI elements of the menu.
    self.container:add(Label(nil, nil, 'Options', title_font, Color(215, 0, 0), true))

    -- Permadeath checkbox.
    local box = HBox()

    box:add(Label(nil, nil, 'Enable Permadeath: ', button_font))
    box:add(Checkbox(
        function()
            settings.permadeath = not settings.permadeath
        end,
        function()
            return settings.permadeath
        end,
        17,
        15))

    self.container:add(box)

    -- Control binding dialog.
    local dlg = Dialog('imgs/tchcmp1.png', nil, 15, nil, nil, button_font)

    dlg:add(Label(nil, nil, 'Set Controls', self.subtitle_font, Color(215, 0, 0), true))

    box = HBox()
    box.spacing = 30
    -- text, font, x, y, callback, value, float, base_col, sel_color, press_col
    box:add(TextInput('Step Forw.',
                      button_font,
                      nil, nil,
                      function(v) settings.forward = v end,
                      function() return settings.forward end))
    box:add(TextInput('Step Back',
                      button_font,
                      nil, nil,
                      function(v) settings.backward = v end,
                      function() return settings.backward end))

    dlg:add(box)

    box = HBox()
    box.spacing = 40
    box:add(TextInput('Step Left',
                      button_font,
                      nil, nil,
                      function(v) settings.stepleft = v end,
                      function() return settings.stepleft end))
    box:add(TextInput('Step Right',
                      button_font,
                      nil, nil,
                      function(v) settings.stepright = v end,
                      function() return settings.stepright end))

    dlg:add(box)

    box = HBox()
    box.spacing = 42
    box:add(TextInput('Turn Left',
                      button_font,
                      nil, nil,
                      function(v) settings.turnleft = v end,
                      function() return settings.turnleft end))
    box:add(TextInput('Turn Right',
                      button_font,
                      nil, nil,
                      function(v) settings.turnright = v end,
                      function() return settings.turnright end))

    dlg:add(box)

    box = HBox()

    box:add(TextButton(
        'Set Controls',
        button_font,
        nil,
        nil,
        function()
            dlg:show()
        end))

    self.container:add(box)

    -- Sound volume bar.
    box = HBox()

    box:add(Label(nil, nil, 'Sound Volume: ', button_font))
    box:add(Bar(
        function(v)
            settings.soundvol = math.ceil(v)
        end,
        function()
            return settings.soundvol
        end,
        0, 100,
        170, 15))

    self.container:add(box)

    -- Music volume bar.
    box = HBox()
    box.spacing = 13

    box:add(Label(nil, nil, 'Music Volume: ', button_font))
    box:add(Bar(
        function(v)
            settings.musicvol = math.ceil(v)
            sound_manager:update_volumes()
        end,
        function()
            return settings.musicvol
        end,
        0, 100,
        170, 15))

    self.container:add(box)

    -- Back button.
    box = HBox()
    box:add(TextButton(
        'Go Back',
        button_font,
        nil,
        nil,
        function()
            settings:save_settings()
            parent.next_menu = constants.MAIN_MENU
        end))
    self.container:add(box)

    -- Register the dialog with the asset manager.
    assets:register(self.name, dlg)
    assets:register(self.name, self.subtitle_font)
end


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

return OptionsMenu
