
-----------------------------------------------------------------
--

ELEMENTS_AMOUNT = 6;

function OnSceneLoad(screen)
    LoadData(screen);

	play_field.game_state = game_state;
	
    game_state = {}
	game_state.elements_to_finish = 0;
    game_state.locked_objects = {};
    game_state.custom_cursors = {};
	game_state.active_objects = {};
    game_state.game_complite = false; 
	game_state.game_skip = false;
    game_state.screen = screen;
    game_state.locker = ReferenceCounter()
    game_state.HELP_STRING = "HELP_MINIGAME_31";
    
	game_state.locked_objects['plan_sobora_bak_2'] = true;
	
	--Object Initialization
	Element = {};
	for i = 1, ELEMENTS_AMOUNT do
		Element[i] = {};
		Element[i].Obj = screen:GetObject('sprait_'..i); 
		Element[i].StartPos = Element[i].Obj:GetPosition();
		Element[i].Active = true;
		Element[i].Z = 200 + i;
		local x = math.random(100) + 800 * (math.random(2) - 1) + 100
		local y = math.random(100) + 400 * (math.random(2) - 1) + 100
		if x > 512 and y < 384 then
			y = 768 - y
		end
		Element[i].Obj:SetXY(x, y);
		Element[i].Obj:SetAngle((math.random(4) - 1) * M_PI_OVER_2);
		Element[i].Obj:SetZ(Element[i].Z);
	end
	--
	game_state.timer = screen:GetTimer()
	AddMiniGamesGUI(screen);
	
	InitChain1(screen)
	AddSheriffEyeLogic(screen)
	
	game_state.screen:SetLayer(2);   
end

function OnSceneFree(screen)
    FreeData(screen);
end

function InitChain1(screen)
    local comics1 = CreateComics(screen,                                                                  
                                 'Sheriff',
								 'eyes')
                                     
    comics1.OnHidden = function()
		game_state.locker:Release()    
		OnCompletedGame();
    end	               
    
    game_state.finish_dialog = CreateChain('pixel',
                                    screen,
                                    comics1, GetString("COMICS_30_5"))
                                    
    comics1.OnShown = function()
        OnMinigameComicsShown(comics1)
    end
end

-------------------------------------------------------------------------
--Drag functions
function StartDrag(object)
	local Index = GetObjectIndex(object);
	if (Index ~= nil) then
		if(Element[Index].Active == true) then
			DragObject = object;
			DragObject:SetXY(GetMouseX(), GetMouseY());
			DragObject:SetZ(300);
		end
	end
end

function DragUpdate()
	if (DragObject ~= nil) then
		DragObject:SetXY(GetMouseX(), GetMouseY());
	end
end

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

function CheckElementPos(element_name)
	if(element_name == nil) then
		return;
	end
	
	object = game_state.screen:GetObject(element_name);
	if (object ~= nil) then
		
		local Index = GetObjectIndex(object);
		assert(Index ~= nil);
		
		CorrectAngle(object:GetName());
		object:SetAngle(fix_angle(object:GetAngle()));
		
		if (object:GetAngle() == 0 and math.abs(object:GetX() - Element[Index].StartPos.x) < 40 and math.abs(object:GetY() - Element[Index].StartPos.y) < 40) then
			Element[Index].Active = false;
			object:Move(Element[Index].StartPos.x - object:GetX(), Element[Index].StartPos.y - object:GetY(), 0.1);
			object:SetZ(100);
		else
			object:SetZ(Element[Index].Z);
		end
		
		if (IsComplite()) then
			ShowDoneButton();
		end
	end
end

function Rotate(object)	
	local Index = GetObjectIndex(object);
	
	if (game_state.locked_objects[object:GetName()] ~= true and Element[Index].Active == true) then
		object:SetXY(GetMouseX(), GetMouseY());
		
		local af = CreateAnimationEffector()
		
		af:AttachTo(object);
		af:SetMode(_ANIM_FWD);
		af:SetKey(_TRACK_A, 0.0, object:GetAngle());
		af:SetKey(_TRACK_A, 0.2, object:GetAngle() + M_PI_OVER_2)
		af:Play();
		GTimerDoString('CorrectAngle("'..object:GetName()..'")', 0.2);
		game_state.locked_objects[object:GetName()] = true;
		GTimerDoString('game_state.locked_objects["'..object:GetName()..'"] = false', 0.2);
		
		if(DragObject == nil) then
			GTimerDoString("CheckElementPos('"..object:GetName().."')", 0.2);
		end
	end
