function OnSceneLoad(screen)
	LoadData(screen)

	OBJECTS_AMOUNT = 8
	
    DefaultOnSceneLoad('data/Scenes/Minigames/09_Tails.lua', 'quest', 'data/Scenes/09_Bank_Office_Cupboard_HO.lua')
    
    game_state.can_used = false;
    game_state.skip_minigame = false;
    game_state.active = false;
    game_state.complited = false;
    game_state.screen = screen;
    
    game_field = {}
    for i = 1, 3 do
		game_field[i] = {}
		for j = 1, 3 do
			game_field[i][j] = 1;
		end
    end
    
    game_field[3][3] = 2;
    free_i = 3;
    free_j = 3;
    
    Objects = {}
    
    for i = 1, OBJECTS_AMOUNT do
		Objects[i] = {};
		Objects[i].Obj = game_state.screen:GetObject(""..i);
		Objects[i].indexI = (i - 1) % 3 + 1;
		Objects[i].need_indexI = (i - 1) % 3 + 1;
		Objects[i].indexJ = math.floor((i - 1)/ 3) + 1;
		Objects[i].need_indexJ = math.floor((i - 1)/ 3) + 1;
    end
    
    Objects[5].Obj:SetVisible(false);
    
    CloseSafe();
    
    if(LoadGame() == 0) then
		Reverce();
		for i = 1,OBJECTS_AMOUNT do
			if(HasGameState('MINI_GAME_09_TAILS_STATE_'..i, ""..(Objects[i].indexI + (Objects[i].indexJ  - 1) * 3)) == false) then
				AddGameState('MINI_GAME_09_TAILS_STATE_'..i, ""..(Objects[i].indexI + (Objects[i].indexJ - 1) * 3))
			end
		end
	end	
	
	RegisterTailToolApply();
	
	if HasGameState('ToolFound', "09_tail") == false then
		ShowMonolog('C_09_MINI_GAME_TAILS_1', 'C_09_MINI_GAME_TAILS_1')
	elseif level_mode ~= 'mini_game' then
	    ShowMonolog('C_09_MINI_GAME_TAILS_2', 'C_09_MINI_GAME_TAILS_2')
	end
end

function RegisterTailToolApply()
    RegisterToolApplyInfo('09_tail', 'area_tail', nil,  nil,   function(tool_name)        
																	Objects[5].Obj:SetVisible(true);
																	Objects[5].Obj:ChangeOpacity(0.0, 1.0, 0.5);
																	Objects[5].Obj:ChangeScale(0.5, 1.0, 0.2);
																	game_state.active = true;
																	game_state.screen:GetObject("area_tail"):SetVisible(false);
																	level_mode = 'mini_game';
																	play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME)
																	ShowMonolog('C_09_MINI_GAME_TAILS_3', 'C_09_MINI_GAME_TAILS_3')
																	
																	PlaySound('09_PlaceCell')
                                                                end,
                                                                function(tool_name)                                                                 
                                                                    Objects[5].Obj:SetVisible(true);
                                                                    game_state.active = true;
                                                                    game_state.screen:GetObject("area_tail"):SetVisible(false);
                                                                    level_mode = 'mini_game';
																	play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME)
																	
																	if IsComplited() then
                                                                        game_state.complited = true;
                                                                        TimerDoString('OpenSafe()', 1.0, game_state.screen:GetTimer());
                                                                    else
                                                                        ShowMonolog('C_09_MINI_GAME_TAILS_3', 'C_09_MINI_GAME_TAILS_3')
                                                                    end
                                                                end);
end


function LoadGame()
	local flag = 0;
	for j = 1,OBJECTS_AMOUNT + 1 do
		for i = 1,OBJECTS_AMOUNT do
			if(HasGameState('MINI_GAME_09_TAILS_STATE_'..i, ""..j) == true) then
				Objects[i].indexI = (j - 1) % 3 + 1;
				Objects[i].indexJ = math.floor((j - 1)/ 3) + 1;
				Objects[i].Obj:SetXY((Objects[i].indexI  - 1) * 90 + 447, (Objects[i].indexJ - 1)* 95 + 232);
				flag = 1;
			end
		end
	end
	
	for i = 1,3 do
			for j = 1,3 do
				if(GetObjectByIndex(i, j) == nil) then
					game_field[i][j] = 2;
				else
					game_field[i][j] = 1;
				end
			end
		end
	
	return flag;
