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

ELEMENTS_AMOUNT = 16;

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.locker = ReferenceCounter()
    game_state.game_complite = false; 
	game_state.screen = screen;
	game_state.HELP_STRING = "HELP_MINIGAME_17";
	game_state.game_skip = false;
	
	game_state.locked_objects['bak'] = true;
	Skip = false;
	
	--Object Initialization
	Element = {};
	for i = 1, ELEMENTS_AMOUNT do
		Element[i] = {};
		Element[i].Obj = screen:GetObject('pazl_'..i); 
		Element[i].StartPos = Element[i].Obj:GetPosition();
		Element[i].Active = true;
		Element[i].Z = 200 + i;
		local r = math.random(2);
		local X = math.random(100) + 800 * (r - 1) + 100;
		local Y = 0;
		if(r == 1) then
			Y = math.random(100) + 400 * (math.random(2) - 1) + 100;
		else
			Y = math.random(100) + 500;
		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);
	screen:SetLayer(2);   
	
	InitComics1();
	AddSheriffEyeLogic(screen)
end

function OnSceneFree(screen)
    FreeData(screen);
end

------------------------------------------------------------------------------------------------------------------
--Comics

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

function ShowComics1()
	game_state.chain1.Start();
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 (Skip ~= true and 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.21);
		
		if(DragObject == nil) then
			GTimerDoString("CheckElementPos('"..object:GetName().."')", 0.22);
		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)
	if(game_state.locked_objects[object:GetName()] == true) then
		return;
	end
	
	StartDrag(object);
	
	DefaultOnObjectMouseDown(object, play_field);
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 1.5;
    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/17_photo_pazzle/bak.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bak',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_1.png',0,0,279,223,1,0)
	anim:SetHotSpot(139,111)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_1',anim,258,272,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_2.png',0,0,160,96,1,0)
	anim:SetHotSpot(80,48)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_2',anim,330,184,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_3.png',0,0,179,223,1,0)
	anim:SetHotSpot(89,111)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_3',anim,388,243,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_4.png',0,0,203,237,1,0)
	anim:SetHotSpot(101,118)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_4',anim,496,226,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_5.png',0,0,123,220,1,0)
	anim:SetHotSpot(61,110)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_5',anim,607,227,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_6.png',0,0,280,235,1,0)
	anim:SetHotSpot(140,117)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_6',anim,687,191,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_7.png',0,0,153,351,1,0)
	anim:SetHotSpot(76,175)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_7',anim,223,517,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_8.png',0,0,287,223,1,0)
	anim:SetHotSpot(143,111)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_8',anim,402,423,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_9.png',0,0,157,202,1,0)
	anim:SetHotSpot(78,101)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_9',anim,530,400,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_10.png',0,0,253,239,1,0)
	anim:SetHotSpot(126,119)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_10',anim,667,376,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_11.png',0,0,148,173,1,0)
	anim:SetHotSpot(74,86)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_11',anim,772,342,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_12.png',0,0,183,225,1,0)
	anim:SetHotSpot(91,112)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_12',anim,335,562,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_13.png',0,0,180,170,1,0)
	anim:SetHotSpot(90,85)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_13',anim,420,570,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_14.png',0,0,183,177,1,0)
	anim:SetHotSpot(91,88)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_14',anim,540,554,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_15.png',0,0,169,160,1,0)
	anim:SetHotSpot(84,80)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_15',anim,661,546,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/17_photo_pazzle/pazl_16.png',0,0,193,241,1,0)
	anim:SetHotSpot(96,120)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pazl_16',anim,782,490,200,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,378,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,401,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,369,212,500,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
