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

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

function OnSceneLoad()
	play_field = miniGamePlayField;
    LoadData();
	play_field = nil;

    miniGamePlayField:EnableRightMouseButtonProcessing(true);
    auxInitGame();
    
    music_volume = GetMusicVolume(GetCurrentMusicName());
	SetMusicVolume(GetCurrentMusicName(), 0.1);
	
	miniGameState = {};
   	miniGameState.delayPeriod = 90.0;
   	miniGameState.timeElapsed = 0.0;
   	miniGameState.delayPeriodPassed = false;
   	miniGameState.completed = false;
   	
   	TimerDoString('OnShowHelp()', 0.5, miniGamePlayField:GetTimer());
end

function OnSceneFree()
	SetMusicVolume(GetCurrentMusicName(), music_volume);
    FreeData()

	StopSound('L03S03');
end

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

BATTERY_AMOUNT = 8;	--// количество батареек

function auxInitGame()
    miniGamePlayField:IgnoreOnTestPoint('fon');
    miniGamePlayField:IgnoreOnTestPoint('budilnik');
    miniGamePlayField:IgnoreOnTestPoint('lampochka');
    miniGamePlayField:IgnoreOnTestPoint('lampochka_2');
    miniGamePlayField:IgnoreOnTestPoint('lampochka_red');
    miniGamePlayField:IgnoreOnTestPoint('lampochka_2_red');
    miniGamePlayField:IgnoreOnTestPoint('radial_glow');
	miniGamePlayField:IgnoreOnTestPoint('radial_glow2');
    miniGamePlayField:GetObject('lampochka'):SetVisible(false);
    miniGamePlayField:GetObject('lampochka_2'):SetVisible(false);
    --miniGamePlayField:GetObject('lampochka_red'):SetVisible(false);
    --miniGamePlayField:GetObject('lampochka_2_red'):SetVisible(false);
    miniGamePlayField:GetObject('radial_glow'):SetVisible(false);
    miniGamePlayField:GetObject('radial_glow2'):SetVisible(false);
        
    Battery = { miniGamePlayField:GetObject('1v'),      miniGamePlayField:GetObject('1v_2'),    miniGamePlayField:GetObject('1v_3'),
                miniGamePlayField:GetObject('3v'), 	    miniGamePlayField:GetObject('3v_2'),    miniGamePlayField:GetObject('5v'),
				miniGamePlayField:GetObject('5v_2'),    miniGamePlayField:GetObject('9v')};
				
	BatteryRevers = {   miniGamePlayField:GetObject('1v_vniz'), 	miniGamePlayField:GetObject('1v_vniz_2'),   miniGamePlayField:GetObject('1v_vniz_3'),
					    miniGamePlayField:GetObject('3v_vniz'), 	miniGamePlayField:GetObject('3v_vniz_2'),   miniGamePlayField:GetObject('5v_vniz'),
						miniGamePlayField:GetObject('5v_vniz_2'), 	miniGamePlayField:GetObject('9v_vniz')};
	
	Shadow = {};

	NeedPosition = {};
	BoxActive = {};
	BatteryState = {};
	BatteryCost = {1, 1, 1, 3, 3, 5, 5, 9};
	StartPosition = {};
	
	for i = 1, BATTERY_AMOUNT do
		NeedPosition[i] = Battery[i]:GetPosition();
		BatteryRevers[i]:SetVisible(false);
		BoxActive[i] = 0;
		BatteryState[i] = 0;
	end
							
	auxInitStartPosotion();
end

function auxInitStartPosotion()
	local Flags = {};
	for i = 1, BATTERY_AMOUNT do
		Flags[i] = false;
	end
				
	for i = 1, BATTERY_AMOUNT do
		local rand;
		repeat
			 rand = math.random(BATTERY_AMOUNT); 
		until Flags[rand] == false
		
		Flags[rand] = true;
		Shadow[i] = miniGamePlayField:GetObject('Shadow_'..rand);
		StartPosition[i] = BatteryRevers[rand]:GetPosition();
		Battery[i]:SetXY(StartPosition[i].x, StartPosition[i].y);
		Battery[i]:SetZ(1500 - (SCREEN_HEIGHT - StartPosition[i].y));
		Battery[i]:SetScale(1 - (SCREEN_HEIGHT - StartPosition[i].y) / 2000);
	end
