

--------------------------------------------------------------------------------
--MiniGame
function InitMiniGameLock(screen)
	LoadDataMiniGame(screen);
	Objects = {}
	
	minigame_complited = false;
	minigame_screem = screen;
	cur_num = {3, 2, 4, 3};
	key_num = {1, 5, 2, 3};
	key = {"0", "4", "1", "2"}
	snake_pos = {390, 390, 390, 390};
	
	for j = 1, 4 do
		Objects[j] = {}
		for i = 1,10 do
			Objects[j][i] = {}
			Objects[j][i].Obj = screen:GetObject(j..'_'..(i - 1));
			Objects[j][i].Obj:SetX(397 + (j - 1) * 76);
			Objects[j][i].Obj:SetY(snake_pos[j] + 69 * (cur_num[j] - i + 1));
			Objects[j][i].Index = j;
		end
	end
	
	CurrectY();
                                
	zone_lock = CreateZoomZone( 'area_lock',
							  'CloseZoneCandle',
							  'back', true,
							  'shest1', true,
							  'shest2', true,
							  'shest3', true,
							  'layer1', true,
							  'layer', true,
							  'layer2', true,
							  '1_1', true, '1_2', true, '1_3', true, '1_4', true, '1_5', true, '1_6', true, '1_7', true, '1_8', true, '1_9', true, '1_0', true,
							  '2_1', true, '2_2', true, '2_3', true, '2_4', true, '2_5', true, '2_6', true, '2_7', true, '2_8', true, '2_9', true, '2_0', true,
							  '3_1', true, '3_2', true, '3_3', true, '3_4', true, '3_5', true, '3_6', true, '3_7', true, '3_8', true, '3_9', true, '3_0', true,
							  '4_1', true, '4_2', true, '4_3', true, '4_4', true, '4_5', true, '4_6', true, '4_7', true, '4_8', true, '4_9', true, '4_0', true,
							  'down_1', true, 'down_2', true, 'down_3', true, 'down_4', true,
							  'up_1', true, 'up_2', true, 'up_3', true, 'up_4', true
						   );
    zone_lock.on_open = function()
--        ShowMonolog('C_02_SKEL_LOCK_OPEN', 'C_02_SKEL_LOCK_OPEN');
    end
    
    object_locked = false;
    Buttons = {};
    
    for i = 1, 4 do
		Buttons[i] = minigame_screem:GetObject("up_"..i);
		Buttons[i]:GetAnimation():Stop();
		
		game_state.custom_cursors["up_"..i] = 'CursorActive'
		game_state.ignore_on_highlight["up_"..i] = true;
	end	
	 for i = 1, 4 do
		Buttons[i + 4] = minigame_screem:GetObject("down_"..i);
		Buttons[i + 4]:GetAnimation():Stop();
		
		game_state.custom_cursors["down_"..i] = 'CursorActive'
		game_state.ignore_on_highlight["down_"..i] = true;
	end	
	
	direction = 1;
end

function InitY()
	for j = 1, 4 do
		for i = 1,10 do
			Objects[j][i].Obj:SetY(snake_pos[j] + 69 * (cur_num[j] - i + 1));
		end
	end
end

function CurrectY()
	for j = 1, 4 do
		for i = 1,10 do
			if(Objects[j][i].Obj:GetY() < 390 - 69 * 3 + 34) then
				Objects[j][i].Obj:SetY(390 - 69 * 3 + 34);
			end
			if(Objects[j][i].Obj:GetY() > 390 + 69 * 3 - 34) then
				Objects[j][i].Obj:SetY(390 + 69 * 3 - 34);
			end
		end
	end
end

function GetObjectByName(name)
	for j = 1, 4 do
		for i = 1,10 do
			if(Objects[j][i].Obj:GetName() == name) then
				return Objects[j][i];
			end
		end
	end
	
	return nil;
end

function StartDrag(object)
	DragObject = object;
	OldMousePos = GetMousePos();
end

