function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/Minigames/06_owl_lock.lua', 'quest', "data/Scenes/06_Laundry_Shelves_HO.lua")
    
	game_state.cargoArray = {};
    game_state.discArray  = {};
    game_state.size       = 3;
    game_state.stage      = 'wait';
    game_state.unvisibleButton = nil;
    
    indexes = {3, 1, 4, 2}
   
    local deltaWidth = (play_field:GetObject('ring_'..game_state.size):GetX() - play_field:GetObject('ring_1'):GetX()) / (game_state.size-1);
      
    game_state.discRadius = deltaWidth / 2;
    game_state.screen = screen;
    game_state.locked = false;
    game_state.skip = false;
    --[[for i=1, game_state.size + 1 do
        CreateCargo(i, 'Pimpa_'..i);
    end]]
    CreateCargo(1, 'Pimpa_1', 1);
    CreateCargo(2, 'Pimpa_2', 2);
    CreateCargo(3, 'Pimpa_3', 3);
    CreateCargo(4, 'Pimpa_4', 1);
    
	Composition = createCompositionscreen_sova(screen);
	Composition:AttachTo(screen);
	Composition:SetZ(200);
    
    for i=1, game_state.size do
        CreateDisc(i);
        
        GetDisc(i).x = play_field:GetObject('ring_1'):GetX() + deltaWidth * (i - 1);
        GetDisc(i).y = play_field:GetObject('ring_'..i):GetY();
        
        GetDisc(i).setLeftCargo ( GetCargo(indexes[i])  );
        GetDisc(i).setRightCargo( GetCargo(indexes[i + 1]) );
    end
    
    if HasGameState('MASK_OPEN', 'true') then
		game_state.screen:GetObject('mask'):SetAngle(M_PI_OVER_2);
		ShowMonolog('C_06_MG_OWL_HINT', 'C_06_MG_OWL_HINT');
    else
		game_state.locked = true;
    end
    
    play_field:GetObject('knopka_3'):SetVisible(false);
    
	RegisterToolApplyInfo('06_owl_button', 'ring_3', nil,  nil,   function(tool_name)        
																	play_field:GetObject('knopka_3'):SetVisible(true);
																	play_field:GetObject('knopka_3'):ChangeOpacity(0.0, 1.0, 0.3);
																	game_state.level_mode = 'mini_game';
																	play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME)
																	PlaySound('06_PlaceButton');
																	
																	ShowMonolog('C_06_MG_OWL_HINT_2', 'C_06_MG_OWL_HINT_2');
                                                                end,
                                                                function(tool_name)                                                                 
                                                                    play_field:GetObject('knopka_3'):SetVisible(true);
                                                                    game_state.level_mode = 'mini_game';
																	play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME)
																	
																	ShowMonolog('C_06_MG_OWL_HINT_2', 'C_06_MG_OWL_HINT_2');
                                                                end);
end

