function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad("data/Scenes/Minigames/06_laundry.lua", 'quest', "data/Scenes/06_Laundry_Main.lua")
    
    minigame_screen = screen;
    
    if HasGameState('ActionCompleted', 'LaundyCodeFounded') then
        play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
	    game_state.level_mode = 'mini_game';
	else
	    ShowMonolog('C_06_NO_CODE_HINT', 'C_06_NO_CODE_HINT');
    end
    
    Key = {5, 13, 21, 33}
    Key_flag = {false, false, false, false}
    
    OBJECTS_COUNT = 36
    
    Objects = {}
    for i = 1, OBJECTS_COUNT do
		Objects[i] = {}
		Objects[i].Entity = screen:GetObject('button_'..i);
		Objects[i].index = i;
    end
    
    UpdateAll(true);
    
    if IsCompleted() then
		FinishMiniGame()
	end
            
    RegisterToolFindInfo('06_key', 'item_key', 
												function(tool_name)
													if (HasGameState('ClosedScene', game_state.scene_path) == false) then
														AddGameState('ClosedScene', game_state.scene_path);
														game_state.complited = true;
													end
													if (HasGameState('MiniGameCompleted', '06_laundry') == false) then
														AddGameState('MiniGameCompleted', '06_laundry');
													end
													
													TimerDoString('StartLevel("'..game_state.parent_scene..'")', 0.2, minigame_screen:GetTimer())
                                                end,
                                                
                                                function(tool_name)
													game_state.complited = true;
                                                end);
	
	if not IsCompleted() then
		screen:GetObject('item_key'):SetOpacity(0);
		if HasGameState('ActionCompleted', 'LaundyCodeFounded') and not game_state.case_open then
            ShowMonolog('C_06_CODE_HINT', 'C_06_CODE_HINT');
        end
	end
end

function OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
end

function OnAllItemsFound()
    DefaultOnAllItemsFound()
end

function OnLose()
    DefaultOnLose()
end

function OnExit()
    DefaultOnExit()
end

function OnClose()
    DefaultOnClose()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function GetObjectByName(obj_name)
	for i = 1,OBJECTS_COUNT do
		if(obj_name == Objects[i].Entity:GetName()) then
			return Objects[i];
		end
	end
	
	return nil;
end

function IsCompleted()
    if HasGameState(game_state.scene_path .. 'L1_Drower') then
        return true
    end

	local active_buttton_count = 4;
	for i = 1, active_buttton_count do
		if(Key_flag[i] == false) then
			return false;
		end
	end
	
	return true;
end

function UpdateAll(init)
	for i = 1,OBJECTS_COUNT do
		Objects[i].Entity:GetAnimation():SetFrame(0);
	end
	
	local active_buttton_count = 4;
	for i = 1, active_buttton_count do
		if init then
			if HasGameState(game_state.scene_path .. Key[i], 'true') then
				Key_flag[i] = true
				minigame_screen:GetObject('active_'..i):SetVisible(true)
			else
				Key_flag[i] = false;
				minigame_screen:GetObject('active_'..i):SetVisible(false);
			end
		end
		if not init then
			Key_flag[i] = false;
			minigame_screen:GetObject('active_'..i):SetVisible(false);
			if HasGameState(game_state.scene_path .. Key[i]) then
				RemoveGameState(game_state.scene_path .. Key[i])
			end
		end		
	end
end

function IsActive(index)
	local active_buttton_count = 4;
	for i = 1, active_buttton_count do
		if(index == Key[i]) then
			if(Key_flag[i] == false) then
				Key_flag[i] = true;
				minigame_screen:GetObject('active_'..i):SetVisible(true);
				return 1;
			else
				Key_flag[i] = false;
				minigame_screen:GetObject('active_'..i):SetVisible(false);
				return 2;
			end
		end
	end
	
	return 3;
end

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

	obj = GetObjectByName(obj_name);
	
	if not game_state.case_open then
		if(obj ~= nil) then
		    
		    if game_state.level_mode ~= 'mini_game' then
		        ShowMonolog('C_06_NO_CODE_HINT', 'C_06_NO_CODE_HINT');
		    else
                PlaySound('06_PressButtons')
                
                obj.Entity:GetAnimation():SetFrame(1);
                
                local flag = IsActive(obj.index); 
                if(flag == 1) then
                    obj.Entity:GetAnimation():SetFrame(1);
                    AddGameState(game_state.scene_path .. obj.index, 'true')
                elseif(flag == 2) then
                    obj.Entity:GetAnimation():SetFrame(0);
                elseif(flag == 3) then
                    UpdateAll();
                    obj.Entity:GetAnimation():SetFrame(1);
                    TimerDoString("UpdateAll();", 0.2, play_field:GetTimer())
                end
                
                if(IsCompleted()) then
                    FinishMiniGame();
                end
            end
		end
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function StartQuestMode()
	--game_state.level_mode = 'quest';
	--play_field:GetLevelScreen():GetPanel():SetMode(PM_QUEST)
	play_field:GetLevelScreen():GetPanel():SetDelayTimer( play_field:GetLevelScreen():GetPanel():GetMaxDelayTimer() )
	PanelOnObjectMouseLeave(play_field:GetLevelScreen():GetPanel():GetObject('button_r_skip'))
	play_field:GetLevelScreen():GetPanel():StopHintTimer()
