


LockLevels = {}
LockLevels.mDisplay = level_mgr:get_layer('LockLevels')

LockLevels.mCostLevels = {10,10,10,10,10,10,10,10,10,10,
                          10,10,10,10,10,10,10,10,10,10}

--LockLevels.mCostLevels = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}

LockLevels.mSelect = 0


LockLevels.mLevels = {
"LocSupplyCloset",
"LocGym",
"LocExecutiveWashroom",
"LocTailor",
"LocBeautySPA",
"LocCoffeshop",
"LocNightClub",
"LocWinterGarden",
"LocSecurityStation",
"LocSwimmingPool",
"LocMailRoom",
"LocCourtyard",
"LocKitchen",
"LocManagersOffice",
"LocConciergeDesk",
"LocPrivateRoom",
"LocObservationDeck",
"LocConferenceRoom",
"LocLaundry",
"LocValetParking"
}

LockLevels.mLevelsActive = {}

-- for compact save data
LockLevels.mLevelsLockState = {}

----------------------------------------------------------------------------------------------------
function LockLevels:onShow()

--    Media:playBackground('Extras')
    
    if HOLevelExtra:isEmpty() then
        for i, v in pairs(LockLevels.mLevels) do
            HOLevelExtra:new(v)
        end
    end

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

    LockLevels.mLevelsLockState = Profile:getUserData('lock_levels')
    if LockLevels.mLevelsLockState == nil then
        LockLevels.mLevelsLockState = {}
        for i = 1, 20 do
            table.insert(LockLevels.mLevelsLockState, true)
            table.insert(LockLevels.mLevelsActive, false)
        end
    end
    
    for i = 1, 20 do

        local elem = LockLevels.mDisplay:get_element(LockLevels.mLevels[i])
        local glow = elem:findChild('glow')
        if LockLevels:getLevelActive(i) then
            play_clip(elem:getCharacter(), {frame_begin = 1, frame_end = 1})
            LockLevels.mLevelsActive[i] = true
        end

        local cost = elem:findChild('cost', true)
        elem:findChild('text1', true):setVisible(true)
        if cost ~= nil then
            cost:setText(LockLevels.mCostLevels[i])
        end

        glow:setVisible(false)
        
        LockLevels.mDisplay:set_element_eh(LockLevels.mLevels[i], function() LockLevels:onPressMini(i) end)
        
		if not LockLevels.mLevelsLockState[i] then
            LockLevels.mSelect = i
            LockLevels:unlock(false)
        end
    end

    LockLevels.mDisplay:set_element_eh('unlock_button', function() LockLevels:unlock(true) end)
    LockLevels.mDisplay:set_element_eh('back_button', function() level_mgr:switch_to_level('LockExtras') fade_panel:fade_out() end)

    local buttons = {'unlock_button', 'back_button'}
    for btn = 1, #buttons do
        LockLevels.mDisplay:set_element_eh(buttons[btn], function() LockLevels.mDisplay:play_clip(buttons[btn], {frame_begin = 1, frame_end = 1}) end, {eh_type = eiRollOver})
        LockLevels.mDisplay:set_element_eh(buttons[btn], function() LockLevels.mDisplay:play_clip(buttons[btn], {frame_begin = 0, frame_end = 0}) end, {eh_type = eiRollOut})
    end

    LockLevels:calculateKeysText()

    Panel:show(false)
end

function LockLevels:calculateKeysText()
    LockLevels.mDisplay:get_element('total_text'):setText(string.format("%d",Profile.data.keysCollected))
    if LockLevels.mSelect ~= 0 then
        LockLevels.mDisplay:get_element('cost_text'):setText(string.format("%d",LockLevels.mCostLevels[LockLevels.mSelect]))
    else
        LockLevels.mDisplay:get_element('cost_text'):setText("-")
    end
end

----------------------------------------------------------------------------------------------------
function LockLevels:onHide()

    Profile:setUserData('lock_levels', LockLevels.mLevelsLockState)
    save_game()
