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

local sLevel = Map

----------------------------------------------------------------------------------------------------
function splitMapLocName(aName)
    return string.match(aName:gsub("_", " "), "(%a+) (%d+)")
end

local map_visible = false

----------------------------------------------------------------------------------------------------
function Map:onShow()

    map_visible = true

    if Profile.data.cutscene ~= 0 then
        Cutscene:show(Profile.data.cutscene, function() Map:showStuff() end)
        Profile.data.cutscene = 0
    else
        Map:showStuff()
    end
end

----------------------------------------------------------------------------------------------------
function Map:showStuff()
    timer_mgr:add_timer(function() if map_visible then Tutorial:enterChapter('Map') end end, 1, 1)

    Media:switchBackgroundSet('HOGameplay')
    Media:soundEvent('map.open')

    Map:showLevel()

    Panel:show(false)
    Panel:showTimer(false)

    Map.mDisplay:play_clip('circles', {looped = true})
    Map.mDisplay:play_clip('screenElements', {looped = true})
    Map.mDisplay:start_amb_effects()

end

----------------------------------------------------------------------------------------------------
function Map:onHide()
    map_visible = false
    Media:stopBackground('Map')
    Tutorial:leaveChapter()
    Map.mDisplay:stop_amb_effects()
end

----------------------------------------------------------------------------------------------------
function Map:showLevel()

    local level = Levels:getCurrentLevel()

    if level == nil then
        return
    end

    if level.levelIdx == Levels:getCount() then
		--reset to begin
		Profile:resetToBegin()
		Profile:setUserData("game_win",true)
		save_game()
		HOLevel:cleanAll()
        Levels:reset()
        Location:resetAll()
		save_game()
        level_mgr:switch_to_level('Credits')
        fade_panel:fade_out()
        return
    end

    local progressAnim = Map.mDisplay:get_element('progressAni')
    play_clip(progressAnim:getCharacter(), {looped = true})

    local progress = Map.mDisplay:get_element('progress')
    local progFrame = math.floor((Profile:getLevel() / 28) * 34)
    play_clip(progress:getCharacter(), {frame_begin = progFrame, frame_end = progFrame})
 
    local locList = level:getLocationsList()

    --levelElem = Map.mDisplay:get_element('level'..)

    for i = 1, 5 do
        local preview = Map.mDisplay:get_element('level'..i)
        preview:setVisible(false)
    end

    local levelClip = Map.mDisplay:get_element('level'..#locList)
    play_clip(levelClip:getCharacter(), {frame_begin = 0})
    levelClip:setVisible(true)

    for i = 1, #locList do
        local preview = levelClip:findChild('stage_'..i, true)
        preview:setVisible(true)
        
        local locName = locList[i]
        local picAnim = levelClip:findChild('preview'..i, true)
        play_clip(picAnim:getCharacter(), {frame_begin = 0})
      
        local pict = levelClip:findChild('picture'..i, true)
        play_clip(pict:getCharacter(), {label_begin = locName, label_end = locName})

        local levelPassed = level:isLocationFinished(locName)
        levelClip:findChild('picInterlace'..i, true):setVisible(levelPassed)

        local info = level:getInfo(locName)

        local location = Location:get(locName)
        location.mCurrentScene = info.scene


        local glow = nil
        size = Map.mDisplay:get_element_count()
        local locElem = levelClip:findChild('Location'..i, true)
        for i = 0, size - 1 do
            local elem = Map.mDisplay:get_element_at(i)
            if locElem:isParentOf(elem, true) then

                if elem:getName() == 'frameGlow' then
                    glow = elem

                elseif elem:getName() == 'challenge' then
                    local challenge_frame = 0
                    if info.challengePassed then
                        challenge_frame = 1
                    end

                    play_clip(elem:getCharacter(), {frame_begin = challenge_frame, frame_end = challenge_frame})

                elseif elem:getName() == "collected" then
                    elem:setText(info.collectedHids)

                elseif elem:getName() == "total" then
                    elem:setText(info.totalHids)

                elseif elem:getName() == "progress" then
                    set_frame_listener(picAnim, picAnim:getCharacter():get_frame_count() - 1,
                    function()
                        play_clip(elem:getCharacter(), {frame_begin = 0, frame_end = info.collectedHids})
                    end)

                elseif elem:getName() == "key" then
                    local key_found = level:isKeysFound(locName)

                    local keyframe = 0
                    if key_found then
                        keyframe = 1
                    end
                    play_clip(elem:getCharacter(), {frame_begin = keyframe, frame_end = keyframe})

                end
            end
        end


        glow:setVisible(false)

        if not levelPassed then
            Map.mDisplay:_set_element_eh(pict,
                function()
                    Tutorial:sendMessage('level.selected')
                    level_mgr:switch_to_level(locName)
                    fade_panel:fade_out()
                    Panel:show(true)
                end)

            Map.mDisplay:_set_element_eh(pict, function() glow:setVisible(true) end, {eh_type = eiRollOver})
            Map.mDisplay:_set_element_eh(pict, function() glow:setVisible(false) end, {eh_type = eiRollOut})
            pict:setActive(true)
        else
            pict:setActive(false)
        end
    end

end

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


----------------------------------------------------------------------------------------------------
function Map_UPDATE()
	return 0
end

----------------------------------------------------------------------------------------------------
function Map_INIT()
    Map.mDisplay:set_element_eh('btn_menu_view',
        function()
            if not Cutscene:isVisible() then
                Bubble:hide(false)
                level_mgr:switch_to_level('MainMenu')
                fade_panel:fade_out()
            end
        end)
end
