dofile('data/Scripts/Minigames/minigameScreen.lua');
dofile('data/Scripts/Minigames/bugs_logic.lua');
-- miniGameScreen

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

FIELDS_AMOUNT = 6;

function OnSceneLoad()
	play_field = miniGamePlayField;
    LoadData();
	play_field = nil;
    
	miniGameState = {};
   	miniGameState.delayPeriod = 90.0;
   	miniGameState.timeElapsed = 0.0;
   	miniGameState.delayPeriodPassed = false;
   	miniGameState.completed = false;
   	
   	miniGameState.punishmentEnable = true
   	miniGameState.punishmentDuration = 3.0;
	miniGameState.punishmentClickCount = 5;
	miniGameState.prevWrongClickTimeStamp = {-miniGameState.punishmentDuration,
                                             -miniGameState.punishmentDuration,
                                             -miniGameState.punishmentDuration,
                                             -miniGameState.punishmentDuration,
                                             -miniGameState.punishmentDuration,
                                             -miniGameState.punishmentDuration};
    
    miniGamePlayField:IgnoreOnTestPoint('minigame');
      
	FieldsLeft = {  miniGamePlayField:GetObject('krug01'), miniGamePlayField:GetObject('krug02'), miniGamePlayField:GetObject('krug03'),
					miniGamePlayField:GetObject('krug04'), miniGamePlayField:GetObject('krug05'), miniGamePlayField:GetObject('krug06')};
			
	FieldsRight = { miniGamePlayField:GetObject('krug01_2'), miniGamePlayField:GetObject('krug02_2'), miniGamePlayField:GetObject('krug03_2'),
					miniGamePlayField:GetObject('krug04_2'), miniGamePlayField:GetObject('krug05_2'), miniGamePlayField:GetObject('krug06_2')};
	
	VisibleAmount = 0;
	
	for i = 1,FIELDS_AMOUNT do
		FieldsLeft[i]:GetAnimation():SetColor('0x01FFFFFF');
		FieldsRight[i]:GetAnimation():SetColor('0x01FFFFFF');
	end
	
	flowerDust = CreateParticleSystemEntity('data/ParticleSystem/Hint.psi', 'FlowerDustParticle');
	flowerDust:AttachTo(miniGamePlayField:GetObject('minigame'));
	flowerDust:SetZ(1000.0);
	
	do
		local initialGlowIntensity = 0.0;
		miniGamePlayField:GetObject('flower_glow_01'):SetOpacity(initialGlowIntensity);
		miniGamePlayField:GetObject('flower_glow_02'):SetOpacity(initialGlowIntensity);
		miniGamePlayField:GetObject('flower_glow_03'):SetOpacity(initialGlowIntensity);
		miniGamePlayField:GetObject('flower_glow_04'):SetOpacity(initialGlowIntensity);
		miniGamePlayField:GetObject('flower_glow_05'):SetOpacity(initialGlowIntensity);
		miniGamePlayField:GetObject('flower_glow_06'):SetOpacity(initialGlowIntensity);
	end
	
	auxInitText();
	auxTextUpdate();
	
	TimerDoString('OnShowHelp()', 0.5, miniGamePlayField:GetTimer());
	
	InitBugs();
end

function OnSceneFree()
    
end

--// auxiliary functions -----------------------------------------------------
--// 

--Text function
function auxInitText()
	info = CreateTextEntity('TitleFont','');
    info:AttachTo(miniGamePlayField);
	info:SetZ(800.0);
	info:SetScale(1.0);
end

function auxTextUpdate()
	info:SetXY(910.0, 650.0);
	info:SetText(VisibleAmount..'/'..6);
end

function auxObjectDown(obj)
    local name = obj:GetName();
	local found = false;
	for i = 1,FIELDS_AMOUNT do
		if(name == FieldsLeft[i]:GetName() or name == FieldsRight[i]:GetName()) then
		    if name == FieldsLeft[i]:GetName() then
                FieldsLeft[i]:GetAnimation():SetColor('0xFFFFFFFF');
                FieldsRight[i]:GetAnimation():SetColor('0xCCFFFFFF');
            else
                FieldsLeft[i]:GetAnimation():SetColor('0xCCFFFFFF');
                FieldsRight[i]:GetAnimation():SetColor('0xFFFFFFFF');
            end
			miniGamePlayField:IgnoreOnTestPoint(FieldsLeft[i]:GetName());
			miniGamePlayField:IgnoreOnTestPoint(FieldsRight[i]:GetName());
			VisibleAmount = VisibleAmount + 1;
			auxTextUpdate();
			if(VisibleAmount == 6) then 
				miniGameState.completed = true;
				miniGameState.punishmentEnable = false
				auxShowButton(true);
				break;
			end
			found = true;
		end
	end
	if (not found and not GetGameApp():IsFastClickPunishmentEnabled()) then
		PlaySound('L99S02');
	end
end

function DisableFlowerDustGeneration()
	assert(flowerDust ~= nil);
    flowerDust:SetGeneration(false);
end

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

function OnUpdate(dt)
	miniGameState.timeElapsed = miniGameState.timeElapsed + dt;
	auxUpdateProgressBar(miniGameState.timeElapsed > miniGameState.delayPeriod and 1.0 or miniGameState.timeElapsed / miniGameState.delayPeriod);

    if (miniGameState.completed == false and
        miniGameState.delayPeriodPassed == false and miniGameState.timeElapsed > miniGameState.delayPeriod) then
        miniGameState.delayPeriodPassed = true;
        auxShowButton(false);
    end
    
    if not miniGameState.completed then
        UpdateBugs();
    end
end

