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

MATCH_BOX_AMOUNT = 5;

HIDE_SHOW_TIME = 0.4

NEEDED_X = 763;
NEEDED_Y = 444;

function OnSceneLoad(screen)
    LoadData(screen);

	play_field.game_state = game_state;
	MiniGameField = screen;
	
    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_19";
	
	--game_state.locked_objects["background"] = true;
	game_state.locked_objects["santa_maria"] = true;
	game_state.locked_objects["crazy_horse"] = true;
	game_state.locked_objects["black_jack"] = true;
	game_state.locked_objects["de_la_santas"] = true;
	game_state.locked_objects["viktoria"] = true;
	
	ObjectIDs = {"de_la_santas", "black_jack", "viktoria", "santa_maria", "crazy_horse"};

	Object = {};
	AddObject("santa_maria", 1, false, 4);
	AddObject("crazy_horse", 1, false, 5);
	AddObject("black_jack", 1, false, 2);
	AddObject("de_la_santas", 1, false, 1);
	AddObject("viktoria", 1, false, 3);
	
	AddObject("lupa", 2, false, 0);
	AddObject("lupa_2", 2, false, 0);
	AddObject("lupa_3", 2, false, 0);
	AddObject("lupa_4", 2, false, 0);
	AddObject("lupa_5", 2, false, 0);
	
	AddObject("pixel_1", 3, true, 1);
	AddObject("pixel_2", 3, true, 2);
	AddObject("pixel_3", 3, true, 3);
	AddObject("pixel_4", 3, true, 4);
	AddObject("pixel_5", 3, true, 5);
	
	game_state.custom_cursors['pixel_1'] = 'CursorZoom'
	game_state.custom_cursors['pixel_2'] = 'CursorZoom'
	game_state.custom_cursors['pixel_3'] = 'CursorZoom'
	game_state.custom_cursors['pixel_4'] = 'CursorZoom'
	game_state.custom_cursors['pixel_5'] = 'CursorZoom'
	
	AddObject("spichka", 5, false, 0);
	AddObject("spichka_2", 5, false, 0);
	AddObject("spichka_4", 5, true, 0);
	
	Object["santa_maria"].Lupa = Object["lupa"].Object;
	Object["crazy_horse"].Lupa = Object["lupa_2"].Object;
	Object["black_jack"].Lupa = Object["lupa_3"].Object;
	Object["de_la_santas"].Lupa = Object["lupa_4"].Object;
	Object["viktoria"].Lupa = Object["lupa_5"].Object;
	
	Radius = 100;--Object["santa_maria"].Object:GetAnimation():GetHeight() / 2 - ;
	
	game_state.timer = screen:GetTimer()
	AddMiniGamesGUI(screen);

	InitChain1(screen)
    AddSheriffEyeLogic(screen)
    
	screen:SetLayer(2); 
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_19_7"))
                                    
    comics1.OnShown = function()
        OnMinigameComicsShown(comics1)
    end
end

function OnSceneFree(screen)
    FreeData(screen);
end

function AddObject(ObjectName, Type, Visible, Index)
	Object[ObjectName] = {};
	Object[ObjectName].Object = MiniGameField:GetObject(ObjectName); 
	Object[ObjectName].Type = Type;
	Object[ObjectName].Object:SetVisible(Visible);
	Object[ObjectName].Index = Index;
end

-------------------------------------------------------------------------
--Drag functions

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

function IsComplite()
	return false;
end

function GetMatchBox(Index)
	for i = 1, MATCH_BOX_AMOUNT do
		if(Object[ObjectIDs[i]].Index == Index) then
			return Object[ObjectIDs[i]];
		end
	end
	
	return nil;
end


