


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

LockExtras.mExtras = {}
LockExtras.mGames = {}

LockExtras.mCostExtras = {8,8,8,8,8,8,8,8,8}
--LockExtras.mCostExtras = {0,0,0,0,0,0,0,0,0}
LockExtras.mCostGames = {14,14}
--LockExtras.mCostExtras = {4,3,3,3,3,3,3,3,5}
--LockExtras.mCostGames = {3,3,4}

LockExtras.mSelect = 0
LockExtras.mSelectGame = 0


----------------------------------------------------------------------------------------------------
function LockExtras:onShow()

    Panel:show(false)
    Panel:showTimer(false)
    
--    Media:playBackground('Extras')

    LockExtras.mExtras = Profile:getUserData('lock_extras')
    if LockExtras.mExtras == nil then
        LockExtras.mExtras = {}
        for i = 1, 9 do
            local extra = {}
            extra.lock = true
            table.insert(LockExtras.mExtras, extra)
        end
    end
    
    LockExtras.mDisplay:get_element('big_frame_area'):setVisible(false)
    LockExtras.mDisplay:set_element_eh('backclick', function()  end)
    
    for i = 1, 9 do
        LockExtras.mDisplay:get_element('big_frame_'..i):setVisible(false)
        LockExtras.mDisplay:set_element_eh('big_frame_'..i, function() LockExtras:onPressBig(i) end)
        
        LockExtras.mDisplay:set_element_eh('mini_frame_'..i, function() LockExtras:onPressMini(i) end)
        
        local elem = LockExtras.mDisplay:get_element('mini_frame_'..i)
        play_clip(elem:getCharacter(), {frame_begin = 1, frame_end = 1})
        
        local cost = elem:findChild('cost')
        elem:findChild('text1', true):setVisible(true)
        cost:setVisible(true)
        cost:setText(LockExtras.mCostExtras[i])

        local elem = LockExtras.mDisplay:get_element('mini_frame_'..i)
        play_clip(elem:getCharacter(), {frame_begin = 1, frame_end = 1})
        elem:findChild('glow'):setVisible(false)
        
        if not LockExtras.mExtras[i].lock then
            LockExtras.mSelect = i
            LockExtras:unlock(false)
        end
    end
    
    LockExtras.mGames = Profile:getUserData('lock_games')
    if LockExtras.mGames == nil then
        LockExtras.mGames = {
        {0,'Match2'},
        --{0,'TextTwist'},
        {0,'Collapse'}}
    end
    
    for i = 1, #LockExtras.mGames do
        local elem = LockExtras.mDisplay:get_element(LockExtras.mGames[i][2])
        play_clip(elem:getCharacter(), {frame_begin = 1, frame_end = 1})

        local cost = elem:findChild('cost')
        elem:findChild('text1', true):setVisible(true)
        cost:setVisible(true)
		cost:setText(LockExtras.mCostGames[i])


        if LockExtras.mGames[i][1] ~= 0 then
            LockExtras.mSelectGame = i
            LockExtras:unlock(false)
            play_clip(elem:getCharacter(), {frame_begin = 2, frame_end = 2})
        else
            play_clip(elem:getCharacter(), {frame_begin = 1, frame_end = 1})
            LockExtras.mDisplay:set_element_eh(LockExtras.mGames[i][2], function() LockExtras:onPressGame(i) end)
        end
        
        elem:findChild('glow'):setVisible(false)
    end
    
    LockExtras.mDisplay:set_element_eh('unlock_button', function() LockExtras:unlock(true) end)
    LockExtras.mDisplay:set_element_eh('back_button', function() level_mgr:switch_to_level('MainMenu') fade_panel:fade_out() end)
    LockExtras.mDisplay:set_element_eh('games_button', function() level_mgr:switch_to_level('LockCutscenes') fade_panel:fade_out() end)

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

    LockExtras:calculateKeysText()

    Panel:show(false)
end

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

----------------------------------------------------------------------------------------------------
function LockExtras:onHide()

    Profile:setUserData('lock_extras', LockExtras.mExtras)
    Profile:setUserData('lock_games', LockExtras.mGames)
    save_game()
--    Media:stopBackground('Extras')
end

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

----------------------------------------------------------------------------------------------------
function LockExtras:onPressMini(i)
    LockExtras:unselect()
    LockExtras.mSelect = i
    local elem = LockExtras.mDisplay:get_element('mini_frame_'..LockExtras.mSelect):findChild('glow')
    elem:setVisible(true)
    LockExtras:calculateKeysText()
end

----------------------------------------------------------------------------------------------------
function LockExtras:onPressGame(i)
    LockExtras:unselect()
    LockExtras.mSelectGame = i
    --play_clip(LockExtras.mDisplay:get_element(LockExtras.mGames[i][2]):getCharacter(), {frame_begin = 1, frame_end = 1})
    local elem = LockExtras.mDisplay:get_element(LockExtras.mGames[LockExtras.mSelectGame][2]):findChild('glow')
    elem:setVisible(true)
end

----------------------------------------------------------------------------------------------------
function LockExtras:onPressMiniUnlocked(i)
   LockExtras.mDisplay:get_element('big_frame_area'):setVisible(true)
   LockExtras.mDisplay:get_element('big_frame_'..i):setVisible(true)
   copy_file_to_my_pictures('extra_'..i..'.png')
