function OnSceneLoad(screen)
    --колличество кнопок
    BUTTON_AMOUNT = 12
    
    play_field.game_state = game_state
    MiniGameScreen = screen;
    
	game_state = {}    
    LoadData(screen)
    local pf = play_field
    local level = pf:GetLevelScreen()
    game_state.custom_cursors = {}
    game_state.locked_objects = {}
    game_state.active_objects = {}
    --level:AddTaskSafe(CreateTask('', '', ''))
    --zone = CreateZoomZone('')
    game_state.locker = ReferenceCounter()
    game_state.game_skip = false;
	game_state.timer = screen:GetTimer();
	game_state.screen = screen;
	game_state.HELP_STRING = "HELP_MINIGAME_11";
    
	AmountPress = 4;
    game_state.active_zone = nil
    game_state.inventory_zones = {}
    game_state.game_pause = true
    game_state.game_complite = false;
    Chain = {};
    
    Objects = {};
    
    AddObject(screen, '1', 1);
    AddObject(screen, '2', 2);
    AddObject(screen, '3', 3);
    AddObject(screen, '4', 4);
    AddObject(screen, '5', 5);
    AddObject(screen, '6', 6);
    AddObject(screen, '7', 7);
    AddObject(screen, '8', 8);
    AddObject(screen, '9', 9);
    AddObject(screen, '0', 10);
    AddObject(screen, 'star', 11);
    AddObject(screen, 'num', 12);
    
    OldIndex = 0;
    
    AddMiniGamesGUI(screen);
    --AddMinigameSkipButton(game_state.timer, screen);
    
    TimerDoString('VisibleRand('..AmountPress..')', 2, game_state.timer)
	
	coin_index = 1;
	Coins = {}
	Coins[1] = screen:GetObject('coin_1');
	Coins[2] = screen:GetObject('coin_2');
	Coins[3] = screen:GetObject('coin_3');
	
    screen:SetLayer(2);
end

function PushCoin(index)
	for i = coin_index, 3 do
		Coins[i]:Move(0.0, 42.0, 0.2);
	end
	
	coin_index = coin_index + 1;
end

function OnScreenShown()
end

function VisibleRand(Amount)
	if(game_state.game_complite == false and game_state.game_skip == false) then
		if(Amount > 0) then
			game_state.game_pause = true;
			local Index = 0;
			repeat
				Index = math.random(BUTTON_AMOUNT);
			until Index ~= OldIndex
			
			Objects[Index].Obj:SetVisible(true);
			PlaySound("11_PhoneButton_"..((Index - 1) % 9 + 1))
			OldIndex = Index;
		
			TimerDo(game_state.timer, 1.0, SetVisible, OldIndex, false);
			TimerDo(game_state.timer, 1.5, VisibleRand, Amount - 1);
			
			Chain[table.getn(Chain) + 1] = OldIndex;
		else
			game_state.game_pause = false;
		end
	end
end

function SetVisible(Index, Flag)
	Objects[Index].Obj:SetVisible(Flag);
end 

function PressButton(Index)
	Objects[Index].Obj:SetVisible(true);
	TimerDo(game_state.timer, 0.2, SetVisible, Index, false);
	
	if(Index == Chain[1]) then
		UpdateArray(Chain)
		
		if(table.getn(Chain) < 1) then
			local s = CreateSoundEntity("11_RepeatSuccess")
			s:AttachTo(play_field)
			s:Play(false)
			PushCoin();
			AmountPress = AmountPress + 1;
			if(AmountPress > 6) then
				game_state.game_complite = true;
				TimerDo(game_state.timer, 0.6, ShowDoneButton);
			end
			
			game_state.game_pause = true;
			TimerDo(game_state.timer, 0.4, VisibleAll, "0xFF00CC00");
			TimerDo(game_state.timer, 1.0, UnVisibleAll);
			TimerDo(game_state.timer, 1.5, VisibleRand, AmountPress);
		end	
	else
		Chain = {};
		local s = CreateSoundEntity("11_RepeatFail")
		s:AttachTo(play_field)
		s:Play(false)
		--AmountPress = AmountPress - 1;
		if(AmountPress < 4) then
			AmountPress = 4;
		end
		
		game_state.game_pause = true;
		TimerDo(game_state.timer, 0.4, VisibleAll, "0xFFCC0000");
		TimerDo(game_state.timer, 1.0, UnVisibleAll);
		TimerDo(game_state.timer, 1.5, VisibleRand, AmountPress);
	end