createCompositionscreen_sova = function()

	local compositionscreen_sova = CreateCompositionSequence();
	local compositionDuration = 1.83333333333333;
	compositionscreen_sova:SetDuration(compositionDuration);


	-- Layer Layer_1
	local element = CreateCompositionElement("data/Texture/Minigames/06_owl_lock/blik.png");
	element:SetName("Layer_1");
	compositionscreen_sova:AddElement(element);
	element:SetHotSpot(32, 32);
	element:SetParentElement(compositionscreen_sova);
	element:SetXY(399, 348);

	element:SetOpacity(1);

	effector = element:GetEffector();
	effector:SetLifeTime(0.16666666666667, 0.66666666666667);

	effector:SetKey(_TRACK_SX, 0.16666666666667, 0.1);
	effector:SetKey(_TRACK_SY, 0.16666666666667, 0.1);
	effector:SetKey(_TRACK_SX, 0.41666666666667, 1);
	effector:SetKey(_TRACK_SY, 0.41666666666667, 1);
	effector:SetKey(_TRACK_SX, 0.66666666666667, 0.05);
	effector:SetKey(_TRACK_SY, 0.66666666666667, 0.05);
	effector:SetKey(_TRACK_A, 0.16666666666667, 1.3089969375);
	effector:SetKey(_TRACK_A, 0.66666666666667, 0.6981317);

	effector:Play();


	-- Layer Layer_2
	local element = CreateCompositionElement("data/Texture/Minigames/06_owl_lock/blik.png");
	element:SetName("Layer_2");
	compositionscreen_sova:AddElement(element);
	element:SetHotSpot(32, 32);
	element:SetParentElement(compositionscreen_sova);
	element:SetXY(399, 348);

	element:SetOpacity(1);

	effector = element:GetEffector();
	effector:SetLifeTime(0, 0.5);

	effector:SetKey(_TRACK_SX, 0, 0.1);
	effector:SetKey(_TRACK_SY, 0, 0.1);
	effector:SetKey(_TRACK_SX, 0.25, 1);
	effector:SetKey(_TRACK_SY, 0.25, 1);
	effector:SetKey(_TRACK_SX, 0.5, 0.05);
	effector:SetKey(_TRACK_SY, 0.5, 0.05);
	effector:SetKey(_TRACK_A, 0, 0);
	effector:SetKey(_TRACK_A, 0.5, 0.6981317);

	effector:Play();


	-- Layer Layer_3
	local element = CreateCompositionElement("data/Texture/Minigames/06_owl_lock/blik.png");
	element:SetName("Layer_3");
	compositionscreen_sova:AddElement(element);
	element:SetHotSpot(32, 32);
	element:SetParentElement(compositionscreen_sova);
	element:SetXY(666, 349);

	element:SetOpacity(1);

	effector = element:GetEffector();
	effector:SetLifeTime(0.41666666666667, 0.91666666666667);

	effector:SetKey(_TRACK_SX, 0.41666666666667, 0.1);
	effector:SetKey(_TRACK_SY, 0.41666666666667, 0.1);
	effector:SetKey(_TRACK_SX, 0.66666666666667, 1);
	effector:SetKey(_TRACK_SY, 0.66666666666667, 1);
	effector:SetKey(_TRACK_SX, 0.91666666666667, 0.05);
	effector:SetKey(_TRACK_SY, 0.91666666666667, 0.05);
	effector:SetKey(_TRACK_A, 0.41666666666667, 1.3089969375);
	effector:SetKey(_TRACK_A, 0.91666666666667, 0.6981317);

	effector:Play();


	-- Layer Layer_4
	local element = CreateCompositionElement("data/Texture/Minigames/06_owl_lock/blik.png");
	element:SetName("Layer_4");
	compositionscreen_sova:AddElement(element);
	element:SetHotSpot(32, 32);
	element:SetParentElement(compositionscreen_sova);
	element:SetXY(666, 349);

	element:SetOpacity(1);

	effector = element:GetEffector();
	effector:SetLifeTime(0.25, 0.75);

	effector:SetKey(_TRACK_SX, 0.25, 0.1);
	effector:SetKey(_TRACK_SY, 0.25, 0.1);
	effector:SetKey(_TRACK_SX, 0.5, 1);
	effector:SetKey(_TRACK_SY, 0.5, 1);
	effector:SetKey(_TRACK_SX, 0.75, 0.05);
	effector:SetKey(_TRACK_SY, 0.75, 0.05);
	effector:SetKey(_TRACK_A, 0.25, 0);
	effector:SetKey(_TRACK_A, 0.75, 0.6981317);

	effector:Play();


	-- Layer Layer_5
	local element = CreateCompositionElement("data/Texture/Minigames/06_owl_lock/eye.png");
	element:SetName("Layer_5");
	compositionscreen_sova:AddElement(element);
	element:SetHotSpot(187.5, 57.5);
	element:SetParentElement(compositionscreen_sova);
	element:SetXY(519, 367);

	element:SetScaleX(1);
	element:SetScaleY(1);
	element:SetAngle(0);

	effector = element:GetEffector();
	effector:SetLifeTime(0, 15);

	effector:SetKey(_TRACK_O, 0, 0);
	effector:SetKey(_TRACK_O, 0.25, 0.7);
	effector:SetKey(_TRACK_O, 0.75, 1);
	effector:SetKey(_TRACK_O, 1.08333333333333, 0);

	effector:Play();

	return compositionscreen_sova;
end

function SkipMiniGame()
	if(game_state.skip == false) then
		game_state.skip = true
		for i = 1, 4 do
			game_state.screen:GetObject('Pimpa_'..i):ChangeOpacity(1.0, 0.0, 0.2);
			TimerDoString("game_state.screen:GetObject('Pimpa_"..i.."'):ChangeOpacity(0.0, 1.0, 0.2);", 0.3, play_field:GetTimer())
		end
		
		TimerDoString("game_state.screen:GetObject('Pimpa_1'):SetXY(111, 379);", 0.2, play_field:GetTimer())
		TimerDoString("game_state.screen:GetObject('Pimpa_2'):SetXY(373, 377);", 0.2, play_field:GetTimer())
		TimerDoString("game_state.screen:GetObject('Pimpa_3'):SetXY(638, 379);", 0.2, play_field:GetTimer())
		TimerDoString("game_state.screen:GetObject('Pimpa_4'):SetXY(898, 379);", 0.2, play_field:GetTimer())
		
		TimerDoString("FinishGame();", 0.5, play_field:GetTimer())
	end
end   
 