function ShowZoom(object)
	ZoomObject = GetMatchBox(Object[object:GetName()].Index).Object;
	
	local X = object:GetX();

	if(ZoomObject:GetName() == ObjectIDs[1]) then
		X = ZoomObject:GetAnimation():GetWidth() / 2;
	end
	if(ZoomObject:GetName() == ObjectIDs[5]) then
		X = 1024 - ZoomObject:GetAnimation():GetWidth() / 2;
	end
		
	ZoomObject = GetMatchBox(Object[object:GetName()].Index).Object;
	ZoomObject:SetXY(X, object:GetY());
	ZoomObject:SetVisible(true);
	
	Object[ZoomObject:GetName()].Lupa:SetXY(X, object:GetY());
	Object[ZoomObject:GetName()].Lupa:SetVisible(true);
	
	local af = CreateAnimationEffector()--AnimationEffector()
    af:AttachTo(ZoomObject)
    af:SetKey(_TRACK_S, 0.0, 0.1)
    af:SetKey(_TRACK_S, HIDE_SHOW_TIME, 1.0)
	af:SetKey(_TRACK_O, 0.0, 0.0)
    af:SetKey(_TRACK_O, HIDE_SHOW_TIME, 1.0)
    af:Play()
    
	local af2 = CreateAnimationEffector()--AnimationEffector()
    af2:AttachTo(Object[ZoomObject:GetName()].Lupa)
    af2:SetKey(_TRACK_S, 0.0, 0.1)
    af2:SetKey(_TRACK_S, HIDE_SHOW_TIME, 1.0)
    af2:SetKey(_TRACK_O, 0.0, 0.0)
    af2:SetKey(_TRACK_O, HIDE_SHOW_TIME, 1.0)
    af2:Play();
    
    Active = false;
	GTimerDoString("Active = true", HIDE_SHOW_TIME)
end

function HideZoom(Box)
	local af = CreateAnimationEffector();--AnimationEffector()
    af:AttachTo(Box);
    af:SetKey(_TRACK_S, 0.0, 1.0);
    af:SetKey(_TRACK_S, HIDE_SHOW_TIME, 0.001);
    af:Play();
    
	local af2 = CreateAnimationEffector();--AnimationEffector()
    af2:AttachTo(Object[ZoomObject:GetName()].Lupa);
    af2:SetKey(_TRACK_S, 0.0, 1.0);
    af2:SetKey(_TRACK_S, HIDE_SHOW_TIME, 0.001);
    af2:Play();
    
    Active = false;
	GTimerDoString("Active = true", HIDE_SHOW_TIME);
end

function StopDrag()
	if(DragObject ~= nil) then
		if(math.abs(DragObject:GetX() - NEEDED_X) < 20 and math.abs(DragObject:GetY() - NEEDED_Y) < 40 and ZoomObject:GetName() == 'santa_maria') then
			Object["spichka"].Object:Move(NEEDED_X - DragObject:GetX(), NEEDED_Y - DragObject:GetY(), 0.2);
			--Object["spichka"].Object:SetXY(NEEDED_X, NEEDED_Y);
			game_state.game_complite = true;
			ShowDoneButton();
		else
			Object["spichka_4"].Object:SetVisible(true);
			Object["spichka_2"].Object:SetVisible(false);
			Object["spichka"].Object:SetVisible(false);
		end
	
		DragObject = nil;
	end
end

function StartDrag()
	Object["spichka_4"].Object:SetVisible(false);
	
	DragObject = Object["spichka_2"].Object;
	DragObject:SetVisible(true);
	DragObject:SetXY(GetMouseX(), GetMouseY());
	DragObject:SetZ(200);
end

function UpdateDrag()
	if(DragObject ~= nil) then
		DragObject:SetXY(GetMouseX(), GetMouseY());
		local X = math.abs(DragObject:GetX() - ZoomObject:GetX()) - 65;
		local Y = math.abs(DragObject:GetY() - ZoomObject:GetY());
		
		if(X * X + Y * Y <= Radius * Radius) then
				ShowBigMatch();
				return;
		end
		ShowSmallMatch();
	end
end

function ShowBigMatch()
	if(DragObject:GetName() ~= "spichka") then
		Object["spichka_2"].Object:SetVisible(false);
		
		DragObject = Object["spichka"].Object;
		DragObject:SetVisible(true);
		DragObject:SetXY(GetMouseX(), GetMouseY());
		DragObject:SetZ(251);
	end