end
--------------

function CorrectAngle(element_name)
	if(element_name ~= nil) then
		local object = game_state.screen:GetObject(element_name)
		local angle = object:GetAngle();
		object:SetAngle(fix_angle(object:GetAngle()));
		object:SetAngle(_correct(angle, 0, 					M_PI_OVER_2));
		object:SetAngle(_correct(angle, M_PI_OVER_2, 		M_PI));
		object:SetAngle(_correct(angle, M_PI, 				M_PI + M_PI_OVER_2));
		object:SetAngle(_correct(angle, M_PI + M_PI_OVER_2, M_TWO_PI));
	end
end

function _correct(angle, value1, value2)
	if (angle > value1 and angle < value2) then
		return value2;
	end
	
	return angle;
end 

function GetObjectIndex(object)
	for i = 1, ELEMENTS_AMOUNT do
		if(object:GetName() == Element[i].Obj:GetName()) then
			return i;
		end
	end
	
	return nil;
end


function IsComplite()
	for i = 1, ELEMENTS_AMOUNT do
		if(Element[i].Active == true) then
			return false;
		end
	end
	
	return true;
end

------------------------------------------------------------------------


function OnScreenShown()
end

function OnUpdate(dt)
	CheckPuzzlePiecePos(DragObject)	
	DragUpdate();
	oldMouseX = GetMouseX()
    oldMouseY = GetMouseY()	
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end
 
function OnObjectMouseDown(object)
	DefaultOnObjectMouseDown(object)
	
	if(game_state.locked_objects[object:GetName()] == true) then
		return;
	end
	
	StartDrag(object);
end

function OnObjectMouseUp(object)
	StopDrag();
end

function OnMouseMove(previous_pos, current_pos)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
	if(game_state.locked_objects[object:GetName()] == true) then
		object = nil;
	end
	
	if(object ~= nil) then
		Rotate(object);
	else
		if(DragObject ~= nil) then
			Rotate(DragObject);
		end
	end
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
    OnMiniGameButtonPress(title);
end

function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true;
		local gathering_time = 1.0    
		for i = 1, ELEMENTS_AMOUNT do
			--Element[i]:SetZ(Element[i]:GetZ() + 100)
			local af = CreateAnimationEffector()        
			af:AttachTo(Element[i].Obj)
			af:SetMode(_ANIM_FWD)
			
			af:SetKey(_TRACK_X, 0.0, Element[i].Obj:GetX())
			af:SetKey(_TRACK_X, gathering_time, Element[i].StartPos.x)
			af:SetKey(_TRACK_Y, 0.0, Element[i].Obj:GetY())        
			af:SetKey(_TRACK_Y, gathering_time, Element[i].StartPos.y)        
			af:SetKey(_TRACK_A, 0.0, Element[i].Obj:GetAngle())
			af:SetKey(_TRACK_A, gathering_time, 0)
			af:Play()        
			Element[i].Active = false;
		end
		Skip = true;
	
		return gathering_time;
	end
	
	return -1.0;
end

function OnLostFocus()
end

function OnGainFocus()
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/31_Sobor/plan_sobora_bak_2.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('plan_sobora_bak_2',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/31_Sobor/sprait_1.png',0,0,355,371,1,0)
	anim:SetHotSpot(177,185)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_1',anim,184,313,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/31_Sobor/sprait_2.png',0,0,305,324,1,0)
	anim:SetHotSpot(152,162)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_2',anim,345,471,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/31_Sobor/sprait_3.png',0,0,313,300,1,0)
	anim:SetHotSpot(156,150)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_3',anim,501,303,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/31_Sobor/sprait_4.png',0,0,229,330,1,0)
	anim:SetHotSpot(114,165)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_4',anim,659,508,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/31_Sobor/sprait_5.png',0,0,282,265,1,0)
	anim:SetHotSpot(141,132)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_5',anim,698,200,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/31_Sobor/sprait_6.png',0,0,240,302,1,0)
	anim:SetHotSpot(120,151)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_6',anim,874,378,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Comics/Sheriff.dds',0,0,530,636,1,0)
	anim:SetHotSpot(266,256)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Sheriff',anim,756,390,400,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel',anim,512,384,501,0,128,96)
	local anim = CreateAnimation('data/Texture/Comics/eyes.png',0,0,79,33,1,0)
	anim:SetHotSpot(39,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eyes',anim,750,212,500,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