function StopDrag()
	if(DragObject ~= nil) then
		DragObject = nil;
	end
end

function MiniGameLockMouseOver(object)
    for i = 1, 8 do
		if object:GetName() == Buttons[i]:GetName() then
			Buttons[i]:GetAnimation():SetFrame(1);
			break;
		end
	end	
end

function MiniGameLockMouseOut(object)
    for i = 1, 8 do
		if object:GetName() == Buttons[i]:GetName() then
			Buttons[i]:GetAnimation():SetFrame(0);
			break;
		end
	end	
end

function MiniGameLockMouseDown(object)
	--local Obj = GetObjectByName(object:GetName());
	
--[[	if(Obj ~= nil and minigame_complited == false) then
		StartDrag(Obj);
	end]]
	
	if minigame_complited then
	    return;
	end
	
	if(object_locked == true) then
		return;
	end
	
	local obj_name = object:GetName();
	
	if obj_name == "back" then
	    ShowMonolog('C_02_CODE_HINT', 'C_02_CODE_HINT');
	end
	
	for i = 1, 8 do
		if(Buttons[i]:GetName() == object:GetName()) then
		    if HasGameState('ActionCompleted', 'DupensRoomChestOpen') then
		        if(i < 5) then
                    ButtonPressed((i - 1) % 4 + 1, -1);
                else
                    ButtonPressed((i - 1) % 4 + 1, 1);
                end
		    else
                ShowMonolog('C_02_HINT_NO_CODE', 'C_02_HINT_NO_CODE');
            end
		end
	end	
end

function ButtonPressed(index, _direction)
	DragObject = Objects[index][3];
	direction = _direction;
	object_locked = true;
	
	PlaySound('02_MG_LockClick');
end

function MiniGameMouseUp()
	--StopDrag()
end

function MiniGameUpdate()
	if(DragObject ~= nil) then
		
		for i = 1,10 do
			Objects[DragObject.Index][i].Obj:SetScrXY(Objects[DragObject.Index][i].Obj:GetScrX(), Objects[DragObject.Index][i].Obj:GetScrY() + 2 * direction);
		end
		snake_pos[DragObject.Index] = snake_pos[DragObject.Index] + 2 * direction;
				
		if(snake_pos[DragObject.Index] >= 390 + 69) then
		    snake_pos[DragObject.Index] = snake_pos[DragObject.Index] - 69;
			UpdateSnakeForward(DragObject.Index);
			DragObject = nil;
			object_locked = false;
			completed()
		elseif(snake_pos[DragObject.Index] <= 390 - 69) then
		    snake_pos[DragObject.Index] = snake_pos[DragObject.Index] + 69;
			UpdateSnakeBackward(DragObject.Index);
			DragObject = nil;
			object_locked = false;
			completed()
		end
		
		InitY();
		CurrectY();

		--OldMousePos = GetMousePos();
	end
end

function completed()
	if IsComplited() and minigame_complited == false then
		minigame_complited = true;
		DragObject = nil;
		FinishAnimation()
		if HasGameState('MiniGameCompleted', '02_lock') == false then
			AddGameState('MiniGameCompleted', '02_lock')
			game_state.need_anim_sceleton = true;
		end
		
		TimerDoString('close_zoom()', 4.1, play_field:GetTimer());
	end
end

function close_zoom()
	if(zone_lock ~= nil) then
		zone_lock.SetVisible(false);
	end
end

function UpdateSnakeForward(index)
	local cur = cur_num[index];
	local prev = cur_num[index] - 1;
	
	local Obj = Objects[index][prev].Obj;
	for i = 1,9 do
		if(cur == 11) then cur = 1; end
		if(prev == 11) then prev = 1; end
		Objects[index][prev].Obj = Objects[index][cur].Obj;
		cur = cur + 1;
		prev = prev + 1;
	end
	
	if(prev == 11) then prev = 1; end
	Objects[index][prev].Obj = Obj;
end

