-- HODialogs

HODialogs.mVisible = true

local tFullscreen = false
----------------------------------------------------------------------------------------------------
function HODialogs:show(aState)
	tFullscreen = is_fullscreen()
    if HODialogs.mVisible == aState then
        return
    end

    if aState then
        HODialogs:set_visible(true)
        HODialogs:set_active(true)
    end

    HODialogs:initButton("btn_continue", function() HODialogs:show(false)  end)
    HODialogs:initButton("btn_options", function() HODialogs:switchScreen("options") end)
    HODialogs:initButton("btn_main_menu", function() end)
    HODialogs:initButton("btn_pause_quit", function() quit_confirm() end)

    HODialogs.mOptionsState = Profile:getUserData("Options")
    if HODialogs.mOptionsState then
        HODialogs:apply_options(true,false)
    end

    HODialogs:initButton("btn_back", function() 
        HODialogs:apply_options(false,true)
    end)
    
    HODialogs:initButton("btn_apply", function()
        HODialogs:apply_options(true,true)
    end)

    HODialogs:initCheckBox("cb_fullscreen", function(aState)
        toggle_fullscreen(aState) 
        HODialogs.mOptionsState.fullscreen.changed = true
    end)

    HODialogs:initScrollBar("sfx", function(aValue)
        media_mgr():setVolume(aValue, group_effect) 
        HODialogs.mOptionsState.effect.changed = true
    end)
    HODialogs:initScrollBar("music", function(aValue)
        media_mgr():setVolume(aValue, group_music) 
        HODialogs.mOptionsState.music.changed = true
    end)

    HODialogs:initButton("btn_retry", function() fade_panel:fade_out() stop_all_effects() HOLevel:reset() HODialogs:show(false) end)
    HODialogs:initButton("btn_leave", function() level_mgr:switch_to_level("Map") fade_panel:fade_out() HODialogs:show(false) end)

    --[[HODialogs:initButton("btn_win_continue", function() 
        timer_mgr:remove_timer("level_win")
        Levels.items[Profile:getLevel() - 1]:onWin() 
        HODialogs:show(false)
    end)]]
    
    HODialogs:initButton("btn_win_level_five_continue", function() 
        timer_mgr:remove_timer("level_win")
        Levels.items[Profile:getLevel() - 1]:onWin() 
        --Levels:getCurrentLevel():onWin() 
        HODialogs:show(false)
    end)
    
    HODialogs:initButton("btn_win_five_continue", function() 
        level_mgr:switch_to_level("Map") 
        fade_panel:fade_out() 
        HODialogs:show(false)
    end)
    
    HODialogs:set_element_eh("ztm", function() end)
    
    HODialogs:initButton("btn_main_menu", function() level_mgr:switch_to_level("MainMenu") fade_panel:fade_out() HODialogs:show(false) end)

    Inventory:dropElement()
    HODialogs.mVisible = aState

    --HODialogs:switchScreen("menu")
    --HODialogs:get_element("panel"):setVisible(aState)

    if aState then
        HODialogs:get_element("panel"):setVisible(true)
        HODialogs:get_element("panel"):setAlpha(0.0)
        --text_elem:updateTextColor()
        local step = 0
        timer_mgr:add_timer(
            function()
                step = step + 1
                HODialogs:get_element("panel"):setAlpha(step / 30)
                --text_elem:updateTextColor()
            end, 0.01, 30)
    else
        HODialogs:get_element("panel"):setVisible(false)
        HODialogs:onHide()
    end
end

----------------------------------------------------------------------------------------------------
function HODialogs:onHide()
    HODialogs:set_visible(false)
    HODialogs:set_active(false)
end

----------------------------------------------------------------------------------------------------
function HODialogs:isVisible()
    return HODialogs.mVisible
end

----------------------------------------------------------------------------------------------------
function HODialogs:apply_options(apply, with_dialog_hide)
    if apply then 
        HODialogs.mOptionsState.fullscreen.changed = false
        HODialogs.mOptionsState.fullscreen.value = is_fullscreen()
        HODialogs:setCheckBox("cb_fullscreen", HODialogs.mOptionsState.fullscreen.value)
        HODialogs.mOptionsState.effect.changed = false
        HODialogs.mOptionsState.effect.value = media_mgr():getVolume(group_effect)
        HODialogs:setScrollBarBit("sfx", HODialogs.mOptionsState.effect.value)
        HODialogs.mOptionsState.music.changed = false
        HODialogs.mOptionsState.music.value = media_mgr():getVolume(group_music)
        HODialogs:setScrollBarBit("music", HODialogs.mOptionsState.music.value)
        Profile:setUserData("Options", HODialogs.mOptionsState)
        save_game()
    else 
        if HODialogs.mOptionsState.fullscreen.changed then
            HODialogs.mOptionsState.fullscreen.changed = false
            toggle_fullscreen(HODialogs.mOptionsState.fullscreen.value)
        end
        if HODialogs.mOptionsState.effect.changed then
            HODialogs.mOptionsState.effect.changed = false
            media_mgr():setVolume(HODialogs.mOptionsState.effect.value, group_effect)
        end
        if HODialogs.mOptionsState.music.changed then
            HODialogs.mOptionsState.music.changed = false
            media_mgr():setVolume(HODialogs.mOptionsState.music.value, group_music) 
        end
    end
    if with_dialog_hide then HODialogs:show(false) end