----------------------------------------------
function CreateCargo(id, pimpaName, index)
    local cargo = {};
    
    cargo.id     = index;
    cargo.pimpa  = play_field:GetObject(pimpaName);

   cargo.effector = CreateAnimationEffector();
    cargo.effector:AttachTo(cargo.pimpa);
    cargo.effector:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
    cargo.effector:SetKey(_TRACK_A, 0, 0);
    cargo.effector:SetKey(_TRACK_A, 1, M_TWO_PI);
    
    cargo.SetX = function(x)
        cargo.pimpa:SetX(x);
    end
    
    cargo.SetY = function(y)
        cargo.pimpa:SetY(y);
    end
    
    cargo.StartAnim = function()
        cargo.effector:Play();
    end
    
    cargo.StopAnim = function()
       cargo.effector:Stop();
    end
    
    game_state.cargoArray[id] = cargo;
    return cargo;
end
----------------------------------------------------
function GetCargo(id)
    return game_state.cargoArray[id];
end
----------------------------------------------------
function CreateDisc(id)
    local disc = {};
    
    disc.id         = id;
    disc.leftCargo  = nil;
    disc.rightCargo = nil;
    
    disc.leftAngle  = math.pi;
    disc.rightAngle = 0;
    
    disc.rotating   = false;
    
    disc.x          = 0;
    disc.y          = 0;
    disc.angle      = 0;
    disc.startAngle	= 0;
	disc.rotateCoef = 0;
	disc.time       = 0;
	
	disc.setLeftCargo = function(cargo)
		disc.leftCargo = cargo;
		
		if cargo ~= nil then
		    local angle = disc.angle + disc.leftAngle;
            disc.leftCargo.SetX( disc.x + game_state.discRadius * math.cos( angle ) );
			disc.leftCargo.SetY( disc.y + game_state.discRadius * math.sin( angle ) );
		end
	end
	
	disc.setRightCargo = function(cargo)
		disc.rightCargo = cargo;
		
		if cargo ~= nil then
		    local angle = disc.angle + disc.rightAngle;
            disc.rightCargo.SetX( disc.x + game_state.discRadius * math.cos( angle ) );
			disc.rightCargo.SetY( disc.y + game_state.discRadius * math.sin( angle ) );
		end
	end
	
    disc.Rotate = function(time)
		disc.time       = 0;
		disc.rotateCoef = math.pi / time;
		disc.startAngle = disc.angle;
		disc.rotating   = true;
		
		if disc.leftCargo ~= nil then
		    disc.leftCargo.StartAnim();
		end
		
		if disc.rightCargo ~= nil then
		    disc.rightCargo.StartAnim();
		end
		
		PlaySound('06_RotateEyes');
	end
	
	disc.Update = function(dt)
	    if disc.rotating == true then
			disc.time = disc.time + dt;
			
			local dAngle = math.min(disc.rotateCoef * disc.time, math.pi);
			
			disc.angle = disc.startAngle + dAngle;
			
			if disc.rightCargo ~= nil then
				local angle = disc.angle + disc.rightAngle;
				disc.rightCargo.SetX( disc.x + game_state.discRadius * math.cos( angle ) );
				disc.rightCargo.SetY( disc.y + game_state.discRadius * math.sin( angle ) );
			end
			
			if disc.leftCargo ~= nil then
				local angle = disc.angle + disc.leftAngle;
				disc.leftCargo.SetX( disc.x + game_state.discRadius * math.cos( angle ) );
				disc.leftCargo.SetY( disc.y + game_state.discRadius * math.sin( angle ) );
			end
			
			if dAngle == math.pi then
			    disc.leftAngle, disc.rightAngle = disc.rightAngle, disc.leftAngle;
				disc.leftCargo, disc.rightCargo = disc.rightCargo, disc.leftCargo;
				disc.rotating = false;
				
				onDiscRotated(disc.id)
			end
		end
	end
	
	game_state.discArray[id] = disc;
	return disc;
end
----------------------------------------------------
function GetDisc(id)
    return game_state.discArray[id];
end
----------------------------------------------------
function onDiscRotated(id)
    local disc = GetDisc(id);
    
    if id > 1 then
        GetDisc(id-1).setRightCargo( disc.leftCargo );
    end
    
    if id < game_state.size then
        GetDisc(id+1).setLeftCargo( disc.rightCargo );
    end
    
    if disc.leftCargo ~= nil then
        disc.leftCargo.StopAnim();
    end
    
    if disc.rightCargo ~= nil then
        disc.rightCargo.StopAnim();
    end
    
    game_state.stage = 'wait';
    
    if IsCompleted() == true then
        FinishGame();
    end
end