function UpdateSnakeBackward(index)
	local cur = cur_num[index];
	local next = cur_num[index] + 1;
	
	local Obj = Objects[index][next].Obj;
	for i = 1,9 do
		if(cur == 0) then cur = 10; end
		if(next == 0) then next = 10; end
		Objects[index][next].Obj = Objects[index][cur].Obj;
		cur = cur - 1;
		next = next - 1;
	end
	
	if(next == 0) then next = 10; end
	Objects[index][next].Obj = Obj;
end

function GetCenterNumber(index)
	local IndexJ = 1;
	for i = 2,10 do
		if(math.abs(Objects[index][i].Obj:GetY() - 390) < math.abs(Objects[index][IndexJ].Obj:GetY() - 390)) then
			IndexJ = i;
		end
	end
	
	return Objects[index][IndexJ].Obj:GetName();
end

function IsComplited()
	for i = 1,4 do
		if math.abs(game_state.screen:GetObject(i..'_'..(key_num[i] - 1)):GetY() - 390) > 20 then
			return false;
		end
	end
	
	return true
end

function FinishAnimation()
	PlayCyclicSound('02_MG_Mechan', 1.0);
	game_state.screen:GetObject('shest1'):Rotate(M_PI * 4, 4.0);
	game_state.screen:GetObject('shest3'):Rotate(M_PI * 4, 4.0);
	game_state.screen:GetObject('shest2'):Move(0, 60, 4.0);
	TimerDoString('StopCyclicSound("02_MG_Mechan")', 4.0, game_state.screen:GetTimer());
end
--------------------------------------------------------------------------------


function LoadDataMiniGame(screen)
	for i = 1,4 do
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,0,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_9',anim,880,130,172,0,1,1)
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,69,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_8',anim,880,198,172,0,1,1)
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,138,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_7',anim,880,267,172,0,1,1)
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,207,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_6',anim,880,335,172,0,1,1)
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,276,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_5',anim,880,394,172,0,1,1)
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,345,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_4',anim,880,473,172,0,1,1)
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,414,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_3',anim,880,542,172,0,1,1)
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,483,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_2',anim,880,611,172,0,1,1)
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,552,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_1',anim,880,680,172,0,1,1)
		local anim = CreateAnimation('data/Texture/Minigames/02_Lock/cifry.png',0,621,48,69,1,0)
		anim:SetHotSpot(24,34)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject(i..'_0',anim,880,749,172,0,1,1)
	end
	
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/back.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('back',anim,512,384,170,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/shest1.png',0,0,158,163,1,0)
	anim:SetHotSpot(79,81)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shest1',anim,366,382,171,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/shest3.png',0,0,195,196,1,0)
	anim:SetHotSpot(97,98)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shest3',anim,660,391,171,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/layer1.png',0,0,57,277,1,0)
	anim:SetHotSpot(28,138)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer1',anim,511,373,180,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/shest2.png',0,0,35,326,1,0)
	anim:SetHotSpot(17,163)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('shest2',anim,509,400,185,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/layer.png',0,0,353,480,1,0)
	anim:SetHotSpot(176,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer',anim,510,384,183,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/layer_2.png',0,0,353,480,1,0)
	anim:SetHotSpot(176,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer2',anim,510,384,190,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/down_.png',0,0,45,39,2,10)
	anim:SetHotSpot(22,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('down_1',anim,397,528,194,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/down_.png',0,0,45,39,2,10)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('down_2',anim,472,528,194,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/down_.png',0,0,45,39,2,10)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('down_3',anim,546,528,194,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/down_.png',0,0,45,39,2,10)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('down_4',anim,618,527,194,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/up_.png',0,0,45,39,2,10)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('up_1',anim,398,246,194,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/up_.png',0,0,45,39,2,10)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('up_2',anim,471,247,194,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/up_.png',0,0,45,39,2,10)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('up_3',anim,545,246,194,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/02_Lock/up_.png',0,0,45,39,2,10)
	anim:SetHotSpot(22,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('up_4',anim,616,247,194,0,1,1)
end


