


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

LockCutscenes.mCostLevels = {20,20,20,20,20,20,20,20,20,20}
--LockCutscenes.mCostLevels = {2,2,2,2,2,2,2,2,2,2}

LockCutscenes.mSelect = 0


LockCutscenes.mLinkToLevel = {
1,
3,
6,
9,
12,
15,
18,
21,
24,
27,
}

LockCutscenes.mLevelsActive = {}

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

----------------------------------------------------------------------------------------------------
function LockCutscenes:onShow()

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

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

        local elem = LockCutscenes.mDisplay:get_element("cutscene"..i)
        local glow = elem:findChild('glow')
        play_clip(elem:getCharacter(), {frame_begin = 0, frame_end = 0})
        if LockCutscenes:getCutsceneActive(i) or Profile:getUserData("game_win") then
            play_clip(elem:getCharacter(), {frame_begin = 1, frame_end = 1})
            LockCutscenes.mLevelsActive[i] = true
        end

		elem:findChild('text1', true):setVisible(true)
        local cost = elem:findChild('cost', true)
		cost:setText(LockCutscenes.mCostLevels[i])
        glow:setVisible(false)
        
        LockCutscenes.mDisplay:set_element_eh("cutscene"..i, function() LockCutscenes:onPressMini(i) end)
        
		if not LockCutscenes.mLevelsLockState[i] then
            LockCutscenes.mSelect = i
            LockCutscenes:unlock(false)
        end
    end

    LockCutscenes.mDisplay:set_element_eh('unlock_button', function() LockCutscenes:unlock(true) end)
    LockCutscenes.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
        LockCutscenes.mDisplay:set_element_eh(buttons[btn], function() LockCutscenes.mDisplay:play_clip(buttons[btn], {frame_begin = 1, frame_end = 1}) end, {eh_type = eiRollOver})
        LockCutscenes.mDisplay:set_element_eh(buttons[btn], function() LockCutscenes.mDisplay:play_clip(buttons[btn], {frame_begin = 0, frame_end = 0}) end, {eh_type = eiRollOut})
    end

    LockCutscenes:calculateKeysText()

    Panel:show(false)
end

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

----------------------------------------------------------------------------------------------------
function LockCutscenes:onHide()

    Profile:setUserData('lock_cutscenes', LockCutscenes.mLevelsLockState)
    save_game()
end

----------------------------------------------------------------------------------------------------
function LockCutscenes:unselect()
    if LockCutscenes.mSelect ~= 0 then
        local elem = LockCutscenes.mDisplay:get_element("cutscene"..LockCutscenes.mSelect):findChild('glow')
        elem:setVisible(false)
    end

    LockCutscenes.mSelect = 0
    LockCutscenes.mSelectGame = 0
end

----------------------------------------------------------------------------------------------------
function LockCutscenes:onPressMini(i)
    if not LockCutscenes.mLevelsActive[i] then
        return
    end
    LockCutscenes:unselect()
    LockCutscenes.mSelect = i
    local elem = LockCutscenes.mDisplay:get_element("cutscene"..LockCutscenes.mSelect):findChild('glow')
    elem:setVisible(true)
    LockCutscenes:calculateKeysText()
end

----------------------------------------------------------------------------------------------------
function LockCutscenes:onPressMiniUnlocked(i)
   LockCutscenes:onPressMini(i)
   Cutscene:show(i, function() level_mgr:switch_to_level('LockCutscenes') fade_panel:fade_out() end)
end

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

function LockCutscenes:getCutsceneActive(i)
    local level_id = LockCutscenes.mLinkToLevel[i]
    local level_real = Profile:getLevel()
    
    if level_real > level_id then
		return true
	end
	return false
end

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

function LockCutscenes:unlock(cost)

    if LockCutscenes.mLevelsActive[LockCutscenes.mSelect] == nil or not LockCutscenes.mLevelsActive[LockCutscenes.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 LockCutscenes.mSelect ~= 0 then
        
    if(Profile.data.keysCollected < LockCutscenes.mCostLevels[LockCutscenes.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 - LockCutscenes.mCostLevels[LockCutscenes.mSelect]
    end
    LockCutscenes:calculateKeysText()
		local elem = LockCutscenes.mDisplay:get_element("cutscene"..LockCutscenes.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 = LockCutscenes.mSelect
        LockCutscenes.mDisplay:set_element_eh("cutscene"..LockCutscenes.mSelect, function()  
            LockCutscenes:onPressMiniUnlocked(select_copy) 
        end)
        LockCutscenes.mLevelsLockState[LockCutscenes.mSelect] = false
    end
    LockCutscenes:unselect()
    LockCutscenes.mSelect = 0
    LockCutscenes.mSelectGame = 0
end