end

----------------------------------------------------------------------------------------------------
function HODialogs:switchScreen(aScreen)
    HODialogs:get_element("options"):setVisible(aScreen == "options")
    HODialogs:get_element("menu"):setVisible(aScreen == "menu")
    HODialogs:get_element("out_time"):setVisible(aScreen == "out_time")
    HODialogs:get_element("level_win"):setVisible(aScreen == "level_win")
    HODialogs:get_element("loc_win"):setVisible(aScreen == "loc_win")
    HODialogs:get_element("loc_five_win"):setVisible(aScreen == "loc_five_win")
    HODialogs:get_element("level_five_win"):setVisible(aScreen == "level_five_win")
    HODialogs:get_element("game_win"):setVisible(aScreen == "game_win")
end

----------------------------------------------------------------------------------------------------
function HODialogs_UPDATE()
	return 0
end

----------------------------------------------------------------------------------------------------
function HODialogs:initButton(aName, aFunc)
    HODialogs:set_element_eh(aName, aFunc)

    HODialogs:set_element_eh(aName,
        function() HODialogs:play_clip(aName.."_view", {frame_begin = 1, frame_end = 1}) end,
         {eh_type = eiRollOver})

    HODialogs:set_element_eh(aName,
        function() HODialogs:play_clip(aName.."_view", {frame_begin = 0, frame_end = 0}) end,
        {eh_type = eiRollOut})
end

----------------------------------------------------------------------------------------------------
function HODialogs:initCheckBox(aName, aFunc)

    --HODialogs:play_clip(aName.."_view", {frame_begin = 1, frame_end = 1})

    HODialogs:set_element_eh(aName,
        function()
            tFullscreen = not tFullscreen
            HODialogs:setCheckBox(aName,tFullscreen)
            aFunc(tFullscreen)
        end)
end

----------------------------------------------------------------------------------------------------
function HODialogs:setScrollBarBit(aName, value)
    local bar = HODialogs:get_element('bar_'..aName)
    local bit = HODialogs:get_element_child(bar, 'bit', true)
    local area = HODialogs:get_element_child(bar, 'area', true)
    
    local barpos = area:getPos()
    local globalbarpos = area:getPos(true)
    local xbardiff = globalbarpos.x - barpos.x
    
    local left = area:getPos(true).x
    local width = area:getSize().x
    
    local bitpos = bit:getPos()
    
    local diff = xbardiff
    
    local offset =  value * width 
    local x_coord =  offset - (width/2)
    bit:setPos(Vec2( x_coord , bitpos.y))
end

----------------------------------------------------------------------------------------------------
function HODialogs:setCheckBox(aName, value)
    if value then
        HODialogs:play_clip(aName.."_view", {frame_begin = 1, frame_end = 1})
    else
        HODialogs:play_clip(aName.."_view", {frame_begin = 0, frame_end = 0})
    end
end

----------------------------------------------------------------------------------------------------
function HODialogs:initScrollBar(aName, aFunc)

    local bar = HODialogs:get_element('bar_'..aName)
    local bit = HODialogs:get_element_child(bar, 'bit', true)
    local area = HODialogs:get_element_child(bar, 'area', true)
    local left = area:getPos(true).x
    local width = area:getSize().x

    local dragListener = function()
            
            local local_x = mouse:getPos(true).x
            --trace(local_x..' ['..left..', '..(left + width)..']')
            if local_x < left - width / 2 or local_x > left + width / 2 then
                return
            end

            local x = (local_x - left + width / 2) / width
            trace(x)
            local finalValue = 0.0
            local cap = 0.1
            if x > cap and x < 1.0 - cap then
                finalValue = (x - cap) / (1.0 - cap - cap)
            elseif x >= 1.0 - cap then
                finalValue = 1.0
            end
            HODialogs:setScrollBarBit(aName, x)
            --trace(finalValue)
            aFunc(finalValue)
        end
    HODialogs:set_element_eh('bar_'..aName, dragListener, {eh_type = eiDrag})
end

----------------------------------------------------------------------------------------------------
function HODialogs_INIT()
    trace('HODialogs_INIT')
    HODialogs:show(false)
end

----------------------------------------------------------------------------------------------------
function HODialogs:resetButtonHandler(aName, aFunc)
    HODialogs:initButton(aName, aFunc)
end