function FinishGame()
	if not HasGameState('MiniGameCompleted', '06_owl_lock') then
        AddGameState('MiniGameCompleted', '06_owl_lock');
        AddGameState('ClosedScene', game_state.scene_path)
    end
    
	
    
    local anim_time = 2.0;
    game_state.locked = true;
    game_state.screen:GetObject('mask'):Rotate(-M_PI_OVER_2, anim_time / 2);
    TimerDoString("game_state.screen:GetObject('zamok'):Move(0.0, 100.0, "..(anim_time / 2)..")", anim_time / 2, play_field:GetTimer())
    TimerDoString("game_state.screen:GetObject('zamok_2'):Move(0.0, 100.0, "..(anim_time / 2)..")", anim_time / 2, play_field:GetTimer())
    TimerDoString("Composition:Play(); PlaySound('06_OpenLock');", anim_time / 2, play_field:GetTimer())
    
    PlaySound('06_CloseMask');
    
    TimerDoString("StartLevel('data/Scenes/06_Laundry_Shelves_HO.lua');", anim_time + 0.4, play_field:GetTimer())
end
----------------------------------------------------
function IsCompleted()
    local disc = nil;
    local index = 1;
    
   -- if(game_state.stage ~= "wait") then
	for i=1, game_state.size do
		disc = GetDisc(i);
		
		if disc.leftCargo.id ~= index then
			if(disc.leftCargo.id == 1 and index == 4) then
			else
				return false;
			end
		end
		
		index = index + 1;
	end
   -- end
    
    return true;
end
----------------------------------------------------


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

function OnUpdate(dt)
    DefaultOnUpdate(dt)
    
	for i, disc in ipairs(game_state.discArray) do
        disc.Update(dt);
    end
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.stage == 'wait' and game_state.locked == false) then
	   for i=1, 5 do
			if obj_name == 'knopka_'..i then
				GetDisc(i).Rotate(1);
				game_state.stage = 'rotate';
				
				game_state.unvisibleButton = object;
				break;
			end
		end
	end

	if(obj_name == 'mask' and HasGameState('MASK_OPEN', 'true') == false) then
		AddGameState('MASK_OPEN', 'true');
		game_state.locked = true;
		local open_time = 0.8;
		game_state.screen:GetObject('mask'):Rotate(M_PI_OVER_2, open_time);
		TimerDoString("game_state.locked = false", open_time, play_field:GetTimer())
		ShowMonolog('C_06_MG_OWL_HINT', 'C_06_MG_OWL_HINT')
		
		PlaySound('06_OpenMask');
	end
	
	 --if not HasGameState('ToolApplied', "02_can") then
	 if game_state.level_mode ~= 'mini_game' then
		if(obj_name == "ring_3") then
			ShowMonolog('C_06_MG_OWL_HINT', 'C_06_MG_OWL_HINT')
		end
	 end
		
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
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_owl_lock/Layers/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_owl_lock/Layers/krug.png',0,0,269,269,1,0)
	anim:SetHotSpot(134,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ring_1',anim,247,369,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/krug.png',0,0,269,269,1,0)
	anim:SetHotSpot(134,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ring_2',anim,511,368,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/krug.png',0,0,269,269,1,0)
	anim:SetHotSpot(134,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ring_3',anim,779,372,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/sova.png',0,0,593,437,1,0)
	anim:SetHotSpot(296,218)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sova',anim,513,219,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/eye_open_left.png',0,0,128,125,1,0)
	anim:SetHotSpot(64,62)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pimpa_2',anim,372,377,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/eye_open_right.png',0,0,128,125,1,0)
	anim:SetHotSpot(64,62)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pimpa_3',anim,638,379,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/eye_closed_left.png',0,0,123,123,1,0)
	anim:SetHotSpot(61,61)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pimpa_1',anim,111,379,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/eye_closed_right.png',0,0,123,123,1,0)
	anim:SetHotSpot(61,61)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pimpa_4',anim,898,379,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/mask.png',0,0,912,601,1,0)
	anim:SetHotSpot(75,420)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('mask',anim,136,463,120,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/zamok.png',0,0,123,251,1,0)
	anim:SetHotSpot(61,125)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('zamok',anim,271,175,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/zamok.png',0,0,123,251,1,0)
	anim:SetHotSpot(61,125)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('zamok_2',anim,745,175,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/L5-knopka1.png',0,0,116,164,1,0)
	anim:SetHotSpot(58,82)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_1',anim,247,366,106,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/L5-knopka2.png',0,0,116,164,1,0)
	anim:SetHotSpot(58,82)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_2',anim,511,366,106,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/06_owl_lock/Layers/L5-knopka3.png',0,0,116,164,1,0)
	anim:SetHotSpot(58,82)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('knopka_3',anim,780,368,106,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