--    Media:stopBackground('Extras')
end

----------------------------------------------------------------------------------------------------
function LockLevels:unselect()
    if LockLevels.mSelect ~= 0 then
        --play_clip(LockLevels.mDisplay:get_element(LockLevels.mLevels[LockLevels.mSelect]):getCharacter(), {frame_begin = 0, frame_end = 0})
        local elem = LockLevels.mDisplay:get_element(LockLevels.mLevels[LockLevels.mSelect]):findChild('glow')
        elem:setVisible(false)
    end

    LockLevels.mSelect = 0
    LockLevels.mSelectGame = 0
end

----------------------------------------------------------------------------------------------------
function LockLevels:onPressMini(i)
    if not LockLevels.mLevelsActive[i] then
        return
    end
    LockLevels:unselect()
    LockLevels.mSelect = i
    --play_clip(LockLevels.mDisplay:get_element(LockLevels.mLevels[LockLevels.mSelect]):getCharacter(), {frame_begin = 1, frame_end = 1})
    local elem = LockLevels.mDisplay:get_element(LockLevels.mLevels[LockLevels.mSelect]):findChild('glow')
    elem:setVisible(true)
    LockLevels:calculateKeysText()
end

----------------------------------------------------------------------------------------------------
function LockLevels:onPressMiniUnlocked(i)
   LockLevels:onPressMini(i)
   HOLevel.ExitTarget = "LockLevels"
   Panel.map_button_callback = function() HOLevelExtra.list[LockLevels.mLevels[i]]:leave() end
   --level_mgr:switch_to_level(LockLevels.mLevels[i])
   --fade_panel:fade_out()
   HOLevelExtra.list[LockLevels.mLevels[i]]:start()
end

----------------------------------------------------------------------------------------------------

function LockLevels:getLevelActive(i)
    local count = Profile.data[LockLevels.mLevels[i]]['pass']
    if count ~= nil and count >= 5 then
        return true
    end
    return false
end

----------------------------------------------------------------------------------------------------

function LockLevels:unlock(cost)

    if LockLevels.mLevelsActive[LockLevels.mSelect] == nil or not LockLevels.mLevelsActive[LockLevels.mSelect] then
        -- not active
        Bubble:show("ids_level_not_active", 180, 300, 650, 100, Bubble.TYPE_NORMAL, true)
        timer_mgr:add_timer( function() Bubble:hide(true)  end, 1, 1,"not_active_level_timer")
        return
    end

    if LockLevels.mSelect ~= 0 then
        
    if(Profile.data.keysCollected < LockLevels.mCostLevels[LockLevels.mSelect] and cost) then
        -- денег мало, длинный шмель :)
        Bubble:show("ids_little_money", 180, 300, 650, 100, Bubble.TYPE_NORMAL, true)
        timer_mgr:add_timer( function() Bubble:hide(true)  end, 1, 1,"not_found_keys_count")
        return
    end

    if cost then
        Profile.data.keysCollected = Profile.data.keysCollected - LockLevels.mCostLevels[LockLevels.mSelect]
    end
    LockLevels:calculateKeysText()
		local elem = LockLevels.mDisplay:get_element(LockLevels.mLevels[LockLevels.mSelect])
		elem:findChild('text1', true):setVisible(false)
        elem:findChild('cost', true):setVisible(false)
        
        play_clip(elem:getCharacter(), {frame_begin = 2, frame_end = 2})
        local select_copy = LockLevels.mSelect
        LockLevels.mDisplay:set_element_eh(LockLevels.mLevels[LockLevels.mSelect], function()  
            LockLevels:onPressMiniUnlocked(select_copy) 
        end)
        LockLevels.mLevelsLockState[LockLevels.mSelect] = false
    end
    LockLevels:unselect()
    LockLevels.mSelect = 0
    LockLevels.mSelectGame = 0
end