dofile('data/Scripts/Scenes/Animations/WinAnimation.lua')

------------------------------------------------------------------------------
-- 
function CreateWindowShade(parent, z, color)
	local fog = CreateSprite(); 
	local posZ = z;
	local color = color;
	
	if (posZ == nil) then
		posZ = -1.0;
	end
	if (color == nil) then
		color = '0x80FFFFFF';
	end

	fog:AttachTo(parent);
	fog:SetSprite('DummyTexture');
	fog:SetColor(color);
	fog:SetXY(0.0, 0.0);
	fog:SetScale(2000.0);
	fog:SetZ(posZ);
	fog:SetVisible(true);

	return fog;
end

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

	if (lastEnteredObjectName == nil or lastEnteredObjectName ~= objectName) then
		lastEnteredObjectName = objectName;
	else
		return
	end
    
	if (gameState.customCursors ~= nil and
        gameState.customCursors[objectName] ~= nil and
        gameState.customCursors[objectName][1] ~= nil and 
        gameState.customCursors[objectName][1] ~= '' and
        gameState.activeZone.HasObject(objectName)) then

        assert(gameState);
        
	    name = gameState.customCursors[objectName][1];
	    if (name ~= 'CursorNavigation') then
	        GetGameApp():SetCustomCursor(name);
	    else
	        local cursorAngle = (gameState.customCursors[objectName][2] ~= nil and gameState.customCursors[objectName][2] or 0.0);
	        GetGameApp():SetCustomCursor(name, cursorAngle);
	    end

		--// highlight active zone of zoom-zone
   		for zoneName,zone in pairs(gameState.zoomZones) do
  			if ((zone.navigationObject ~= nil and objectName == zone.navigationObject) or
				(zone.activeObject ~= nil and objectName == zone.activeObject)) then

  			    assert(gameState ~= nil and gameState.zoomZones[zoneName] ~= nil);

	            HighlightGameObject(playField:GetObject(objectName), playField:GetTimer(), 1.5);
			    break;
		    end
		end
	    
	    -- PlaySound('L99S04')
	else
	    GetGameApp():SetCustomCursor('CursorArrow');
	end
end

------------------------------------------------------------------------------
-- 
function DefaultOnObjectMouseLeave(object)
    -- PlaySound('L99S04')
    GetGameApp():SetCustomCursor('CursorArrow');
end

------------------------------------------------------------------------------
-- 
function DefaultOnObjectClick(object, isWrongObject, playWrongObjectSound)
    if (isWrongObject == true) then
        --// clicking punishment	
		assert(gameState ~= nil and 
			   gameState.elapsedTime ~= nil and gameState.prevWrongClickTimeStamp ~= nil and
               gameState.punishmentDuration ~= nil and gameState.punishmentClickCount ~= nil);
		assert(table.getn(gameState.prevWrongClickTimeStamp) > gameState.punishmentClickCount and
			   table.getn(gameState.prevWrongClickTimeStamp) < 10);

		for i = table.getn(gameState.prevWrongClickTimeStamp),2,-1 do
			gameState.prevWrongClickTimeStamp[i] = gameState.prevWrongClickTimeStamp[i - 1];
		end
		gameState.prevWrongClickTimeStamp[1] = gameState.elapsedTime;

		if (gameState.prevWrongClickTimeStamp[1] - gameState.prevWrongClickTimeStamp[gameState.punishmentClickCount] < gameState.punishmentDuration) then
			if (not GetGameApp():IsFastClickPunishmentEnabled()) then
			    GetGameApp():EnableFastClickPunishment();

        		for i = table.getn(gameState.prevWrongClickTimeStamp),2,-1 do
			        gameState.prevWrongClickTimeStamp[i] = -gameState.punishmentDuration;
		        end
			end
		end

        --// n-click on the same object blocking code
        local objectName = object:GetName();
   		for zoneName,zone in pairs(gameState.zoomZones) do
  			if (zone.navigationObject ~= nil and objectName == zone.navigationObject and (prevObjectName == nil or objectName ~= prevObjectName)) then
  			    assert(gameState ~= nil and gameState.zoomZones[zone.name] ~= nil);

                prevObjectName = objectName;
                TimerDoString('prevObjectName = nil;', 1.0, playField:GetTimer());
                
				isWrongObject = false;
  			    DefaultOnButtonPress(zone.name);
			    break;
		    end
		end

		if (isWrongObject == true and playWrongObjectSound ~= false) then
	        playField:ShowWrongObjectEffect();
	        PlaySound('L99S02');
		end
    end
end