end

function ShowSmallMatch()
	if(DragObject:GetName() ~= "spichka_2") then
		Object["spichka"].Object:SetVisible(false);
		
		DragObject = Object["spichka_2"].Object;
		DragObject:SetVisible(true);
		DragObject:SetXY(GetMouseX(), GetMouseY());
		DragObject:SetZ(200);
	end
end
------------------------------------------------------------------------

function OnScreenShown()
end

function OnUpdate(dt)
	UpdateDrag();
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 or game_state.game_complite == true) then
		return;
	end
	
	if(object:GetName() == "paketik" and ZoomObject ~= nil) then
		StartDrag();
		return;
	end
	
	for i = 1, MATCH_BOX_AMOUNT do
		if(Object[object:GetName()] ~= nil and Object[object:GetName()].Type == 3) then
			if(Active ~= false) then
				if(ZoomObject ~= nil) then
					HideZoom(ZoomObject);
					if(ZoomObject:GetName() ~= object:GetName()) then
						ShowZoom(object);
					end
				else
					ShowZoom(object);
				end
			end
			return
		else
			if(Object[object:GetName()] ~= nil and Object[object:GetName()].Type == 2) then
				if(Active ~= false and ZoomObject ~= nil) then
					HideZoom(ZoomObject);
					ZoomObject = nil;
				end
				return;
			end
		end
	end
	
	if(Active ~= false and ZoomObject ~= nil) then
		HideZoom(ZoomObject);
		ZoomObject = nil;
	end
end

function OnObjectMouseUp(object)
	StopDrag();
end

function OnMouseMove(previous_pos, current_pos)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

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

function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true;
		return 0.1;
	end
	
	return 1.0;
end

function OnLostFocus()
end

function OnGainFocus()
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
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/19_match/bak.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/santa_maria.png',0,0,471,465,1,0)
	anim:SetHotSpot(235,232)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('santa_maria',anim,834,135,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/crazy_horse.png',0,0,471,465,1,0)
	anim:SetHotSpot(235,232)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('crazy_horse',anim,852,110,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/black_jack.png',0,0,471,465,1,0)
	anim:SetHotSpot(235,232)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('black_jack',anim,835,129,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/de_la_santas.png',0,0,471,465,1,0)
	anim:SetHotSpot(235,232)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('de_la_santas',anim,829,125,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/viktoria.png',0,0,471,465,1,0)
	anim:SetHotSpot(235,232)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('viktoria',anim,828,123,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/lupa.png',0,0,503,508,1,0)
	anim:SetHotSpot(251,254)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('lupa',anim,845,123,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('0x01FFFFFF')
	screen:CreateObject('pixel_1',anim,148,494,101,0,20,20)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('pixel_2',anim,327,500,101,0,20,20)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('pixel_3',anim,516,503,101,0,20,20)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('pixel_4',anim,693,496,101,0,20,20)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('pixel_5',anim,873,506,101,0,20,20)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/spichka.png',0,0,86,308,1,0)
	anim:SetHotSpot(43,154)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('spichka',anim,317,101,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/spichka_2.png',0,0,49,112,1,0)
	anim:SetHotSpot(24,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('spichka_2',anim,532,69,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/paketik.png',0,0,203,266,1,0)
	anim:SetHotSpot(101,133)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('paketik',anim,83,135,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/spichka_4.png',0,0,105,78,1,0)
	anim:SetHotSpot(52,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('spichka_4',anim,90,165,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/lupa.png',0,0,503,508,1,0)
	anim:SetHotSpot(251,254)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('lupa_2',anim,844,125,255,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/lupa.png',0,0,503,508,1,0)
	anim:SetHotSpot(251,254)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('lupa_3',anim,844,128,255,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/lupa.png',0,0,503,508,1,0)
	anim:SetHotSpot(251,254)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('lupa_4',anim,847,129,255,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/19_match/lupa.png',0,0,503,508,1,0)
	anim:SetHotSpot(251,254)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('lupa_5',anim,843,128,255,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,409,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,451,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,450,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
