dofile('data/Scripts/Scenes/Common.lua');
dofile('data/Scripts/Scenes/Animations/26_Outro_Animations.lua');
dofile('data/Scripts/Scenes/Animations/25_NearTheCopter_Animations.lua');

------------------------------------------------------------------------------
--
function OnSceneLoad(restoringFromSave)
	InitGameState();

	play_field = playField;
    LoadData();
	play_field = nil;

    RootZoomZone = CreateZoomZone('RootZoomZone', nil, 
                                   nil, 0.0, 
                                   nil, 0.0,
							{'RootBackground'},

							{});

    HomeZoomZone = CreateZoomZone('HomeZoomZone', RootZoomZone, 
                                   'RootBackground', 0.0, 
                                   'SarahHomeBackground', 0.0,
							{'SarahHomeBackground'},

							{});

	gameState.nextAnimationDelay = 0.0;
	gameState.screenClosing = false;

	SetZoneVisible(RootZoomZone, true, false);

	auxSceneInit();
	auxProcessToolObjectStateAnCursorType();

	SetLastLevelIndex(0);
end

------------------------------------------------------------------------------
--
function OnSceneFree()
    FreeData()
end

--// Local auxiliary functions - - - - - - - - - - - - - - - - - - - - - - - -
function auxSceneInit()
--	gameState.customCursors[''] = {'CursorZoom'};
--	playField:IgnoreOnTestPoint('');

	gameState.customCursors['RootBackground'] = nil;
	gameState.customCursors['SarahHomeBackground'] = nil;
	gameState.lockedObjects['RootBackground'] = true;
	gameState.lockedObjects['SarahHomeBackground'] = true;

	CreateCreditsVisualNode(playField, --:GetLevelScreen(),
							SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0, 400.0,
							-300, -380.0,
							600, 760.0,
							-2700.0,
							SCREEN_HEIGHT + 100.0 + 700.0);

	auxCreateOutroAnimations(playField:GetObject('SarahHomeBackground'));
	auxPlayOutroAnimation('Top');
--	auxPlayOutroAnimation('Dino');
--	auxPlayOutroAnimation('Seagull1');
--	auxPlayOutroAnimation('Seagull2');
--	auxPlayOutroAnimation('Seagull3');
	auxPlayOutroAnimation('Sea');
	auxPlayOutroAnimation('Pool');

	auxCreateNearTheCopterAnimations(playField:GetObject('RootBackground'));
	auxPlayNearTheCopterAnimation('EnvTop');
--	auxPlayNearTheCopterAnimation('Crab1');
--	auxPlayNearTheCopterAnimation('Crab2');
	auxPlayNearTheCopterAnimation('Balloon');
--	auxPlayNearTheCopterAnimation('Seagull');
--	auxPlayNearTheCopterAnimation('BurningEmbers');
	auxPlayNearTheCopterAnimation('Wood');
--	auxPlayNearTheCopterAnimation('Pilot');
end