function OnShowHelp()
	miniGamePlayField:GetLevelScreen():ShowDialog(500, 300, GetTextString('MINIGAME_ZEPPELIN_HELP'), GetTextString('MINIGAME_HELP_DIALOG_TITLE'));
end

function OnObjectMouseDown(object)
	auxObjectDown(object);
	
	if(object:GetName() == 'actZoneBug') then
		RunBug();
	end
	
	-- Punishment for clicking
	if(miniGameState.punishmentEnable and object:GetName() == 'Punish_area') then
	
	    assert(miniGameState ~= nil and 
			   miniGameState.timeElapsed ~= nil and miniGameState.prevWrongClickTimeStamp ~= nil and
               miniGameState.punishmentDuration ~= nil and miniGameState.punishmentClickCount ~= nil);
		assert(table.getn(miniGameState.prevWrongClickTimeStamp) > miniGameState.punishmentClickCount and
			   table.getn(miniGameState.prevWrongClickTimeStamp) < 10);

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

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

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

function OnObjectMouseUp(object)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnMouseMove(previousPos, currentPos)
	 if flowerDust ~= nil then
        flowerDust:SetScrXY(currentPos.x, currentPos.y);
     end
end

function OnObjectMouseEnter(object)
	local objectName = object:GetName();
	
	if miniGameState.active_zone == nil and
	    string.sub(objectName, 1, string.len('act_zone01_flower')) == 'act_zone01_flower' then	--// act_zone01_flowerXX
		assert(flowerDust ~= nil);
        flowerDust:SetGeneration(true);
		TimerDoString('DisableFlowerDustGeneration()', 0.1, miniGamePlayField:GetTimer());
		do
		    local glowName = 'flower_glow_'..string.sub(objectName, string.len('act_zone01_flower') + 1, string.len('act_zone01_flower') + 2);
			local obj = miniGamePlayField:GetObject(glowName);
			obj:SetOpacity(0.0);
			do
			    local af = CreateAnimationEffector();
			    af:AttachTo(obj);
			    af:SetKey(_TRACK_O, 0.0, 0.0);
			    af:SetKey(_TRACK_O, 0.4, 1.0);
			    af:SetKey(_TRACK_O, 3.4, 0.0);
			    af:Play();
			end
		end
    end
end

function OnObjectMouseLeave(object)
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/Minigames/Zeppelin/minigame.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('minigame',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug01.png',0,0,59,57,1,0)
	anim:SetHotSpot(29,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug01',anim,101,422,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug02.png',0,0,64,65,1,0)
	anim:SetHotSpot(32,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug02',anim,198,346,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug03.png',0,0,132,121,1,0)
	anim:SetHotSpot(66,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug03',anim,380,323,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug04.png',0,0,53,57,1,0)
	anim:SetHotSpot(26,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug04',anim,385,440,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug05.png',0,0,56,50,1,0)
	anim:SetHotSpot(28,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug05',anim,208,488,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug06.png',0,0,63,58,1,0)
	anim:SetHotSpot(31,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug06',anim,250,394,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug01.png',0,0,59,57,1,0)
	anim:SetHotSpot(29,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug01_2',anim,627,360,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug02.png',0,0,64,65,1,0)
	anim:SetHotSpot(32,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug02_2',anim,713,290,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug03.png',0,0,132,121,1,0)
	anim:SetHotSpot(66,60)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug03_2',anim,911,262,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug04.png',0,0,53,57,1,0)
	anim:SetHotSpot(26,26)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug04_2',anim,911,382,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug05.png',0,0,56,50,1,0)
	anim:SetHotSpot(28,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug05_2',anim,734,433,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/krug06.png',0,0,63,58,1,0)
	anim:SetHotSpot(31,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('krug06_2',anim,774,342,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/flower_glow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFF80FF')
	play_field:CreateObject('flower_glow_01',anim,14,221,101,0,2,2.5)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/flower_glow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xF0FF80FF')
	play_field:CreateObject('flower_glow_02',anim,23,520,101,0,2.2,2.2)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/flower_glow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xF0FF80FF')
	play_field:CreateObject('flower_glow_03',anim,13,641,101,0,2,2)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/flower_glow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xF0FF80FF')
	play_field:CreateObject('flower_glow_04',anim,980,168,101,0,2.4,2.4)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/flower_glow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xF0FF80FF')
	play_field:CreateObject('flower_glow_05',anim,979,499,101,0,2,2)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/flower_glow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xF0FF80FF')
	play_field:CreateObject('flower_glow_06',anim,995,607,101,0,2,2)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	play_field:CreateObject('act_zone01_flower01',anim,16,218,102,0,0.3,0.5)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	play_field:CreateObject('act_zone01_flower02',anim,27,531,102,0,0.5,0.5)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	play_field:CreateObject('act_zone01_flower03',anim,20,645,102,0,0.3,0.3)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	play_field:CreateObject('act_zone01_flower04',anim,989,172,102,0,0.4,0.6)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	play_field:CreateObject('act_zone01_flower05',anim,989,500,102,0,0.4,0.6)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	play_field:CreateObject('act_zone01_flower06',anim,997,609,102,0,0.4,0.4)
	local anim = CreateAnimation('data/Texture/Minigames/Zeppelin/Layer_Wood01.png',0,0,302,130,1,0)
	anim:SetHotSpot(151,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Layer_Wood01',anim,256,80,500,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/actZoneC.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	play_field:CreateObject('actZoneBug',anim,439,546,400,0,1.4,1.4)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Punish_area',anim,512,384,99,0,128,96)
end

function FreeData()
end
-- CLEAR_ON_SAVE_END