end

function auxStartDtag(object)
    Index = auxGetObjectIndex(object:GetName());
    
    if Index ~= nul then
		DragObject = object;
		DragObject:SetZ(1500.0);
		MousePos = GetMousePos();
		dx = MousePos.x - DragObject:GetX();
		dy = MousePos.y - DragObject:GetY();
		
		if (BatteryState[Index] > 0) then
			BoxActive[BatteryState[Index]] = 0;
			BatteryState[Index] = 0;
		else
			Shadow[Index]:ChangeOpacity(1.0, 0.0, 0.2) ;
		end
		
		Battery[Index]:SetScale(1.0);
		auxUpdateSumCost();
	end
end

function auxStopDrag()
    if(DragObject ~= nil) then
        Index = auxGetObjectIndex(DragObject:GetName());
        local InitFlag = false;
        
        for i = 1, BATTERY_AMOUNT do
			if (math.abs(NeedPosition[i].x  - DragObject:GetX()) < 50 and math.abs(NeedPosition[i].y  - DragObject:GetY()) < 50 and BoxActive[i] == 0) then
				PlaySound('L03S01', false);
				DragObject:Move(NeedPosition[i].x - DragObject:GetX(), NeedPosition[i].y - DragObject:GetY(), 0.05);
				BoxActive[i] = BatteryCost[Index];
				BatteryState[Index] = i;
				DragObject:SetZ(105.0);
				InitFlag = true;
			end
        end
        
        if (InitFlag == false) then
			BatteryState[Index] = 0;
			Shadow[Index]:ChangeOpacity(0.0, 1.0, 0.2) ;
			DragObject:Move(StartPosition[Index].x - DragObject:GetX(), StartPosition[Index].y - DragObject:GetY(), 0.1);
			Battery[Index]:SetScale(1 - (SCREEN_HEIGHT - StartPosition[Index].y) / 2000);
			DragObject:SetZ(1500 - (SCREEN_HEIGHT - StartPosition[Index].y));
		end
	
        DragObject = nil;
        dx = nil;
        dy = nil;
        auxUpdateSumCost();
    end
end

function auxDrag(MouseX, MouseY)
    if(DragObject ~= nil) then
        local X = MouseX - DragObject:GetX() - dx;
        local Y = MouseY - DragObject:GetY() - dy;
        DragObject:Move(X, Y, 0.0);
    end
end

function auxGetObjectIndex(name)
    for i = 1,BATTERY_AMOUNT do
        if (Battery[i]:GetName() == name) or (BatteryRevers[i]:GetName() == name) then
            return i;
        end
    end
    
    return nil;
end 