end

----------------------------------------------------------------------------------------------------
function LockExtras:onPressGameUnlocked(i)
    

    if LockExtras.mGames[i][2] == 'Match2' then
        Match2.currentLayout = get_random(1, #Match2.layouts + 1)
		Match2.ExitLevel = LockExtras.mGames[i][2]
		
		Match2Core:setCloseCallback( function(skip)
			if not skip and LockExtras.mGames[i][1] < 3 then
				LockExtras.mGames[i][1] = LockExtras.mGames[i][1] + 1
				Match2.ExitLevel = LockExtras.mGames[i][2]
				Match2.currentLayout = get_random(1, #Match2.layouts + 1)
			else
				LockExtras.mGames[i][1] = 0 
				Match2.ExitLevel = "LockExtras"
				Match2Core:setCloseCallback(nil)
			end
		end)

    elseif LockExtras.mGames[i][2] == 'Collapse' then
        Collapse.powerups = {'color_bomb', 'bomb', 'shuffle', 'freeze'}
        while #Collapse.powerups > 2 do
            table.remove(Collapse.powerups, get_random(1, #Collapse.powerups + 1))
        end

        local colorsNum = 3
        Collapse.colors = {}
        for i = 1, colorsNum do
            local collect = math.floor(get_random(65 * 7, 76 * 7) / colorsNum)
            table.insert(Collapse.colors, collect)
        end

        Collapse.speed = 1.0 + math.random() / 3.0

        Collapse.ExitLevel = LockExtras.mGames[i][2]
		
		CollapseCore:setCloseCallback( function(skip)
			if not skip and LockExtras.mGames[i][1] < 3 then
				LockExtras.mGames[i][1] = LockExtras.mGames[i][1] + 1
				Collapse.ExitLevel = LockExtras.mGames[i][2]
				-- setup 
				Collapse.powerups = {'color_bomb', 'bomb', 'shuffle', 'freeze'}
				while #Collapse.powerups > 2 do
					table.remove(Collapse.powerups, get_random(1, #Collapse.powerups + 1))
				end

				local colorsNum = LockExtras.mGames[i][1] * 2
				Collapse.colors = {}
				for i = 1, colorsNum do
					local collect = math.floor(get_random(65 * 7, 76 * 7) / colorsNum)
					table.insert(Collapse.colors, collect)
				end

				Collapse.speed = 1.0 + math.random() / 3.0
				-- end setup
			else
				LockExtras.mGames[i][1] = 0 
				Collapse.ExitLevel = "LockExtras"
				CollapseCore:setCloseCallback(nil)
			end
		end)
    else
		TextTwist.ExitLevel = LockExtras.mGames[i][2]
		TextTwist:setCloseCallback( function(skip)
			if not skip and LockExtras.mGames[i][1] < 3 then
				LockExtras.mGames[i][1] = LockExtras.mGames[i][1] + 1
				TextTwist.ExitLevel = LockExtras.mGames[i][2]
			else
				LockExtras.mGames[i][1] = 0 
				TextTwist.ExitLevel = "LockExtras"
				TextTwist:setCloseCallback(nil)
			end
		end)
    end

    level_mgr:switch_to_level(LockExtras.mGames[i][2]) 
    fade_panel:fade_out()
end

----------------------------------------------------------------------------------------------------
function LockExtras:onPressBig(i)
    LockExtras.mDisplay:get_element('big_frame_area'):setVisible(false)
    LockExtras.mDisplay:get_element('big_frame_'..i):setVisible(false)
end

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

function LockExtras:unlock(cost)

    if LockExtras.mSelect ~= 0 then
        
    if(Profile.data.keysCollected < LockExtras.mCostExtras[LockExtras.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 - LockExtras.mCostExtras[LockExtras.mSelect]
    end
    LockExtras:calculateKeysText()
    
		local elem = LockExtras.mDisplay:get_element('mini_frame_'..LockExtras.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 = LockExtras.mSelect
        LockExtras.mDisplay:set_element_eh('mini_frame_'..LockExtras.mSelect, function()  
            LockExtras:onPressMiniUnlocked(select_copy) 
        end)
        LockExtras.mExtras[LockExtras.mSelect].lock = false
    end
    
    if LockExtras.mSelectGame ~= 0 then
    
    if(Profile.data.keysCollected < LockExtras.mCostGames[LockExtras.mSelectGame] 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 - LockExtras.mCostGames[LockExtras.mSelectGame]
    end
    LockExtras:calculateKeysText()
    
		local elem = LockExtras.mDisplay:get_element(LockExtras.mGames[LockExtras.mSelectGame][2])
		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 = LockExtras.mSelectGame
        LockExtras.mDisplay:set_element_eh(LockExtras.mGames[LockExtras.mSelectGame][2], function()
            LockExtras:onPressGameUnlocked(select_copy) 
        end)
       LockExtras.mGames[LockExtras.mSelectGame][1] = 1
    end
    LockExtras:unselect()
    LockExtras.mSelect = 0
    LockExtras.mSelectGame = 0
end