local zone_tube

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/31_Draw_Well_Main.lua', 'quest')

	RegisterQuestObjects('area_acid')

	LinkScene('area_30', 'CursorDoorUp','data/Scenes/30_Road_Fork_Main.lua')
	
	LoadSound('31_Valve_rotating', 'data/Sound/31_Draw_well/Valve_rotating.ogg')
	LoadSound('31_Put_valve', 'data/Sound/31_Draw_well/Put_valve.ogg')
	LoadSound('31_Water_drops', 'data/Sound/31_Draw_well/Water_drops.ogg')

	play_field:GetObject('vent'):Hide()
	play_field:GetObject('kap'):Hide()
	play_field:GetObject('dim'):Hide()
	play_field:GetObject('vent'):GetAnimation():Stop()
	play_field:GetObject('kap'):GetAnimation():Stop()
	play_field:GetObject('dim'):GetAnimation():Stop()
	play_field:GetObject('ventil'):GetAnimation():Stop()
	play_field:GetObject('ventil'):GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_NOLOOP)

	play_field:GetObject('area_acid'):Hide();

	RegisterMonologObject('area_bones', 'C_31_BONES', true)
	RegisterMonologObject('area_valve', 'C_31_VALVE')
	RegisterMonologObject('area_acid', 'C_31_ACID')

	zone_tube = CreateZoomZone('area_tube',
							   'CloseZoneTube',
							   'BG_pit_2_zoom', true,
							   'ventil', false,
							   'area_valve', true)
	play_field:GetObject('area_valve'):SetMaxOpacity(0.001)

	local on_valve_apply = function()
		zone_tube.SetObjectVisible('ventil', true)
		play_field:GetObject('vent'):Show()
		UnregisterActiveObject('area_valve')
		RegisterValveZone()
		MakeSoundEntity('31_Put_valve'):Play(false);
	end
	local on_load_when_valve_applied = function()	
		zone_tube.SetObjectVisible('ventil', true)
		play_field:GetObject('vent'):Show()
		UnregisterActiveObject('area_valve')
		RegisterValveZone()
	end
	RegisterToolApplyInfo('32_valve', 'area_valve', nil, nil, on_valve_apply, on_load_when_valve_applied)
	
	AddRatsComposition()
end

function RegisterValveZone()

    if not HasGameState('TaskComplete', '31_rotate_valve_three_times') then
		if not HasGameState('31_valve_rotates') then
			AddGameState('31_valve_rotates', '0')
		end
        
        local on_valve_click = function(obj_name)
            play_field:GetObject('ventil'):GetAnimation():Play()
            
            valve_nclicks = tonumber(GetGameState('31_valve_rotates'):Get(0))            
            
            if valve_nclicks < 3 then

                --PlaySound("Lock");
                if not play_field.valveSound
                then
                    play_field.valveSound = MakeSoundEntity('31_Valve_rotating');
                end
                play_field.valveSound:StopSound();
                play_field.valveSound:Play(false);

                valve_nclicks = valve_nclicks + 1
                
				SetGameState('31_valve_rotates', tostring(valve_nclicks))
                
                if valve_nclicks == 3 then
                    AddGameState('TaskComplete', '31_rotate_valve_three_times')
                    
                    play_field:GetObject('ventil'):GetAnimation():Play()
                    TimerDoString("CloseTubeZoomOnFinish()", 1., play_field:GetTimer())
                    zone_tube.close_button:Stop()
                    zone_tube.Disable()
                    play_field:GetObject('area_acid'):Show()
                    
                end
            end
        end
        RegisterActiveObject('area_valve', on_valve_click, nil, true)
    else
        zone_tube.Disable()
		play_field:GetObject('area_acid'):Show()
		StartTubeAcid()
    end
    
end

function CloseTubeZoomOnFinish()
	zone_tube.SetVisible(false)
	TimerDoString("play_field:GetObject('vent'):GetAnimation():Stop()", 2., play_field:GetTimer())
	TimerDoString("StartTubeAcid()", 2., play_field:GetTimer())
end