function auxUpdateSumCost()
    if auxIsAllSector(1, 4) then
	    -- Right
        if (BoxActive[1] + BoxActive[2]) == (BoxActive[3] + BoxActive[4]) then
		    if (miniGamePlayField:GetObject('lampochka'):IsVisible() == false) then
			    PlaySound('L03S03');
		    end
		    miniGamePlayField:GetObject('lampochka_red'):SetVisible(false);
		    miniGamePlayField:GetObject('lampochka'):SetVisible(true);
		    miniGamePlayField:GetObject('radial_glow'):SetVisible(true);
		    CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject('radial_glow'));
		-- Wrong
		else
		    miniGamePlayField:GetObject('lampochka_red'):SetVisible(true);
		    miniGamePlayField:GetObject('radial_glow'):SetVisible(true);
		end
				
	else
	    miniGamePlayField:GetObject('lampochka_red'):SetVisible(true);
		miniGamePlayField:GetObject('lampochka'):SetVisible(false);
		miniGamePlayField:GetObject('radial_glow'):SetVisible(false);
	end
	
	if auxIsAllSector(5, 8) then
	    -- Right
        if (BoxActive[5] + BoxActive[6]) == (BoxActive[7] + BoxActive[8]) then
		    if (miniGamePlayField:GetObject('lampochka_2'):IsVisible() == false) then
			    PlaySound('L03S03');
		    end
		    miniGamePlayField:GetObject('lampochka_2_red'):SetVisible(false);
		    miniGamePlayField:GetObject('lampochka_2'):SetVisible(true);
		    miniGamePlayField:GetObject('radial_glow2'):SetVisible(true);
		    CreateTorchFireFibrillationEffector(miniGamePlayField:GetObject('radial_glow2'));
		-- Wrong
		else
		    miniGamePlayField:GetObject('lampochka_2_red'):SetVisible(true);
		    miniGamePlayField:GetObject('radial_glow2'):SetVisible(true);
		end
	else
	    miniGamePlayField:GetObject('lampochka_2_red'):SetVisible(true);
		miniGamePlayField:GetObject('lampochka_2'):SetVisible(false);
		miniGamePlayField:GetObject('radial_glow2'):SetVisible(false);
	end
	
	if IsComplite() then
		miniGameState.completed = true;
		auxShowButton(true);
		miniGamePlayField:EnableRightMouseButtonProcessing(false);
		for i = 1,BATTERY_AMOUNT do
			miniGamePlayField:IgnoreOnTestPoint(Battery[i]:GetName());
			miniGamePlayField:IgnoreOnTestPoint(BatteryRevers[i]:GetName());
		end
	end
end

function auxIsAllSector(x, y)
	for i = x, y do
		if(BoxActive[i] == 0) then
			return false;
		end
	end
	return true;
end

function auxReversBattery(object)
	Index = auxGetObjectIndex(object:GetName());
	
	if(Index ~= nil) then
		PlaySound('L03S02', false);
		BatteryRevers[Index]:SetVisible(true);
		Battery[Index]:SetVisible(false);
		BatteryRevers[Index]:SetXY(Battery[Index]:GetX(), Battery[Index]:GetY());

		if((DragObject ~= nil ) or (BatteryState[Index] > 0)) then
			BatteryRevers[Index]:SetScale(1);
		else
			BatteryRevers[Index]:SetScale(1 - (SCREEN_HEIGHT - StartPosition[Index].y) / 2000);
		end
		
		if(DragObject ~= nil) then
			DragObject = BatteryRevers[Index];
		end
			
		BatteryRevers[Index]:SetZ(Battery[Index]:GetZ());
		Battery[Index], BatteryRevers[Index] = BatteryRevers[Index], Battery[Index];
		BatteryCost[Index] = 0 - BatteryCost[Index];
		if(BoxActive[Index]) then
			BoxActive[BatteryState[Index]] = BatteryCost[Index];
		end
		auxUpdateSumCost();
	end
end

function IsComplite()
	if (miniGamePlayField:GetObject('lampochka'):IsVisible() and miniGamePlayField:GetObject('lampochka_2'):IsVisible()) then
		return true;
	end
	
	return false;
end

function CreateTorchFireFibrillationEffector(object)
	local duration = 3.0;

	fireFibrillationEffector = CreateAnimationEffector();
	fireFibrillationEffector:AttachTo(object);
	fireFibrillationEffector:SetMode(_ANIM_LOOP);

	fireFibrillationEffector:SetKey(_TRACK_O, 0.0, 1.0);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *1.0/4.0, 0.5);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *2.0/4.0, 1.0);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *3.0/4.0, 0.4);
	fireFibrillationEffector:SetKey(_TRACK_O, duration *4.0/4.0, 1.0);

	fireFibrillationEffector:Play();
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
end

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

function OnObjectMouseDown(object)
    auxStartDtag(object);
