function OnSceneLoad(screen)
    LoadData(screen)
	DefaultOnSceneLoad('data/Scenes/Minigames/09_combi_lock.lua', 'quest', 'data/Scenes/09_Bank_Office_Depositary.lua')
	
	if HasGameState('ToolApplied', '09_duster_wet') then
	    play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
	    game_state.level_mode = 'mini_game';
	end
	
	game_state.skip_minigame = false;
    game_state.complited = false;
    game_state.screen = screen;
    
	
	CreateTorchFireFibrillationEffector(game_state.screen:GetObject('green_light'));
	CreateTorchFireFibrillationEffector(game_state.screen:GetObject('red_light'));
	
	game_state.screen:GetObject('green_light'):SetVisible(false);
		
	local Index = {2, 2, 3, 2}
	
	OBJECTS_AMOUNT = 4;
	Objects = {}
	
	for i = 1,OBJECTS_AMOUNT do
		Objects[i] = {};
		Objects[i].Entity = game_state.screen:GetObject("polzunok_"..i);
		Objects[i].Target = game_state.screen:GetObject("area_circle_"..i);
		Objects[i].NeedIndex = Index[i];
		Objects[i].Position = math.random(10);
		Objects[i].Entity:SetAngle(M_PI / 5 * (Objects[i].Position - 1));
		Objects[i].Active = true;
		Objects[i].Index = i;
		
		game_state.custom_cursors[Objects[i].Target:GetName()] = 'CursorActive';
		game_state.ignore_on_highlight[Objects[i].Target:GetName()] = true;
	end
	
	LoadGame();
	
	for i = 1,OBJECTS_AMOUNT do
		SaveState(i, Objects[i].Position);
	end
	
	ShowMonolog('C_09_ENTER_PASSWORD_HINT', 'C_09_ENTER_PASSWORD_HINT');
end

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

function LoadGame()
	for i = 1,OBJECTS_AMOUNT do
		for j = 1,10 do
			if(HasGameState('MINI_GAME_09_LOCK_STATE_'..i, "0"..j)) then
				Objects[i].Position = j;
				Objects[i].Entity:SetAngle(M_PI / 5 * (Objects[i].Position - 1));
			end
		end
	end
end

function SaveState(index, state)
	for j = 1,10 do
		if(HasGameState('MINI_GAME_09_LOCK_STATE_'..index, "0"..j)) then
			RemoveGameState('MINI_GAME_09_LOCK_STATE_'..index, "0"..j)
		end
	end
	
	AddGameState('MINI_GAME_09_LOCK_STATE_'..index, "0"..state);
end

function CreateTorchFireFibrillationEffector(object)
	local duration = 0.3;

	local fireFibrillationEffector = CreateAnimationEffector();
	fireFibrillationEffector:AttachTo(object);
	fireFibrillationEffector:SetMode(_ANIM_LOOP);

	fireFibrillationEffector:SetKey(_TRACK_O, 0.0, 1.0);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *1.0/4.0, 0.95);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *2.0/4.0, 1.1);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *3.0/4.0, 0.90);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *4.0/4.0, 1.1);

	fireFibrillationEffector:Play();
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 GetObjectByTargetName(name)
	for i = 1,OBJECTS_AMOUNT do
		if(name == Objects[i].Target:GetName()) then
			return Objects[i];
		end
	end
	
	return nil;
end

function IsComplited()
	for i = 1,OBJECTS_AMOUNT do
		if(Objects[i].Position ~= Objects[i].NeedIndex) then
			return false;
		end
	end
	
	return true;
end

function OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field
	
	Rotate(object, 0);
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end


function OnObjectRightMouseDown(object)	
	Rotate(object, 1);
end

function OnObjectRightMouseUp(object)
end