end

function VisibleAll(color)
	for i = 1,BUTTON_AMOUNT do
		Objects[i].Obj:SetVisible(true);
		Objects[i].Obj:GetAnimation():SetColor(color);
	end
end

function UnVisibleAll()
	for i = 1,BUTTON_AMOUNT do
		Objects[i].Obj:SetVisible(false);
		Objects[i].Obj:GetAnimation():SetColor("0xFFFFFFFF");
	end
end

function UpdateArray(Array)
	for i = 2, table.getn(Chain) do
		Chain[i - 1] = Chain[i];
	end
	
	Chain[table.getn(Chain)] = nil;
end

function OnSceneFree(screen)
    FreeData(screen)
end

function AddObject(screen, ObjName, Index)
	Objects[Index] = {};
	Objects[Index].Obj = screen:GetObject(ObjName);
	Objects[Index].Obj:SetVisible(false);
	Objects[Index].Index = Index;
end

function GetObject(object)
	for i = 1,BUTTON_AMOUNT do
		if(Objects[i].Obj:GetName() == object:GetName()) then
			return Objects[i];
		end
	end
	
	return nil;
end

function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true;
		
		return 0.1;
	end
	
	return -1.0;
end

function OnUpdate(dt)
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName()
    local pf = play_field
    local is_wrong_click = true

    is_wrong_click = DefaultOnObjectMouseDown(object)
	
	if(game_state.game_pause == false and game_state.game_complite == false and game_state.game_skip == false) then
		local Obj = GetObject(object);
		if(Obj ~= nil) then
			PressButton(Obj.Index);
			PlaySound("11_PhoneButton_"..((Obj.Index - 1) % 9 + 1))
		end
	end
	
    if is_wrong_click then
        --pf:ShowWrongObjectEffect()
    end    
end

function OnObjectMouseUp(object)
end

function OnObjectWillBeFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
end

function OnObjectReachPanel(object)
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function CanBeHinted(obj)
    if locked_object[obj] ~= true then
        return true
    else
        return false
    end
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
    OnMiniGameButtonPress(title);
end

function OnLostFocus()
end

function OnGainFocus()
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',0,0,45,28,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('1',anim,532,371,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',48,0,45,30,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('2',anim,580,372,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',93,0,45,28,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('3',anim,626,372,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',0,28,45,28,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('4',anim,532,400,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',46,28,46,28,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('5',anim,578,400,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',93,28,46,27,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('6',anim,625,399,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',0,55,46,27,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('7',anim,533,427,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',46,54,46,28,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('8',anim,578,427,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',93,55,46,27,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('9',anim,625,427,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',0,81,48,28,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('star',anim,532,453,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',48,81,45,28,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('0',anim,579,454,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/knop.png',93,81,46,28,1,0)
	anim:SetHotSpot(70,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('num',anim,626,453,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/11_call_box_5/zoom/phone_closed.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('phone_closed',anim,512,384,89,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/dlja_monet.png',0,0,90,131,1,0)
	anim:SetHotSpot(45,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('dlja_monet',anim,614,139,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/sprajt_dlja_monet.png',0,0,90,128,1,0)
	anim:SetHotSpot(45,64)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprajt_dlja_monet',anim,613,140,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/sprajt_dlja_monet2.png',0,0,129,112,1,0)
	anim:SetHotSpot(64,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprajt_dlja_monet2',anim,617,238,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/coin.png',0,0,44,45,1,0)
	anim:SetHotSpot(22,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('coin_1',anim,614,174,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/coin.png',0,0,44,45,1,0)
	anim:SetHotSpot(22,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('coin_2',anim,614,131,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/11_Chain/coin.png',0,0,44,45,1,0)
	anim:SetHotSpot(22,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('coin_3',anim,614,88,101,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
