
MainMenu = {}
MainMenu.mDisplay = level_mgr:get_layer("MainMenu")

--local sLevel = Map

----------------------------------------------------------------------------------------------------
function MainMenu:processChildren(aDisplay, aElemName, aPrefix, aFunc)
    scene = aDisplay:get_element(aElemName)

    local size = aDisplay:get_element_count()
    for i = 0, size - 1 do
        elem = aDisplay:get_element_at(i)
        if scene == nil or scene:isParentOf(elem) then
            elemName = elem:getName()
            if string.sub(elemName, 0, aPrefix:len()) == aPrefix then
                aFunc(elem, parent_name)
            end
        end
    end
    
end

----------------------------------------------------------------------------------------------------
function MainMenu:initButton(aName, aFunc)
    MainMenu:showButton(aName, true)
    MainMenu.mDisplay:set_element_eh(aName, aFunc)

    MainMenu.mDisplay:set_element_eh(aName,
        function()
            if not NameInput.open then
                MainMenu.mDisplay:play_clip(aName.."_view", {frame_begin = 1, frame_end = 1})
            end
        end,
        {eh_type = eiRollOver})

    MainMenu.mDisplay:set_element_eh(aName,
        function()
            if not NameInput.open then
                MainMenu.mDisplay:play_clip(aName.."_view", {frame_begin = 0, frame_end = 0})
            end
        end,
        {eh_type = eiRollOut})
end

----------------------------------------------------------------------------------------------------
function MainMenu:showButton(aName, state)
    MainMenu.mDisplay:get_element(aName..'_view'):setVisible(state)
    MainMenu.mDisplay:get_element(aName..'_caption'):setVisible(state)
    MainMenu.mDisplay:get_element(aName):setActive(state)
end

----------------------------------------------------------------------------------------------------
function MainMenu:onShow()
    Panel:show(false)
    Panel:showTimer(false)
    
    Media:switchBackgroundSet('Main')

    MainMenu.mDisplay:start_amb_effects()
    
    local func = function(elem)
        play_clip(elem:getCharacter(), {looped = true})
    end

    MainMenu:processChildren(MainMenu.mDisplay, "", "amb_", func)

    MainMenu:initButton("btn_options",
        function()
            if not NameInput.open then
                MainMenu.mDisplay:stop_amb_effects()
                HODialogs:show(true)
                HODialogs:resetButtonHandler('btn_back', function() HODialogs:apply_options(false,true) MainMenu.mDisplay:start_amb_effects() end)
                HODialogs:resetButtonHandler('btn_apply', function() HODialogs:apply_options(true,true) MainMenu.mDisplay:start_amb_effects() end)
                HODialogs:switchScreen("options")
            end
        end)

    MainMenu:initButton("btn_quit",
            function()
                if not HODialogs.mVisible and not NameInput.open then
                    quit_confirm()
                end
            end)

    MainMenu:initButton("btn_credits",
        function()
            if not HODialogs.mVisible and not NameInput.open then
                level_mgr:switch_to_level("Credits")
                fade_panel:fade_out()
            end
        end)

  
    if not ProfileList:hasCurrent() then
        MainMenu.mNameField:setText('ids_new_user')
        MainMenu:initButton("btn_play",
            function()
                if not HODialogs.mVisible and not NameInput.open then
                    NameInput:show(onNameEnter)
                end
            end)
    else
        MainMenu.mNameField:setUnicodeSumbols(ProfileList:getCurrentName())
        MainMenu:initButton("btn_play",
            function()
                if not HODialogs.mVisible and not NameInput.open then
                    level_mgr:switch_to_level("Map")
                    fade_panel:fade_out()
                end
            end)
    end

    if ProfileList:hasList() then
        MainMenu:initButton("btn_change_player",
            function()
                if not HODialogs.mVisible and not NameInput.open then
                    level_mgr:switch_to_level("ProfileScreen")
                    fade_panel:fade_out()
                end
            end)
        MainMenu.mDisplay:get_element('change_back'):setVisible(true)
        
        MainMenu:initButton("btn_extras",
        function()
            if not NameInput.open then
                level_mgr:switch_to_level("LockExtras")
                fade_panel:fade_out()
            end
        end)
    else
        MainMenu:showButton('btn_change_player', false)
        MainMenu.mDisplay:get_element('change_back'):setVisible(false)
    end
    --addInputListener(MainMenu.mNameField)
    --MainMenu.mNameField:reset()
end

----------------------------------------------------------------------------------------------------
function onNameEnter(aName)
    local msg = ProfileList:add(aName)
    if msg ~= nil then
        return msg
    end

    level_mgr:switch_to_level('Map')
    fade_panel:fade_out()
    return nil
end

----------------------------------------------------------------------------------------------------
function MainMenu:onHide()
    --removeInputListener(MainMenu.mNameField)
    --Media:stopBackground('MainMenu')
    MainMenu.mDisplay:stop_amb_effects()
end

----------------------------------------------------------------------------------------------------
function MainMenu:ON_SHOW()
	--mouse:setCursorFrame("default")
end


----------------------------------------------------------------------------------------------------
function MainMenu_UPDATE()
	return 0
end

----------------------------------------------------------------------------------------------------
function MainMenu_INIT()
    MainMenu.mNameField = MainMenu.mDisplay:get_element('btn_player_name_caption')
    --MainMenu.mNameField = EditTextElem:new(MainMenu.mDisplay:get_element('btn_player_name_caption'),
        --{filter = {92, 47, 58, 42, 63, 60, 62, 124, 32}, max_length = 12})
end
