
-----------------------------------------------------------------
--
--Временные переменные для гибкости теста
START_TOWER = 1;

TOWERS_AMOUNT = 3;

KOLESO_HEIGHT  = 55;

function OnSceneLoad(screen)
    LoadData(screen);

	play_field.game_state = game_state;
	
	Towers_info = {};
	Towers_info.top = 200;
	Towers_info.buttom = 710;
	
	Towers_info.x = {200, 500, 815};
	Towers_info.height = {0, 0, 0};

    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_25";
    
	game_state.locked_objects['bak'] = true;
	game_state.locked_objects['kustik_left'] = true;
	game_state.locked_objects['kustik_right'] = true;
    
    RegisterElement(screen:GetObject('koleso_1'), 1);
    RegisterElement(screen:GetObject('koleso_3'), 2);
    RegisterElement(screen:GetObject('koleso_2'), 3);
    RegisterElement(screen:GetObject('koleso_4'), 4);
    
    Tower = {};
    Tower[1] = {};
    Tower[2] = {};
    Tower[3] = {};
    
    PushBack(Tower[1], screen:GetObject('koleso_1'));
    PushBack(Tower[1], screen:GetObject('koleso_3'));
    PushBack(Tower[1], screen:GetObject('koleso_2'));
	PushBack(Tower[1], screen:GetObject('koleso_4'));
	
	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_25_5"))
                                    
    comics1.OnShown = function()
        OnMinigameComicsShown(comics1)
    end
end


function RegisterElement(object, index)
	local element_data = {};
	if not game_state.elements then
        game_state.elements = {};
    end
	element_data.name = object:GetName();
	element_data.pos = {};
	element_data.width = object:GetAnimation():GetWidth();
	element_data.height = object:GetAnimation():GetHeight();
	element_data.pos.x = Towers_info.x[START_TOWER];
	element_data.pos.y = Towers_info.buttom - Towers_info.height[START_TOWER];
	element_data.index = index
	
	Towers_info.height[START_TOWER] = Towers_info.height[START_TOWER] + KOLESO_HEIGHT; 
	
	object:SetX(element_data.pos.x);
	object:SetY(element_data.pos.y);
	
    table.insert(game_state.elements, element_data);
    game_state.elements_to_finish = game_state.elements_to_finish + 1;
end

---------------------------
function PushBack(Array, Element)
	assert(Array ~= nil);
	
	Array[#Array + 1] = Element;
end

function PopBack(Array)
	assert(Array ~= nil);	
	Array[#Array] = nil;
end

function GetBack(Array)
	assert(Array ~= nil);	
	return Array[#Array];
end

function GetLenght(Array)
	assert(Array ~= nil);
	return #Array;
end
-------------------------------------------------------------------------
--drag function
function Drag(MouseX, MouseY)
    if(DragObject ~= nil) then
        DragObject:SetX(MouseX);
    end
end

function StopDrag()
    if(DragObject ~= nil) then
		if (DragObject:GetX() < 350) then
			AddToTower(1);
		else
			if (DragObject:GetX() < 650) then
				AddToTower(2);
			else
				AddToTower(3);
			end
		end
		DragObject = nil;
    end
end

function StartDrag(object)
	if(game_state.game_complite == false) then
		for i = 1, TOWERS_AMOUNT do
			if(GetBack(Tower[i]) ~= nil and object:GetName() == GetBack(Tower[i]):GetName()) then
				PlaySound("25_Hanoi_in");
				DragObject = object;
				TowerOld = i;
				DragObject:SetZ(300.0);
				DragObject:SetY(Towers_info.top);
				MousePos = GetMousePos();
				Towers_info.height[i] = Towers_info.height[i] - KOLESO_HEIGHT;
				PopBack(Tower[i]);
			end
		end
	end
end


function AddToTower(TowerID)
	assert(DragObject ~= nil);
	
	if(GetBack(Tower[TowerID]) ~= nil and GetBack(Tower[TowerID]):GetAnimation():GetWidth() < DragObject:GetAnimation():GetWidth()) then
		TowerID = TowerOld;
	end
	
	PlaySound("25_Hanoi_out");
	PushBack(Tower[TowerID], DragObject);
	DragObject:Move(Towers_info.x[TowerID] - DragObject:GetX(), 0.0, 0.2);
	GTimerDoString("GetBack(Tower["..TowerID.."]):Move(0.0, Towers_info.buttom - "..Towers_info.height[TowerID].." - "..DragObject:GetY()..", 0.2)", 0.3);
	Towers_info.height[TowerID] = Towers_info.height[TowerID] + KOLESO_HEIGHT;
	
	if(TowerID == 3 and GetLenght(Tower[TowerID]) == 4) then
		game_state.game_complite = true
		ShowDoneButton();
	end
end
------------------------------------------------------------------------

function SkipMiniGame()
	if(game_state.game_skip == false) then
		game_state.game_skip = true;
		
		for i = 1,4 do
			game_state.screen:GetObject('koleso_'..i):SetVisible(false);
			game_state.screen:GetObject('koleso_'..i):SetX(815);
		end
		
		game_state.screen:GetObject('koleso_4'):SetY(556);
		game_state.screen:GetObject('koleso_3'):SetY(669);
		game_state.screen:GetObject('koleso_2'):SetY(613);
		game_state.screen:GetObject('koleso_1'):SetY(721);
		
		for i = 1,4 do
			game_state.screen:GetObject('koleso_'..i):SetVisible(true);
			game_state.screen:GetObject('koleso_'..i):ChangeOpacity(0.0, 1.0, 0.5);
		end
				
		return 0.7;
	end
	
	return -1.0;
end

function OnScreenShown()
end

function OnSceneFree(screen)
    FreeData(screen)
end

function OnUpdate(dt)
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
	if(game_state.locked_objects[object:GetName()] ~= true and game_state.game_skip == false) then
		StartDrag(object);
	end
	
	DefaultOnObjectMouseDown(object)
end
function OnObjectMouseUp(object)
    StopDrag();
end

function OnMouseMove(previous_pos, current_pos)
    Drag(current_pos.x, current_pos.y);
end

function OnObjectRightMouseUp(object)
end

function OnObjectWillBeFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
end

function OnObjectReachPanel(object)
end

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

function OnAllPiecesReachedPositions()
    for i, v in ipairs(game_state.elements) do
        local object = puzzle:GetObject(v.name)
        object:SetZ(puzzle:GetObject('border'):GetZ() - 1);
    end
    OnAllPuzzlePiecesPlaced()
end

function OnSetTool(tool)
end

function CanBeHinted(obj)
    if locked_object[obj] ~= true then
        return true
    else
        return false
    end
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/25_Tower/bak.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bak',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/25_Tower/koleso_1.png',0,0,280,81,1,0)
	anim:SetHotSpot(140,40)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('koleso_1',anim,203,708,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/25_Tower/koleso_2.png',0,0,198,70,1,0)
	anim:SetHotSpot(99,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('koleso_2',anim,196,604,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/25_Tower/koleso_3.png',0,0,244,78,1,0)
	anim:SetHotSpot(122,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('koleso_3',anim,199,655,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/25_Tower/koleso_4.png',0,0,174,75,1,0)
	anim:SetHotSpot(87,37)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('koleso_4',anim,197,550,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/25_Tower/kustik_left.png',0,0,174,233,1,0)
	anim:SetHotSpot(87,116)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('kustik_left',anim,87,653,300,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/25_Tower/kustik_right.png',0,0,130,179,1,0)
	anim:SetHotSpot(65,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('kustik_right',anim,959,679,300,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,505,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,499,212,500,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