end

function Save(object)
	for j = 1,OBJECTS_AMOUNT + 1 do
		if(HasGameState('MINI_GAME_09_TAILS_STATE_'..object.Obj:GetName(), ""..j) == true) then
			RemoveGameState('MINI_GAME_09_TAILS_STATE_'..object.Obj:GetName(), ""..j);
		end
	end
	
	AddGameState('MINI_GAME_09_TAILS_STATE_'..object.Obj:GetName(), ""..(object.indexI + (object.indexJ  - 1) * 3));
end

function IsComplited()
	for i = 1,OBJECTS_AMOUNT do
		if(Objects[i].indexI ~= Objects[i].need_indexI or Objects[i].indexJ ~= Objects[i].need_indexJ) then
			return false; 
		end
	end
	
	return true
end

function CloseSafe()
	game_state.screen:GetObject("Seyf2"):SetVisible(false);
	game_state.screen:GetObject("kluch"):SetVisible(false);
	game_state.screen:GetObject("sprait_kruchka"):SetVisible(false);
end

function OpenSafe()
	PlaySound('02_MG_Gather_Lighter');
	game_state.screen:GetObject("Seyf2"):SetVisible(true);
	game_state.screen:GetObject("kluch"):SetVisible(true);
	game_state.screen:GetObject("sprait_kruchka"):SetVisible(true);
	game_state.screen:GetObject("Seyf2"):ChangeOpacity(0.0, 1.0, 0.5);
	game_state.screen:GetObject("kluch"):ChangeOpacity(0.0, 1.0, 0.5);
	game_state.screen:GetObject("sprait_kruchka"):ChangeOpacity(0.0, 1.0, 0.5);
	
	TimerDoString("StartLevel('data/Scenes/09_Bank_Office_Cupboard_HO.lua');", 0.5, game_state.screen:GetTimer())
	if (HasGameState('ClosedScene', game_state.scene_path) == false) then
		AddGameState('ClosedScene', game_state.scene_path);
	end
	
	if (HasGameState('MiniGameCompleted', '09_Tails') == false) then
		AddGameState('MiniGameCompleted', '09_Tails');
	end

end

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

function GetObjectByIndex(index_i, index_j)
	for i = 1,OBJECTS_AMOUNT do
		if(Objects[i].indexI == index_i and Objects[i].indexJ == index_j) then
			return Objects[i];
		end
	end
	
	return nil;
end

function Reverce()
	local reverce_max_amount = 100;
	local reverce_amount = 0;
	
	repeat
		local rand = math.random(4);
		local value = 0;
		
		if(rand == 1) then
			value = MoveTils(GetObjectByIndex(free_i + 1, free_j))
		elseif (rand == 2) then
			value = MoveTils(GetObjectByIndex(free_i - 1, free_j))
		elseif (rand == 3) then
			value = MoveTils(GetObjectByIndex(free_i, free_j + 1))
		elseif (rand == 4) then
			value = MoveTils(GetObjectByIndex(free_i, free_j - 1))
		end
		
		reverce_amount = reverce_amount + value;
	until(reverce_amount >= reverce_max_amount)
end

