-- Items to use
-- 06_ball 15_ball

local part1;
local part2;

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/Minigames/18_lock.lua', 'quest', 'data/Scenes/18_Apartment_Main.lua')
    
    AddNoteToNotebook('STAINED_GLASS_HALLWAY');
    
    play_field:GetObject('ball_1'):SetVisible(false)
    play_field:GetObject('ball_2'):SetVisible(false)
	
	part1 = play_field:GetObject('Part1');
	part2 = play_field:GetObject('Part2');
	
	part2:SetXY(part2:GetX() - part1:GetX(), part2:GetY() - part1:GetY())
	part2:SetZ(5);
	part2:AttachTo(part1);
	part1:SetAngle(- M_PI / 60);    -- 3 degree initial slope
	
	for i = 0, 3 do
		local ball;
    	ball= play_field:GetObject('ball_' .. i)
		ball:SetXY(ball:GetX() - part1:GetX(), ball:GetY() - part1:GetY())
		ball:SetZ(2)
		ball:AttachTo(part1)
		if i == 1 or i == 2 then
		    ball:SetVisible(false);
		end
	end
	
	balls_applied = 0
	local tool_names = {'06_ball', '15_ball'};
	for _, name in ipairs(tool_names) do
	    RegisterToolApplyInfo(name, 'area_1', nil, nil,    function (tool_name)
                                                                    if balls_applied == 0 then
                                                                        FirstBallApply();
                                                                    else
                                                                        SecondBallApply();
                                                                    end
                                                                    balls_applied = balls_applied + 1;
                                                                end,                                                                 
                                                                function (tool_name)
                                                                    if balls_applied == 0 then
                                                                        FirstBallApplied();
                                                                    else
                                                                        SecondBallApplied();
                                                                    end
                                                                    balls_applied = balls_applied + 1;
                                                                end);
	end	
                                                                
    if not game_state.ball1_applied then
        ShowMonolog('C_18_MG_HINT_NO_BALLS', 'C_18_MG_HINT_NO_BALLS');
    elseif not game_state.ball2_applied then
        ShowMonolog('C_18_MG_HINT_NO_BALL', 'C_18_MG_HINT_NO_BALL');
    end
end

function FirstBallApply()
    local part1 = play_field:GetObject('Part1');
    PlaySound('18_BallPlaced')
    TimerDoString("PlaySound('18_LockRotate')", 0.05, play_field:GetTimer());
    
    play_field:GetObject('area_1'):SetVisible(false);
    
    do
        local part1 = play_field:GetObject('Part1');
        local eff = CreateAnimationEffector()
        eff:AttachTo( part1 );
        eff:SetMode(_ANIM_FWD);
        
        eff:SetKey(_TRACK_A, 0, part1:GetAngle());
        eff:SetKey(_TRACK_A, 3, part1:GetAngle() - M_PI / 36);
        
        eff:Play();
    end
    do
        local ball = play_field:GetObject('ball_1')
        ball:SetVisible(true)
        
        local eff = CreateAnimationEffector()
        eff:AttachTo(ball)
        eff:SetMode(_ANIM_FWD)
        
        eff:SetKey(_TRACK_X, 0, ball:GetX())
        eff:SetKey(_TRACK_X, 1.4, ball:GetX()-95)
        eff:SetKey(_TRACK_X, 2.6, ball:GetX()-197)
        
        eff:SetKey(_TRACK_Y, 0, ball:GetY())
        eff:SetKey(_TRACK_Y, 1.4, ball:GetY() + 2)
        eff:SetKey(_TRACK_Y, 2.6, ball:GetY() + 70)
        
        eff:SetKey(_TRACK_A, 0, ball:GetAngle())
        eff:SetKey(_TRACK_A, 1.4, ball:GetAngle() - M_PI)
        eff:SetKey(_TRACK_A, 2.6, ball:GetAngle() - M_TWO_PI * 1.7)
                
        eff:Play()
    end
    
    game_state.custom_cursors['area_1'] = 'CursorActive'
    game_state.ball1_applied = true;
    TimerDoString("play_field:GetObject('area_1'):SetVisible(true);", 0.8, play_field:GetTimer())
end

function FirstBallApplied()
    local part1 = play_field:GetObject('Part1');
    part1:SetAngle(-0.1396)
    
    local ball = play_field:GetObject('ball_1')
    ball:SetVisible(true)
    ball:SetXY(-246, 307)
    ball:SetAngle(-10.6814)
    
    game_state.custom_cursors['area_1'] = 'CursorActive'
    game_state.ball1_applied = true;
end