------------------------------------------------------------------------------
-- 
function DefaultOnZoomZoneActiveObjectClick(objectName)
   	for i,zone in pairs(gameState.zoomZones) do
	    if (zone.activeObject ~=nil and zone.activeObject == objectName) then
	        SetZoneVisible(zone, true);
   			return false;
	    end
    end
    return true;
end

------------------------------------------------------------------------------
-- 
function DefaultOnObjectFound(object)
	do
		local soundList = {'L99S09', 'L99S10', 'L99S11'};
		PlaySound(soundList[math.random(1, table.getn(soundList))]);
    end
	do 
		local soundList = {'L99S05', 'L99S06'};
		local soundName = soundList[math.random(1, table.getn(soundList))];
		TimerDoString('PlaySound(\''..soundName..'\');', 1.1, playField:GetTimer());
	end
	--// (!) by default all found objects must be highlighted by particle tail
	return true;
end

------------------------------------------------------------------------------
-- 
function DefaultOnButtonPress(zoneName)
   	for i,zone in pairs(gameState.zoomZones) do
	    if (zone.name == zoneName) then
	        SetZoneVisible(zone, false);
   			break;
	    end
    end
end

------------------------------------------------------------------------------
-- 
function DefaultOnUpdate(dt)
	assert(gameState ~= nil and gameState.elapsedTime ~= nil);
	gameState.elapsedTime = gameState.elapsedTime + dt;

    --// periodical random zoom-zone highlight
	assert(gameState.highlightPeriod ~= nil);
    gameState.randomZoneHighlightDelay = gameState.randomZoneHighlightDelay + dt;
    
    if (gameState.randomZoneHighlightDelay > gameState.highlightPeriod) then
        gameState.randomZoneHighlightDelay = gameState.randomZoneHighlightDelay - gameState.highlightPeriod;
        
        assert(gameState.activeZone ~= nil);
        local activeZoneObjectsInCurrentZone = {};

       	for zoneName,zone in pairs(gameState.zoomZones) do
	        if (zone.parentZoomZone ~= nil and
                zone.parentZoomZone == gameState.activeZone and gameState.lockedObjects[zone.activeObject] == nil) then
                table.insert(activeZoneObjectsInCurrentZone, zone.activeObject);
	        end
        end

--
local HIGHLIGHT_ACTIVE_OBJECTS_IN_ACTIVE_ZONE = true;
		-- add active interactive objects
		if (HIGHLIGHT_ACTIVE_OBJECTS_IN_ACTIVE_ZONE and
			gameState.customCursors and gameState.activeZone) then
			for objectName, cursorType in pairs(gameState.customCursors) do
				if (cursorType[1] == 'CursorArms' and gameState.activeZone.HasObject(objectName)) then
	                table.insert(activeZoneObjectsInCurrentZone, objectName);
				end
			end
		end
        
        local objectCount = table.getn(activeZoneObjectsInCurrentZone);
        if (objectCount > 0) then
            HighlightActiveZone(playField:GetObject(activeZoneObjectsInCurrentZone[math.random(1, objectCount)]), playField:GetTimer(), 3.0);
        end
    end
end

------------------------------------------------------------------------------
-- 
function DefaultCanToolBeApplied(tool, object)
	local toolName = tool:GetName();
	local objName = object:GetName();
    local canToolBeApplied = true;
	
	assert(gameState.zoomZones ~= nil);

   	for zoneName,zone in pairs(gameState.zoomZones) do
	    if (objName == zone.activeObject or objName == zone.navigationObject) then
			canToolBeApplied = false;
   			break;
	    end
    end
	return canToolBeApplied;
end

------------------------------------------------------------------------------
-- 
function DefaultOnWin(showLevelCompletedAnimation)
    if (showLevelCompletedAnimation == nil) then
	    showLevelCompletedAnimation = true;
	end
	if (playField:GetLevelScreen():GetGamePanel():IsHidden() == false) then
		playField:GetLevelScreen():GetGamePanel():HidePanel();
	end

--// close active zoom-zones and panel with tip
assert(gameState.activeZone ~= nil);
while (gameState.activeZone.biggestObject:GetAnimation():GetWidth() == 640.0) do
	CloseActiveZone();
end	
playField:GetLevelScreen():HideTip();
		
	if (showLevelCompletedAnimation == true) then
		local duration = 4.0;
		if (gameState ~= nil and gameState.finalZoomInPos ~= nil) then
			auxZoomPlayField(2.0, gameState.finalZoomInPos);
		end
		auxPlayLevelCompleteAnimation();
		TimerDoString('CompleteLevel();', duration, playField:GetTimer());
	else
		CompleteLevel();
	end
end

------------------------------------------------------------------------------
-- 
function LoadTutorial()
end