function SkipMiniGame()
	if(game_state.complited == false and game_state.skip_minigame == false) then
		game_state.skip_minigame = true;
		
		for i = 1,OBJECTS_AMOUNT do
			local af = CreateAnimationEffector()
			af:AttachTo(Objects[i].Entity);
			af:SetMode(_ANIM_FWD);
			af:SetKey(_TRACK_A, 0.0, Objects[i].Entity:GetAngle());
			af:SetKey(_TRACK_A, 0.5, M_PI / 5 * (Objects[i].NeedIndex - 1))
			af:Play();
		end
		
		TimerDoString("StartLevel('data/Scenes/09_Bank_Office_Depositary.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_combi_lock') == false) then
			AddGameState('MiniGameCompleted', '09_combi_lock');
		end		
	end
end

function Rotate(object, flag)

	local object = GetObjectByTargetName(object:GetName());
	
	if object and game_state.level_mode ~= 'mini_game' then
	    ShowMonolog('C_09_MINI_GAME_NO_CODE_HINT', 'C_09_MINI_GAME_NO_CODE_HINT');
	    return;
	end
	
	if(object ~= nil and object.Active == true and game_state.complited == false and game_state.skip_minigame == false) then
		if(flag == 0) then
			object.Entity:Rotate(M_PI / 5, 0.2);
			object.Position = object.Position + 1;
			if(object.Position > 10) then
				object.Position = 1;
			end
		else
			object.Entity:Rotate(-M_PI / 5, 0.2);
			object.Position = object.Position - 1;
			if(object.Position < 1) then
				object.Position = 10;
			end
		end
		
		PlaySound('09_MG_RotateButton');
		object.Active = false;
		SaveState(object.Index, object.Position);
		
		TimerDoString("Objects["..object.Index.."].Active = true", 0.3, game_state.screen:GetTimer())
		TimerDoString("Objects["..object.Index.."].Entity:SetAngle(fix_angle(Objects["..object.Index.."].Entity:GetAngle()));", 0.3, game_state.screen:GetTimer())
		
		if(IsComplited() == true) then
			game_state.screen:GetObject('green_light'):SetVisible(true);
			game_state.screen:GetObject('red_light'):SetVisible(false);
			 game_state.complited = true;
			 
			 PlaySound('09_MG_Win')
			 
			TimerDoString("StartLevel('data/Scenes/09_Bank_Office_Depositary.lua');", 0.4, game_state.screen:GetTimer())
			if (HasGameState('ClosedScene', game_state.scene_path) == false) then
				AddGameState('ClosedScene', game_state.scene_path);
			end
			
			if (HasGameState('MiniGameCompleted', '09_combi_lock') == false) then
				AddGameState('MiniGameCompleted', '09_combi_lock');
			end		 
		end
	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_combi_lock/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,97,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/green light.png',0,0,70,69,1,0)
	anim:SetHotSpot(35,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('green_light',anim,495,117,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/red light.png',0,0,75,79,1,0)
	anim:SetHotSpot(37,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('red_light',anim,537,113,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/polzunok.png',0,0,66,127,1,0)
	anim:SetHotSpot(34,73)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('polzunok_1',anim,120,393,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/polzunok.png',0,0,66,127,1,0)
	anim:SetHotSpot(34,73)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('polzunok_2',anim,381,393,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/polzunok.png',0,0,66,127,1,0)
	anim:SetHotSpot(34,73)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('polzunok_3',anim,639,393,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/polzunok.png',0,0,66,127,1,0)
	anim:SetHotSpot(34,73)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('polzunok_4',anim,895,393,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/BOLT.png',0,0,44,51,1,0)
	anim:SetHotSpot(22,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BOLT',anim,118,392,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/BOLT.png',0,0,44,51,1,0)
	anim:SetHotSpot(22,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BOLT_2',anim,380,393,110,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/BOLT.png',0,0,44,51,1,0)
	anim:SetHotSpot(22,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BOLT_3',anim,640,393,110,60,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/09_combi_lock/BOLT.png',0,0,44,51,1,0)
	anim:SetHotSpot(22,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BOLT_4',anim,896,393,110,80,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_circle_1',anim,123,391,150,0,1.1,1.1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_circle_2',anim,381,390,150,0,1.1,1.1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_circle_3',anim,638,392,150,0,1.1,1.1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_circle_4',anim,896,393,150,0,1.1,1.1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
