function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/24_Mosaic.lua', 'mini_game', 'data/Scenes/24_Dolls_Main.lua')
	LinkScene('area_exit', 'CursorDoorDown', 'data/Scenes/24_Dolls_Main.lua', false)

	game_screen = screen;
	
	local Array = {}
	for i = 1,3 do
		Array[i] = {};
		for j = 1,4 do
			Array[i][j] = 0;
		end
	end
	
	Array[2][2] = 1;
	
	left_field = {};
	right_field = {};
	
	for i = 1,3 do
		left_field[i] = {};
		right_field[i] = {};
		for j = 1,4 do
			left_field[i][j] = 1;
			right_field[i][j] = 0;
		end
	end
	
	right_field[3][4] = 2;
	
	minigame_complited = false;
	minigame_skip = false;
	
	OBJECTS_AMOUNT = 12;

	Objects = {}
	for i = 1,OBJECTS_AMOUNT do
		Objects[i] = {}
		Objects[i].Entity = screen:GetObject(""..i);
		Objects[i].NeedPos = Objects[i].Entity:GetPosition();
		
		
		local x = 0;
		local y = 0;
		
		if(i ~= OBJECTS_AMOUNT) then
			repeat
				x = math.random(1,3);
				y = math.random(1,4);
			until (Array[x][y] == 0)
		
			Objects[i].Panel = left_field;
			Objects[i].Active = false;
			Objects[i].Fixed = false;
			
			Array[x][y] = 1;
			Objects[i].Entity:SetXY(266 + (x - 1) * 90, 165 + (y - 1) * 125);
			Objects[i].Entity:SetScale(0.80);
		else
			x = 3;
			y = 4;
			Objects[i].Panel = right_field;
			Objects[i].Active = true;
			Objects[i].Fixed = true;
			Objects[i].Entity:SetXY(733 + (x - 1) * 90, 165 + (y - 1) * 125);
			Objects[i].Entity:SetScale(1.0);
		end
		
		Objects[i].StartPos = Objects[i].Entity:GetPosition();
		Objects[i].Index_i = x;
		Objects[i].Index_j = y;
		Objects[i].NeedIndex_i = (i - 1) % 3 + 1;
		Objects[i].NeedIndex_j = math.floor((i  - 1) / 3) + 1;
	end
	
	ShowMonolog('24_MiniGame', 'C_24_MINI_GAME1')
end

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

function OnUpdate(dt)
	UpdateDrag();
    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(object_name)
	for i = 1, OBJECTS_AMOUNT do
		if(object_name == Objects[i].Entity:GetName()) then
			return Objects[i];
		end
	end
	
	return nil;
end

function StartDrag(object)
	SetCursor('CursorTool')
	DragObject = object;
	DragObject.Entity:SetZ(105);
	DragObject.Active = false;
end

function UpdateDrag()
	if(DragObject ~= nil) then
		SetCursor('CursorTool')
		DragObject.Entity:SetXY(GetMousePos().x, GetMousePos().y);
	end
end

function IsComplited()
	for i = 1,OBJECTS_AMOUNT do
		if(Objects[i].Active == false or Objects[i].Index_i ~= Objects[i].NeedIndex_i or Objects[i].Index_j ~= Objects[i].NeedIndex_j )then
			return false;
		end
	end
	
	return true;
end

function UpdateField()
	local counter = 0;
	
	repeat
		counter = 0;
		for i = 1,OBJECTS_AMOUNT do
			if(Objects[i].Panel == right_field and Objects[i].Index_i == Objects[i].NeedIndex_i and Objects[i].Index_j == Objects[i].NeedIndex_j and Objects[i].Fixed == false) then
				if(right_field[Objects[i].NeedIndex_i - 1] and right_field[Objects[i].NeedIndex_i - 1][Objects[i].NeedIndex_j] == 2) or
					(right_field[Objects[i].NeedIndex_i + 1] and right_field[Objects[i].NeedIndex_i + 1][Objects[i].NeedIndex_j] == 2) or
					(right_field[Objects[i].NeedIndex_i][Objects[i].NeedIndex_j - 1] == 2) or
					(right_field[Objects[i].NeedIndex_i][Objects[i].NeedIndex_j + 1] == 2) then
					
					right_field[Objects[i].NeedIndex_i][Objects[i].NeedIndex_j] = 2;
					Objects[i].Fixed = true;
					Objects[i].Entity:ChangeScale(0.8, 1.0, 0.2);
					counter = counter + 1;
				end
			end
		end
	until(counter == 0)
end