function auxProcessToolObjectStateAnCursorType()
--    assert(	gameState. ~= nil 
end

--// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function auxUpdatePeriodicAnimations(dt)
	if (gameState.nextAnimationDelay == 0.0) then
		local animations1 = {};
		local animations2 = {};

		for compositionName, composition in pairs(nearTheCopterCompositions) do
			if (composition:IsCyclic() == false and composition:GetName()~= 'Balloon') then
				table.insert(animations1, composition:GetName());
			end
		end

		for compositionName, composition in pairs(outroCompositions) do
			if (composition:IsCyclic() == false) then
				table.insert(animations2, composition:GetName());
			end
		end

		if (math.random(1,2) == 1) then
			local animName = animations1[math.random(1, table.getn(animations1))];
			if (animName ~= nil) then
				local composition = auxGetNearTheCopterAnimation(animName);

	            if (composition ~= nil and composition:IsPaused() == true) then
					auxPlayNearTheCopterAnimation(animName);
				end
			end
		else
			local animName = animations2[math.random(1, table.getn(animations2))];
			if (animName ~= nil) then
				local composition = auxGetOutroAnimation(animName);

	            if (composition ~= nil and composition:IsPaused() == true) then
					auxPlayOutroAnimation(animName);
				end
			end
		end
		gameState.nextAnimationDelay = math.random(4, 13);
	else
		gameState.nextAnimationDelay = (gameState.nextAnimationDelay - dt >= 0.0 and gameState.nextAnimationDelay - dt or 0.0);
	end
end
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

------------------------------------------------------------------------------
--
function OnDialogButtonPress(name, param)
end

-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
--
function auxPlayDinoAnimation()
	if (auxGetOutroAnimation('Dino'):IsVisible() == false) then
		auxPlayOutroAnimation('Dino');
	end
end

------------------------------------------------------------------------------
--
function OnUpdate(dt)
    DefaultOnUpdate(dt);

	if (gameState.elapsedTime > 35.0) then
		if (gameState.activeZone == RootZoomZone) then
--// (!) small hack 8\
local defZZAppearingTime = ZOOM_ZONE_APPEARING_TIME;
ZOOM_ZONE_APPEARING_TIME = 3.0;
			SetZoneVisible(HomeZoomZone, true);
			TimerDoString('auxPlayDinoAnimation();', ZOOM_ZONE_APPEARING_TIME + 1.0, playField:GetTimer());
ZOOM_ZONE_APPEARING_TIME = defZZAppearingTime;

		elseif (gameState.elapsedTime > 110.0 and gameState.screenClosing == false) then
			gameState.screenClosing = true;
			playField:GetLevelScreen():ExitToMenu();
		end
	end
	auxUpdatePeriodicAnimations(dt);
end

------------------------------------------------------------------------------
--
function OnWin()
--	DefaultOnWin();
end

------------------------------------------------------------------------------
--
function OnObjectMouseEnter(object)
    local objectName = object:GetName();

    DefaultOnObjectMouseEnter(object);
	ActivateActiveZoneGlow(objectName);
end

------------------------------------------------------------------------------
--
function OnObjectMouseLeave(object)
    local objectName = object:GetName();

    DefaultOnObjectMouseLeave(object);
    DeactivateActiveZoneGlow(objectName);
end

------------------------------------------------------------------------------
--
function OnObjectMouseDown(object)
    local objectName = object:GetName();
    local isWrongClick = true;

	assert(gameState.activeZone ~= nil and gameState.locker:GetNReferences() > 0);

--	do
--// SPECIAL FOR OUTRO (DEBUG) ... 
	if (gameState.lockedObjects[objectName] ~= true) then
        if (gameState.activeZone.HasObject(objectName)) then
            isWrongClick = DefaultOnZoomZoneActiveObjectClick(objectName);

			--// ...

	        if (isWrongClick == true and playField:GetLevelScreen():IsTaskObject(objectName)) then
	            auxAttachInPlace(object, playField);
				object:ResetClippingRect();
				--// remove object from zoom-zone which contents it
				assert(gameState.activeZone.objects[objectName] ~= nil);
				gameState.activeZone.objects[objectName] = nil;

                playField:OnObjectFound(object);
                isWrongClick = false;
            end
        else
--			assert(gameState.activeZone ~= RootZoomZone and 
--				   gameState.locker:GetNReferences() > 1 and
--				   'Attempt to close root scene - you have clicked on an object which hasn`t been added to any zoom-zone');
            if (gameState.activeZone ~= RootZoomZone and gameState.locker:GetNReferences() > 1) then
                if (gameState.lockedObjects[gameState.activeZone.activeObject] == nil) then
                    SetZoneVisible(gameState.activeZone, false);
                end
            else
                Log('Lua: OnObjectMouseDown(\''..objectName..'\'): Was made click on object which hasn`t been attached to any zoom-zone');
            end
        end --// if (gameState.activeZone.HasObject(...
    else
        return;
    end --// if (gameState.lockedObjects[...

	--// Tips - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
--	if (objectName == 'actZoneSarcophagusKey2') then
--	    playField:GetLevelScreen():ShowTip(GetTextString('LEVEL1_SARCOPHAGUS_TIP1'), ...);
--	elseif (objectName == 'ZoomGateLockBackground') then
--		...
--	else
--	    playField:GetLevelScreen():HideTip();
--	end
	--//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    DefaultOnObjectClick(object, isWrongClick, false);
    DefaultOnObjectMouseEnter(object);
end

------------------------------------------------------------------------------
--
function OnObjectMouseUp(object)
end

------------------------------------------------------------------------------
--
function OnObjectFound(object)
    local objectName = object:GetName();
	-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	local objectMustBeHighlighted = DefaultOnObjectFound(object);
	return objectMustBeHighlighted;
end

------------------------------------------------------------------------------
--
function OnToolFound(tool)
end

------------------------------------------------------------------------------
--
function CanToolBeApplied(tool, object)
	local toolName = tool:GetName();
	local objName = object:GetName();
    local canToolBeApplied = true;
	-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	canToolBeApplied = DefaultCanToolBeApplied(tool, object);
	return canToolBeApplied;
end

------------------------------------------------------------------------------
--
function OnToolApply(tool, object)
	local toolName = tool:GetName();
	local objName = object:GetName();
    local toolApplied = false;

	-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	if (not toolApplied) then
	    playField:GetLevelScreen():ShowTip(GetTextString('WRONG_ITEM_APPLIED_TIP1'),
										   GetTextString('WRONG_ITEM_APPLIED_TIP2'));
	end
	-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    DefaultOnObjectMouseEnter(object);
	if (toolApplied == true) then
	    DeactivateActiveZoneDeactivatedGlow(object:GetName());
	end
	return toolApplied;
end

------------------------------------------------------------------------------
--
function OnObjectReachPanel(object)
    local objectName = object:GetName();
end

------------------------------------------------------------------------------
--
function OnButtonPress(zoneName)
    DefaultOnButtonPress(zoneName);
end

------------------------------------------------------------------------------
--
function OnMouseMove(prevMousePos, currentMousePos)
end

------------------------------------------------------------------------------
--
function OnSetTool(tool)
end

------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData()
	local anim = CreateAnimation('data/Texture/Scenes/25_NearTheCopter/Layers/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('RootBackground',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/26_Outro/Layers/SarahHomeBackground.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('SarahHomeBackground',anim,512,384,200,0,1,1)
end

function FreeData()
end
-- CLEAR_ON_SAVE_END