end

function FinishMiniGame(skip)
	game_state.case_open = true
		
	local delay = 0.3
	local hgs_flag = HasGameState(game_state.scene_path .. 'L1_Drower')
	
	if hgs_flag then
		delay = 0
	else
		AddGameState(game_state.scene_path .. 'L1_Drower', 'true')
	end
	
	minigame_screen:GetObject('L1_Drower'):Move(0.0, 167.0, delay);
	
	PlaySound('06_OpenCashRegister')
	
	if skip == true then
	    AddGameState(game_state.scene_path .. 'L1_Drower', 'true')
	end
	if not hgs_flag then
		TimerDoString('play_field:GetObject("item_key"):ChangeOpacity(0, 1, 0.6)', 0.3, play_field:GetTimer())
	end
	
	StartQuestMode()
end

function SkipMiniGame()
    FinishMiniGame(true);
end

function OnObjectMouseUp(object)
    DefaultOnObjectMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
    DefaultOnToolFound(tool)
end

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

function OnObjectReachPanel(object)
    DefaultOnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
    DefaultOnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
    DefaultOnSetTool(tool)
end

function LevelLoaded(level)
end

function OnSceneDestroy(playfield)
    DefaultOnSceneDestroy(playfield)
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/06_laundry/background.png',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/L2_table.png',0,0,849,321,1,0)
	anim:SetHotSpot(424,160)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('L2_table',anim,519,407,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/L1_Drower.png',0,0,841,499,1,0)
	anim:SetHotSpot(420,249)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('L1_Drower',anim,519,500,97,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_1.png',0,0,43,36,2,0)
	anim:SetHotSpot(21,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_1',anim,291,409,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_2.png',0,0,44,41,2,0)
	anim:SetHotSpot(22,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_2',anim,287,369,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_3.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_3',anim,284,327,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_4.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_4',anim,284,281,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_5.png',0,0,44,48,2,0)
	anim:SetHotSpot(22,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_5',anim,284,237,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_6.png',0,0,45,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_6',anim,283,194,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_7.png',0,0,45,43,2,0)
	anim:SetHotSpot(22,21)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_7',anim,286,148,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_8.png',0,0,44,38,2,0)
	anim:SetHotSpot(22,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_8',anim,290,109,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_9.png',0,0,44,31,2,0)
	anim:SetHotSpot(22,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_9',anim,297,74,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_10.png',0,0,43,36,2,0)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_10',anim,439,411,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_20.png',0,0,44,41,2,0)
	anim:SetHotSpot(22,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_11',anim,438,370,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_30.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_12',anim,438,329,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_40.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_13',anim,438,285,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_50.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_14',anim,438,236,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_60.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_15',anim,440,191,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_70.png',0,0,44,44,2,0)
	anim:SetHotSpot(22,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_16',anim,441,151,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_80.png',0,0,43,38,2,0)
	anim:SetHotSpot(22,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_17',anim,442,113,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_90.png',0,0,43,30,2,0)
	anim:SetHotSpot(22,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_18',anim,444,76,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_100.png',0,0,43,36,2,0)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_19',anim,594,411,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_200.png',0,0,43,40,2,0)
	anim:SetHotSpot(22,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_20',anim,595,374,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_300.png',0,0,44,45,2,0)
	anim:SetHotSpot(22,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_21',anim,596,333,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_400.png',0,0,44,48,2,0)
	anim:SetHotSpot(22,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_22',anim,597,287,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_500.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_23',anim,598,242,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_600.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_24',anim,597,199,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_700.png',0,0,44,44,2,0)
	anim:SetHotSpot(22,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_25',anim,598,157,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_800.png',0,0,43,38,2,0)
	anim:SetHotSpot(22,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_26',anim,598,116,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_900.png',0,0,43,31,2,0)
	anim:SetHotSpot(22,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_27',anim,600,79,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_a.png',0,0,43,39,2,0)
	anim:SetHotSpot(22,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_28',anim,749,113,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_b.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_29',anim,751,154,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_c.png',0,0,43,36,2,0)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_30',anim,742,425,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_d.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_31',anim,751,291,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_e.png',0,0,44,32,2,0)
	anim:SetHotSpot(22,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_32',anim,743,76,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_f.png',0,0,44,44,2,0)
	anim:SetHotSpot(22,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_33',anim,752,199,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_g.png',0,0,43,40,2,0)
	anim:SetHotSpot(22,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_34',anim,747,385,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_h.png',0,0,44,46,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_35',anim,751,340,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_red.png',0,0,44,47,2,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('button_36',anim,753,244,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_5_a.png',0,0,63,61,1,0)
	anim:SetHotSpot(31,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('active_1',anim,287,236,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_40_a.png',0,0,63,61,1,0)
	anim:SetHotSpot(31,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('active_2',anim,438,283,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_300_a.png',0,0,63,61,1,0)
	anim:SetHotSpot(31,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('active_3',anim,596,332,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/LK3_knopka_f_a.png',0,0,63,61,1,0)
	anim:SetHotSpot(31,30)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('active_4',anim,752,197,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_laundry/item_key.png',0,0,137,46,1,0)
	anim:SetHotSpot(68,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('item_key',anim,525,589,198,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