function StopDrag(doAbort)
	if(DragObject ~= nil) then
		local MousePos = GetMousePos();
		
		if (MousePos.x > 219 and
            MousePos.x < 550 and
            MousePos.y > 30 and
            MousePos.y < 700) and not doAbort
        then --Left panel
			local index_i = math.floor((MousePos.x - 219) / 90) + 1;
			local index_j = math.floor((MousePos.y - 88) / 125) + 1;
			
			if(left_field[index_i] ~= nil and left_field[index_i][index_j] == 0) then
				left_field[index_i][index_j] = 1;
				DragObject.Panel[DragObject.Index_i][DragObject.Index_j] = 0;
				DragObject.Index_i = index_i;
				DragObject.Index_j = index_j;
				DragObject.Panel = left_field;
				DragObject.Entity:Move(264 + (index_i - 1) * 90 - DragObject.Entity:GetX(), 164 + (index_j - 1) * 125 - DragObject.Entity:GetY(), 0.2);
				DragObject.StartPos = Vector(264 + (index_i - 1) * 90, 88 + (index_j - 1) * 125 + 76);
			else
				DragObject.Entity:Move(DragObject.StartPos.x - DragObject.Entity:GetX(), DragObject.StartPos.y - DragObject.Entity:GetY(), 0.2);
			end
		elseif (MousePos.x > 688 and
                MousePos.x < 990 and
                MousePos.y > 30 and
                MousePos.y < 700) and not doAbort
        then--Right panel
			local index_i = math.floor((MousePos.x - 688) / 90) + 1;
			local index_j = math.floor((MousePos.y - 88) / 125) + 1;
			if(right_field[index_i] ~= nil and right_field[index_i][index_j] == 0) then
				right_field[index_i][index_j] = 1;
				DragObject.Panel[DragObject.Index_i][DragObject.Index_j] = 0;
				DragObject.Index_i = index_i;
				DragObject.Index_j = index_j;
				DragObject.Active = true;
				DragObject.Panel = right_field;
				DragObject.Entity:Move(733 + (index_i - 1) * 90 - DragObject.Entity:GetX(), 164 + (index_j - 1) * 125 - DragObject.Entity:GetY(), 0.2);
				DragObject.StartPos = Vector(733 + (index_i - 1) * 90, 88 + (index_j - 1) * 125 + 76);
						
				UpdateField();
				if(IsComplited()) then
					if HasGameState('MiniGameCompleted', '24_Mosaic') == false then
						AddGameState('MiniGameCompleted', '24_Mosaic');
						TimerDoString("FinishGame()", 0.4, game_screen:GetTimer())
						minigame_complited = true;
					end
				end
			else
				DragObject.Entity:Move(DragObject.StartPos.x - DragObject.Entity:GetX(), DragObject.StartPos.y - DragObject.Entity:GetY(), 0.2);
			end
		else
			DragObject.Entity:Move(DragObject.StartPos.x - DragObject.Entity:GetX(), DragObject.StartPos.y - DragObject.Entity:GetY(), 0.2);
		end
		
		DragObject.Entity:SetZ(100);
		DragObject = nil;
		SetCursor('CursorDefault');
	end
end

function FinishGame()
	StartLevel('data/Scenes/24_Dolls_Main.lua')
end

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

	local obj = GetObjectByName(obj_name);
	
	if(obj ~= nil and obj.Fixed == false and minigame_skip == false and minigame_complited == false) then
		StartDrag(obj);
	end	
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
	StopDrag();
    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

function SkipMiniGame()
	if(minigame_complited == false and minigame_skip == false) then
		minigame_skip = true;
		minigame_complited = true;
		for i = 1,OBJECTS_AMOUNT do
			Objects[i].Entity:SetVisible(false);
			Objects[i].Entity:SetXY(733 + ((i - 1) % 3) * 90, 164 + math.floor((i  - 1) / 3) * 125);
			Objects[i].Entity:SetVisible(true);
			Objects[i].Entity:ChangeOpacity(0.0, 1.0, 0.5);
			if HasGameState('MiniGameCompleted', '24_Mosaic') == false then
				AddGameState('MiniGameCompleted', '24_Mosaic');
				TimerDoString("FinishGame()", 0.7, game_screen:GetTimer())
			end
		end
	end
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/Mosaic/BG_Window_Minigame.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Window_Minigame',anim,512,383,95,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/01.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('1',anim,727,166,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/02.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('2',anim,817,166,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/03.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('3',anim,907,167,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/04.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('4',anim,727,292,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/05.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('5',anim,817,291,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/06.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('6',anim,906,292,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/07.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('7',anim,728,417,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/08.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('8',anim,816,416,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/09.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('9',anim,907,418,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/10.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('10',anim,728,542,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/11.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('11',anim,817,542,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Mosaic/12.PNG',0,0,101,136,1,0)
	anim:SetHotSpot(50,68)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('12',anim,908,542,100,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_exit',anim,508,708,400,0,100,15)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