function MoveTils(object, is_move)
	local flag = 0;
	
	if(object == nil) then
		return 0;
	end
	
	if(game_field[object.indexI + 1] ~= nil and game_field[object.indexI + 1][object.indexJ] == 2) then
		if(is_move ~= nil)then object.Obj:Move(90, 0, 0.2); else object.Obj:SetX(object.Obj:GetX() + 90) end
		PlaySound('09_MG_RotateButton');
		game_field[object.indexI + 1][object.indexJ] = 1;
		game_field[object.indexI][object.indexJ] = 2;
		free_i = object.indexI;
		free_j = object.indexJ;
		object.indexI = object.indexI + 1;
		Save(object);
		flag = 1;
	elseif(game_field[object.indexI - 1] ~= nil and game_field[object.indexI - 1][object.indexJ] == 2) then
		if(is_move ~= nil)then object.Obj:Move(-90, 0, 0.2); else object.Obj:SetX(object.Obj:GetX() - 90) end
		PlaySound('09_MG_RotateButton');
		game_field[object.indexI - 1][object.indexJ] = 1;
		game_field[object.indexI][object.indexJ] = 2;
		free_i = object.indexI;
		free_j = object.indexJ;
		object.indexI = object.indexI - 1;
		Save(object);
		flag = 1;
	elseif(game_field[object.indexI][object.indexJ + 1] == 2) then
		if(is_move ~= nil)then object.Obj:Move(0, 95, 0.2); else object.Obj:SetY(object.Obj:GetY() + 95) end
		PlaySound('09_MG_RotateButton');
		game_field[object.indexI][object.indexJ + 1] = 1;
		game_field[object.indexI][object.indexJ] = 2;
		free_i = object.indexI;
		free_j = object.indexJ;
		object.indexJ = object.indexJ + 1;
		Save(object);
		flag = 1;
	elseif(game_field[object.indexI][object.indexJ - 1] == 2) then
		if(is_move ~= nil)then object.Obj:Move(0, -95, 0.2); else object.Obj:SetY(object.Obj:GetY() - 95) end
		PlaySound('09_MG_RotateButton');
		game_field[object.indexI][object.indexJ - 1] = 1;
		game_field[object.indexI][object.indexJ] = 2;
		free_i = object.indexI;
		free_j = object.indexJ;
		object.indexJ = object.indexJ - 1;
		Save(object);
		flag = 1;
	end
	
	return flag;
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 OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field
	
	if(game_state.complited == false and game_state.skip_minigame == false) then
		local obj = GetObject(obj_name);
		if(obj ~= nil) then
			if(game_state.active == true) then
				game_state.active = false;
				MoveTils(obj, true);
				TimerDoString('game_state.active = true', 0.2, game_state.screen:GetTimer());
			end
			
			if(IsComplited() == true) then
				game_state.complited = true;
				TimerDoString('OpenSafe()', 1.0, game_state.screen:GetTimer());
			end
		end
	end
	
	if HasGameState('ToolFound', "09_tail") and HasGameState('ToolApplied', "09_tail") == false and (obj_name == 'area_tail') then
		ShowMonolog('C_09_MINI_GAME_TAILS_2', 'C_09_MINI_GAME_TAILS_2');
	end
	if (HasGameState('ToolFound', "09_tail") == false) and (obj_name == 'area_tail') then
		ShowMonolog('C_09_MINI_GAME_TAILS_1', 'C_09_MINI_GAME_TAILS_1');
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function SkipMiniGame()
	if(game_state.skip_minigame == false and game_state.complited == false) then
		game_state.skip_minigame = true;
		for i = 1,OBJECTS_AMOUNT do
			Objects[i].Obj:SetOpacity(0.0);
			Objects[i].Obj:SetXY((Objects[i].need_indexI  - 1) * 90 + 447, (Objects[i].need_indexJ - 1)* 95 + 232);
			Objects[i].Obj:ChangeOpacity(0.0, 1.0, 1.0);
		end
		TimerDoString('OpenSafe()', 1.0, game_state.screen:GetTimer());
	end
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/09_Tails/Seyf1.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Seyf1',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/1.png',0,0,98,103,1,0)
	anim:SetHotSpot(49,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('1',anim,447,232,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/2.png',0,0,98,103,1,0)
	anim:SetHotSpot(49,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('2',anim,537,232,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/3.png',0,0,98,103,1,0)
	anim:SetHotSpot(49,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('3',anim,628,232,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/4.png',0,0,98,103,1,0)
	anim:SetHotSpot(49,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('4',anim,447,327,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/5.png',0,0,98,103,1,0)
	anim:SetHotSpot(49,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('5',anim,537,327,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/6.png',0,0,98,103,1,0)
	anim:SetHotSpot(49,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('6',anim,628,327,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/7.png',0,0,98,103,1,0)
	anim:SetHotSpot(49,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('7',anim,447,422,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/8.png',0,0,98,103,1,0)
	anim:SetHotSpot(49,51)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('8',anim,538,422,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/Seyf2.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Seyf2',anim,512,384,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/sprait_kruchka.png',0,0,47,48,1,0)
	anim:SetHotSpot(23,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_kruchka',anim,542,206,107,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_tail',anim,542,330,200,0,55,55)
	local anim = CreateAnimation('data/Texture/Minigames/09_Tails/Key_big.png',0,0,156,229,1,0)
	anim:SetHotSpot(78,114)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('kluch',anim,523,320,106,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