function StartTubeAcid()
	play_field:GetObject('kap'):Show()
	play_field:GetObject('kap'):GetAnimation():Play()
	TimerDoString('StartTubeAcidSmoke()',
				  GetAnimationLength(play_field:GetObject('kap'):GetAnimation()),
				  play_field:GetTimer())

	local on_apply = function()
		FindTool('31_bulb_full')
		UnregisterActiveObject('area_acid')
		game_state.ignore_on_highlight['area_acid'] = true
	end
	local on_load_when_applied = function()
		UnregisterActiveObject('area_acid')
		game_state.ignore_on_highlight['area_acid'] = true
	end
	RegisterToolApplyInfo('29_bulb', 'area_acid', nil, nil, on_apply, on_load_when_applied)
	
	MakeSoundEntity('31_Water_drops'):Play(true);
end

function StartTubeAcidSmoke()
	play_field:GetObject('dim'):Show()
	play_field:GetObject('dim'):GetAnimation():Play()
end

function AddRatsComposition()

    createCompositionRatEating1 = function()
    
        local composition17_Theatre_Entrance_rat = CreateCompositionSequence();
        local compositionDuration = 3.5;
        composition17_Theatre_Entrance_rat:SetDuration(compositionDuration);
    
    
        -- Layer Rat_eating
        local element = BuildRatEatingComposition();
        element:SetName("Rat_eating");
        composition17_Theatre_Entrance_rat:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(composition17_Theatre_Entrance_rat);
        element:SetXY(99.8609466552734, 491.110809326172);
    
        element:SetScaleX(-0.22821681822404);
        element:SetScaleY(0.21152493213464);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 3.5);
        
    
        effector:Play();
        
        
        composition17_Theatre_Entrance_rat:Play();
    
        return composition17_Theatre_Entrance_rat;
    end
    
    createCompositionRatEating2 = function()
    
        local composition17_Theatre_Entrance_rat = CreateCompositionSequence();
        local compositionDuration = 3.5;
        composition17_Theatre_Entrance_rat:SetDuration(compositionDuration);
    
    
        -- Layer Rat_eating_2
        local element = BuildRatEatingComposition();
        element:SetName("Rat_eating");
        composition17_Theatre_Entrance_rat:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(composition17_Theatre_Entrance_rat);
        element:SetXY(733.226806640625, 700.548400878906);
    
        element:SetScaleX(0.37221359784482);
        element:SetScaleY(0.37221359784482);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 3.5);
        
    
        effector:Play();
    
    
        composition17_Theatre_Entrance_rat:Play();
    
        return composition17_Theatre_Entrance_rat;
    end
    
    local compos = createCompositionRatEating1();
    compos:SetCyclic(true);
    compos:SetZ(45);
    compos:SetSpeed(0.8);
    compos:AttachTo(play_field);
    compos:SetCyclic(true);
    
    local compos = createCompositionRatEating2();
    compos:SetCyclic(true);
    compos:SetZ(45);
    compos:SetSpeed(0.8);
    compos:AttachTo(play_field);
    compos:SetCyclic(true);
    compos:PlayAt(1);

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
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/Scenes/31_Draw_Well/Zone_Main/Layers/BG_Drawwell.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Drawwell',anim,512,384,40,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/31_Draw_Well/Zone_Main/Animations/dim.png',0,0,37,207,12,12)
	anim:SetHotSpot(16,103)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('dim',anim,337,560,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/31_Draw_Well/Zone_Main/Animations/kap.png',0,0,19,157,12,12)
	anim:SetHotSpot(0,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('kap',anim,331,454,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/31_Draw_Well/Zone_Main/Animations/vent.png',0,0,42,42,3,6)
	anim:SetHotSpot(21,21)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('vent',anim,269,408,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/31_Draw_Well/Zone_Main/Zoom_Tube/BG_pit_2_zoom.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('BG_pit_2_zoom',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/31_Draw_Well/Zone_Main/Zoom_Tube/ventil.png',0,0,103,41,4,8)
	anim:SetHotSpot(51,20)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ventil',anim,479,318,155,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_tube',anim,269,424,100,0,15,12)
	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_30',anim,592,218,100,0,12,60)
	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_valve',anim,479,342,160,0,20,20)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,1)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_acid',anim,338,446,100,0,6,30)
	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_bones',anim,522,677,100,0,130,20)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
