-- Minigame 27_pipes

function OnSceneLoad(screen)
    LoadData(screen)
        
    g_screen = screen
    g_timer = screen:GetTimer()
        
    OBJECTS_AMOUNT = 49
    Pipes = {}
        
    screen:IgnoreOnTestPoint('Background')
    
    for i = 1, OBJECTS_AMOUNT do
	    Pipes[i] = screen:GetObject("Pipe_"..i)
	    Pipes[i].RightAngle = Pipes[i]:GetAngle()
	    Pipes[i].Direction = math.random(0, 3)
	    Pipes[i]:SetAngle(Pipes[i].Direction * M_PI_OVER_2)
	    if Pipes[i]:GetAnimation():GetColorStr() == '0xFFFFFF00' then
	        Pipes[i].Type = 'line'
	        Pipes[i]:GetAnimation():SetColor('0xFFFFFFFF')
	    else
	        Pipes[i].Type = 'not_line'
	    end
	end    
end

function IsLevelComplete()
    for i = 1, OBJECTS_AMOUNT do
        if math.abs(Pipes[i].RightAngle - Pipes[i]:GetAngle()) > 0.05 then
            if Pipes[i].Type == 'line' then
				if math.abs(Pipes[i].RightAngle - Pipes[i]:GetAngle() + M_PI) > 0.05 then
					return false
				end
			else
				return false
			end    
        end
    end

	return true
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function ShowMiniGameHelp()
    CreateDialogMiniGameHelp('MINI_GAME_HELP_PIPES')
end

function GetObjectByName(obj_name)
    for i = 1, OBJECTS_AMOUNT do
        if Pipes[i]:GetName() == obj_name then
            return Pipes[i]
        end
    end
    return nil
end

function OnUpdate(dt)
end

function OnLose()
end

function OnExit()
end

function OnButtonMouseEnter(title)
end

function OnButtonMouseLeave(title)
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnButtonPress(title)
    DefaultMinigameOnButtonPress(title)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName()
    local is_wrong_click = true
    local pipe = GetObjectByName(obj_name)
    
    if pipe then
	    local dir = pipe.Direction
	    dir = dir + 1
	    if dir > 3 then 
	        dir = 0
	    end
	    pipe.Direction = dir
		pipe:SetAngle(dir * M_PI_OVER_2)
		
		PlaySound('27_mg_pipe_rotate')
				
		if IsLevelComplete() then
		    for i = 1, OBJECTS_AMOUNT do
                g_screen:IgnoreOnTestPoint('Pipe_'..i)
            end
            MiniGameCompleted(0.5)
		end
	end

--  is_wrong_click = DefaultOnObjectMouseDown(object)
    return is_wrong_click
end

function OnObjectMouseUp(object)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
end

function OnSkipMiniGame()
    local time = 1.0
    if game_state.minigame_complete then
        return
    end
    
    for i = 1, OBJECTS_AMOUNT do
        local ef = CreateAnimationEffector();
        ef:AttachTo(Pipes[i])
        ef:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
           
        ef:SetKey(_TRACK_A, 0.0, Pipes[i]:GetAngle())
        ef:SetKey(_TRACK_A, time, Pipes[i].RightAngle)
                
        ef:Play();    
    end  
    for i = 1, OBJECTS_AMOUNT do
        g_screen:IgnoreOnTestPoint('Pipe_'..i)
    end
    MiniGameCompleted(time)
end

function OnPhotographing()
	DefaultOnPhotographing()
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function OnMinigameCompleted()
end

function OnLostFocus()
    DefaultMinigameOnLostFocus()
end

function OnGainFocus()
    DefaultMinigameOnGainFocus()
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/27_pipes/Background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Background',anim,512,384,95,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Wrench.png',0,0,114,190,1,0)
	anim:SetHotSpot(57,95)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Wrench',anim,825,235,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_1',anim,706,300,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_line.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFF00')
	screen:CreateObject('Pipe_2',anim,706,236,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_3',anim,706,172,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_4',anim,642,172,100,270,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_5',anim,642,236,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_tee.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_6',anim,578,236,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_7',anim,578,172,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_line.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFF00')
	screen:CreateObject('Pipe_8',anim,514,172,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_9',anim,514,236,100,270,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_10',anim,450,172,100,270,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_11',anim,450,236,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_12',anim,386,236,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_13',anim,386,172,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_14',anim,322,172,100,270,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_line.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFF00')
	screen:CreateObject('Pipe_15',anim,322,236,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_tee.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_16',anim,514,300,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_line.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFF00')
	screen:CreateObject('Pipe_17',anim,450,300,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_18',anim,322,300,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_line.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFF00')
	screen:CreateObject('Pipe_19',anim,386,300,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_20',anim,514,364,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_21',anim,578,364,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_22',anim,578,300,100,270,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_23',anim,642,300,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_24',anim,642,364,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_25',anim,706,364,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_26',anim,706,428,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_tee.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_27',anim,642,428,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_tee.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_28',anim,578,428,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_29',anim,642,492,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_30',anim,706,492,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_31',anim,706,556,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_line.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFF00')
	screen:CreateObject('Pipe_32',anim,642,556,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_line.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFF00')
	screen:CreateObject('Pipe_33',anim,578,556,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_34',anim,578,492,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_tee.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_35',anim,514,428,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_tee.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_36',anim,514,492,100,270,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_37',anim,514,556,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_38',anim,450,428,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_39',anim,450,364,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_line.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFF00')
	screen:CreateObject('Pipe_40',anim,386,364,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_41',anim,322,364,100,270,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_42',anim,322,428,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_43',anim,386,428,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_44',anim,386,492,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_45',anim,450,492,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_46',anim,450,556,100,90,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_line.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFF00')
	screen:CreateObject('Pipe_47',anim,386,556,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_48',anim,322,556,100,180,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/27_pipes/Pipe_corner.png',0,0,66,66,1,0)
	anim:SetHotSpot(33,33)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Pipe_49',anim,322,492,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