function SecondBallApply()
    local part1 = play_field:GetObject('Part1');
    play_field:GetObject('area_1'):SetVisible(false)
    
    PlaySound('18_BallPlaced')
    TimerDoString("PlaySound('18_LockRotate')", 0.05, play_field:GetTimer());
    
    do
        local eff = CreateAnimationEffector()
        eff:AttachTo( part1 );
        eff:SetMode(_ANIM_FWD);
        
        eff:SetKey(_TRACK_A, 0,     part1:GetAngle());
        eff:SetKey(_TRACK_A, 1,    part1:GetAngle() - M_PI / 36);
        eff:SetKey(_TRACK_A, 3,    part1:GetAngle() - M_PI / 18);
        
        eff:Play();
    end
    
    do
        local ball = play_field:GetObject('ball_2')
        ball:SetVisible(true)

        local eff = CreateAnimationEffector()
        eff:AttachTo( ball );
        eff:SetMode(_ANIM_FWD);
        
        eff:SetKey(_TRACK_X, 0, ball:GetX())
        eff:SetKey(_TRACK_X, 1, ball:GetX()-95)
        eff:SetKey(_TRACK_X, 1.5, ball:GetX()-153)
        
        eff:SetKey(_TRACK_Y, 0, ball:GetY())
        eff:SetKey(_TRACK_Y, 1, ball:GetY() + 2)
        eff:SetKey(_TRACK_Y, 1.5, ball:GetY() + 40)
        
        eff:SetKey(_TRACK_A, 0, ball:GetAngle())
        eff:SetKey(_TRACK_A, 1, ball:GetAngle() - M_PI)
        eff:SetKey(_TRACK_A, 1.5, ball:GetAngle() - M_TWO_PI * 1.1)
        
        eff:Play();
    end
    do
        local ball = play_field:GetObject('ball_3')
        local eff = CreateAnimationEffector()
        eff:AttachTo( ball );
        eff:SetMode(_ANIM_FWD);
        
        eff:SetKey(_TRACK_A, 0, ball:GetAngle());
        eff:SetKey(_TRACK_A, 1.8, ball:GetAngle() - M_TWO_PI / 3 * 2);
        
        eff:SetKey(_TRACK_X, 0, ball:GetX())
        eff:SetKey(_TRACK_X, 0.1, ball:GetX() - 5)
        eff:SetKey(_TRACK_X, 1.8, ball:GetX() - 205)
        
        eff:SetKey(_TRACK_Y, 0, ball:GetY())
        eff:SetKey(_TRACK_Y, 0.1, ball:GetY() - 5)
        eff:SetKey(_TRACK_Y, 1.8, ball:GetY() - 5)
        
        eff:Play();
    end
    
    game_state.ball2_applied = true;    
    TimerDoString("OnMinigameComplete();", 3, play_field:GetTimer());
end

function SecondBallApplied()
    play_field:GetObject('area_1'):SetVisible(false)
    part1:SetAngle(-0.3137)
    
    ball = play_field:GetObject('ball_1')
    ball:SetVisible(true)
    ball:SetXY(-246, 307)
    ball:SetAngle(-10.6814)
    ball = play_field:GetObject('ball_2')
    ball:SetVisible(true)
    ball:SetXY(-202, 277)
    ball:SetAngle(-6.9115)
    ball = play_field:GetObject('ball_3')
    ball:SetXY(-160, 247)
    ball:SetAngle(- 4.1888)
    
    game_state.ball2_applied = true;    
    TimerDoString("OnMinigameComplete();", 1, play_field:GetTimer());
end

function OnMinigameComplete()
	AddGameState('MiniGameCompleted', '18_lock');
	AddGameState('ClosedScene', game_state.scene_path)
	
	StartLevel(game_state.parent_scene);
end

function OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
end

function OnAllItemsFound()
    DefaultOnAllItemsFound()
end

function OnLose()
    DefaultOnLose()
end

function OnExit()
    DefaultOnExit()
end

function OnClose()
    DefaultOnClose()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field
    
    if obj_name == 'area_1' then
        ShowMonolog('C_18_MG_HINT_NO_BALLS', 'C_18_MG_HINT_NO_BALLS');
    end

    if obj_name == 'area_2' then
        ShowMonolog('C_18_MG_HINT_NO_BALL', 'C_18_MG_HINT_NO_BALL');
    end
    
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
    DefaultOnObjectMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
    DefaultOnToolFound(tool)
end

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

function OnObjectReachPanel(object)
    DefaultOnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
    DefaultOnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
    DefaultOnSetTool(tool)
end

function LevelLoaded(level)
end

function OnSceneDestroy(playfield)
    DefaultOnSceneDestroy(playfield)
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/18_lock/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/18_lock/Part1.png',0,0,656,471,1,0)
	anim:SetHotSpot(342,80)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Part1',anim,497,188,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/18_lock/Part2.png',0,0,480,213,1,0)
	anim:SetHotSpot(140,107)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Part2',anim,286,484,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/18_lock/petla1.png',0,0,170,130,1,0)
	anim:SetHotSpot(85,65)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('petla1',anim,801,229,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/18_lock/ball.png',0,0,69,62,1,0)
	anim:SetHotSpot(34,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ball_1',anim,448,425,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/18_lock/ball.png',0,0,69,62,1,0)
	anim:SetHotSpot(34,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ball_2',anim,448,425,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_1',anim,437,431,115,0,18,17)
	local anim = CreateAnimation('data/Texture/Minigames/18_lock/screw.png',0,0,37,37,1,0)
	anim:SetHotSpot(18,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('screw',anim,496,188,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/18_lock/ball2.png',0,0,45,46,1,0)
	anim:SetHotSpot(22,23)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ball_3',anim,542,440,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/18_lock/ball.png',0,0,69,62,1,0)
	anim:SetHotSpot(34,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ball_0',anim,206,531,101,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