end
function OnObjectMouseUp(object)
    auxStopDrag();
end

function OnObjectRightMouseDown(object)
	auxReversBattery(object);
end
function OnObjectRightMouseUp(object)
end

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

function OnObjectMouseEnter(object)
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/Clock/fon.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('fon',anim,512,384,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/budilnik.png',0,0,628,687,1,0)
	anim:SetHotSpot(360,363)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('budilnik',anim,593,430,200,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/1v.png',0,0,96,180,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('1v',anim,346,238,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/3v.png',0,0,96,181,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('3v',anim,755,277,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/5v.png',0,0,96,181,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('5v',anim,465,534,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/9v.png',0,0,96,181,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('9v',anim,754,560,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/1v_vniz.png',0,0,96,181,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('1v_vniz',anim,251,664,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/3v_vniz.png',0,0,96,180,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('3v_vniz',anim,143,545,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/5v_vniz.png',0,0,96,180,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('5v_vniz',anim,50,461,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/9v_vniz.png',0,0,96,180,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('9v_vniz',anim,974,616,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/1v.png',0,0,96,180,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('1v_2',anim,464,249,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/1v.png',0,0,96,180,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('1v_3',anim,634,265,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/1v_vniz.png',0,0,96,181,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('1v_vniz_2',anim,151,652,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/1v_vniz.png',0,0,96,181,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('1v_vniz_3',anim,60,204,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/3v.png',0,0,96,181,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('3v_2',anim,342,524,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/3v_vniz.png',0,0,96,180,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('3v_vniz_2',anim,213,469,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/5v.png',0,0,96,181,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('5v_2',anim,636,549,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/5v_vniz.png',0,0,96,180,1,0)
	anim:SetHotSpot(46,89)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('5v_vniz_2',anim,901,672,250,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/lampochka.png',0,0,72,78,1,0)
	anim:SetHotSpot(36,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('lampochka',anim,545,200,201,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/lampochka.png',0,0,72,78,1,0)
	anim:SetHotSpot(36,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('lampochka_2',anim,543,487,201,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFF0000')
	play_field:CreateObject('radial_glow2',anim,546,480,600,0,3,3)
	local anim = CreateAnimation('data/Texture/Scenes/Common/radialGlow.png',0,0,97,99,1,0)
	anim:SetHotSpot(48,48)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFF0000')
	play_field:CreateObject('radial_glow',anim,546,201,600,0,3,3)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/Shadow01.png',0,0,160,88,1,0)
	anim:SetHotSpot(80,80)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Shadow_1',anim,275,761,149,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/Shadow01.png',0,0,160,88,1,0)
	anim:SetHotSpot(80,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Shadow_2',anim,180,711,149,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/Shadow01.png',0,0,160,88,1,0)
	anim:SetHotSpot(80,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Shadow_3',anim,83,245,149,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/Shadow01.png',0,0,160,88,1,0)
	anim:SetHotSpot(80,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Shadow_4',anim,145,611,149,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/Shadow01.png',0,0,160,88,1,0)
	anim:SetHotSpot(80,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Shadow_5',anim,233,525,149,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/Shadow01.png',0,0,160,88,1,0)
	anim:SetHotSpot(80,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Shadow_6',anim,76,518,149,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/Shadow01.png',0,0,160,88,1,0)
	anim:SetHotSpot(80,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Shadow_7',anim,924,737,149,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/Shadow01.png',0,0,160,88,1,0)
	anim:SetHotSpot(80,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('Shadow_8',anim,996,680,149,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/lampochka_red.png',0,0,72,78,1,0)
	anim:SetHotSpot(36,36)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('lampochka_red',anim,545,197,201,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Clock/lampochka_red.png',0,0,72,78,1,0)
	anim:SetHotSpot(36,36)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	play_field:CreateObject('lampochka_2_red',anim,543,484,201,0,1,1)
end

function FreeData()
end
-- CLEAR_ON_SAVE_END
