local zone_table
local zone_book

function add_fibrillation_effector_light(obj)
    local delay = 0.3
    local duration = 5.0

    eff= CreateAnimationEffector()
    eff:AttachTo(obj)
    eff:SetMode(_ANIM_LOOP)

    local dopacity = -0.50

    eff:SetKey(_TRACK_O, 0.0 + delay, 1.0 + dopacity)
    eff:SetKey(_TRACK_O, duration * 1.0/4.0 + delay, 0.75 + dopacity)
    eff:SetKey(_TRACK_O, duration * 2.0/4.0 + delay, 1.0 + dopacity)
    eff:SetKey(_TRACK_O, duration * 3.0/4.0 + delay, 0.59 + dopacity)
    eff:SetKey(_TRACK_O, duration * 4.0/4.0 + delay, 1.0 + dopacity)
    eff:Play()
end

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/08_Library_Main.lua', 'quest')

	RegisterQuestObjects('area_glass', 'glass_mud_1', 'glass_mud_2', 'glass_mud_3', 'area_ho', 'Layer_Rag', 'area_Zoom_Book')
	game_state.ignore_on_highlight['area_Street_Evening'] = true

    local register_table_back_active_object = function()
        UnregisterActiveObject('area_glass')
        RegisterActiveObject('area_glass',
                             function()
                                 ShowMonolog('08_LookBook', 'C_08_LOOK_BOOK')
                             end,
                             nil,
                             true,
                             'CursorGlass')
    end

	play_field:GetObject('area_glass'):SetMaxOpacity(0.001)
	play_field:IgnoreOnTestPoint('glass_mud_4')

    zone_table = CreateZoomZone('area_Zoom_Book',
                                'CloseZoneTable',
								'BG_Zoom_Book', true,
                                'glass_mud_1', true,
                                'glass_mud_2', true,
                                'glass_mud_3', true,
                                'glass_mud_4', true,
								'area_glass', true)
    zone_table.on_open = function()
    end

    if HasGameState('TaskComplete', '09_clock') then
		zone_table.Disable()
	end

    RegisterActiveObject('glass_mud_1', function()
                                 ShowMonolog('08_DIRTY_GLASS', 'C_08_DIRTY_GLASS')
                             end, nil, true, 'CursorGlass')
    RegisterActiveObject('glass_mud_2', function()
                                 ShowMonolog('08_DIRTY_GLASS', 'C_08_DIRTY_GLASS')
                             end, nil, true, 'CursorGlass')
    RegisterActiveObject('glass_mud_3', function()
                                 ShowMonolog('08_DIRTY_GLASS', 'C_08_DIRTY_GLASS')
                             end, nil, true, 'CursorGlass')
    RegisterActiveObject('area_glass', function()
                                 ShowMonolog('08_DIRTY_GLASS', 'C_08_DIRTY_GLASS')
                             end, nil, true, 'CursorGlass')

    RegisterToolFindInfo('08_rag', 'Layer_Rag')

    local on_rag_apply = function(tool_name, object_name)
        if object_name == 'glass_mud_1' then
            zone_table.SetObjectVisible('glass_mud_1', false)
            play_field:GetObject('glass_mud_1'):ChangeOpacity(1., 0., 0.3)
			play_field:IgnoreOnTestPoint('glass_mud_1')
            UnregisterActiveObject('glass_mud_1');
        end
        if object_name == 'glass_mud_2' then
            zone_table.SetObjectVisible('glass_mud_2', false)
			play_field:IgnoreOnTestPoint('glass_mud_2')
            play_field:GetObject('glass_mud_2'):ChangeOpacity(1., 0., 0.3)
        end
        if object_name == 'glass_mud_3' then
            zone_table.SetObjectVisible('glass_mud_3', false)
			play_field:IgnoreOnTestPoint('glass_mud_3')
            play_field:GetObject('glass_mud_3'):ChangeOpacity(1., 0., 0.3)
        end
        --[[
        if object_name == 'glass_mud_4' then
            zone_table.SetObjectVisible('glass_mud_4', false)
            play_field:GetObject('glass_mud_4'):ChangeOpacity(1., 0., 0.3)
        end
        ]]
        if not object_name then
            register_table_back_active_object()
            play_field:GetObject('book_dust_main'):SetVisible(false);
        else
            PlaySound("CleanGlass");
        end
    end
    local on_load_when_rag_applied = function(tool_name, object_name)
        if object_name == 'glass_mud_1' then
            zone_table.SetObjectVisible('glass_mud_1', false)
        end
        if object_name == 'glass_mud_2' then
            zone_table.SetObjectVisible('glass_mud_2', false)
        end
        if object_name == 'glass_mud_3' then
            zone_table.SetObjectVisible('glass_mud_3', false)
        end
        --[[
        if object_name == 'glass_mud_4' then
            zone_table.SetObjectVisible('glass_mud_4', false)
        end
        ]]
        if not object_name then
            zone_table.SetObjectVisible('glass_mud_1', false)
            zone_table.SetObjectVisible('glass_mud_2', false)
            zone_table.SetObjectVisible('glass_mud_3', false)
            --zone_table.SetObjectVisible('glass_mud_4', false)
            register_table_back_active_object()
            play_field:GetObject('book_dust_main'):SetVisible(false);
        end
    end
    RegisterToolApplyInfo('08_rag', {'glass_mud_1', 'glass_mud_2', 'glass_mud_3'}, nil, nil, on_rag_apply, on_load_when_rag_applied)

	if HasGameState('TaskComplete', '09_clock') then
		game_state.ignore_on_highlight['area_Zoom_Book'] = true
		game_state.ignore_on_highlight['area_glass'] = true
	end

    --[[
    zone_book = CreateZoomZone('Zoom_Book',
                               'CloseZoneBook',
							   'Zoom_Book_Zoomed', true)
    ]]

	add_fibrillation_effector_light(play_field:GetObject('dver_svet'))

	LinkScene('area_Street_Evening', 'CursorDoorUp', 'data/Scenes/06_Hotel_Street_Evening_Main.lua', false)
    LinkScene('area_ho', 'CursorGlass', 'data/Scenes/08_Library_HO.lua', true)

	local all_books = {'area_books_1', 'area_books_2', 'area_books_3', 'area_books_4'}
    RegisterMonologObject('area_books_1', 'C_08_BOOKS_1', true, all_books)
    RegisterMonologObject('area_books_2', 'C_08_BOOKS_2', true, all_books)
    RegisterMonologObject('area_books_3', 'C_08_BOOKS_3', true, all_books)
    RegisterMonologObject('area_books_4', 'C_08_BOOKS_4', true, all_books)
    RegisterActiveObject('area_spider_web',
                         function()
							ShowMonolog('C_08_BOOKS_4', SelectRandom('C_08_SPIDER_WEB_1', 'C_08_SPIDER_WEB_2'))
							game_state.ignore_on_highlight['area_spider_web'] = true
						 end,
                         function()
							ShowMonolog('C_08_BOOKS_4', SelectRandom('C_08_SPIDER_WEB_1', 'C_08_SPIDER_WEB_2'))
							game_state.ignore_on_highlight['area_spider_web'] = true
						 end,
						 true,
						 'CursorGlass');

					 
    LoadSound("CleanGlass", "data/Sound/08_Library/Clean_glass.ogg");
    
    AddRatsComposition();
end

function AddRatsComposition()

    createComposition17_Theatre_Entrance_rat = 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(343, 633);
    
        element:SetScaleX(0.26);
        element:SetScaleY(0.26);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 3.5);
    
    
        effector:Play();
    
    
        -- Layer Layer_2
        local element = CreateCompositionElement("data/Texture/Scenes/08_Library/Zone_Main/Animations/mask_08_1.png");
        element:SetName("Layer_2");
        composition17_Theatre_Entrance_rat:AddElement(element);
        element:SetHotSpot(162, 116.5);
        element:SetParentElement(composition17_Theatre_Entrance_rat);
        element:SetXY(392, 587);
    
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 60);
    
    
        effector:Play();
    
    
        -- Layer Layer_3
        local element = CreateCompositionElement("data/Texture/Scenes/08_Library/Zone_Main/Animations/mask_08_2.png");
        element:SetName("Layer_3");
        composition17_Theatre_Entrance_rat:AddElement(element);
        element:SetHotSpot(143, 116.5);
        element:SetParentElement(composition17_Theatre_Entrance_rat);
        element:SetXY(839, 587);
    
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 60);
    
    
        effector:Play();
    
    
        composition17_Theatre_Entrance_rat:Play();
    
        return composition17_Theatre_Entrance_rat;
    end
    
    local createCompositionRatRunning1 = function()
    
        local compositionRatRunning1 = CreateCompositionSequence();
        local compositionDuration = 3.5;
        compositionRatRunning1:SetDuration(compositionDuration);
        
        -- Layer Rat_running
        local element = BuildRatRunningComposition();
        element:SetName("Rat_running");
        compositionRatRunning1:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(compositionRatRunning1);
        element:SetScaleX(-0.23);
        element:SetScaleY(0.23);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0.83333333333333, 5.83333333333333);
    
        effector:SetKey(_TRACK_X, 0.83333333333333, 302);
        effector:SetKey(_TRACK_Y, 0.83333333333333, 571);
        effector:SetKey(_TRACK_X, 2.70833333333333, 866);
        effector:SetKey(_TRACK_Y, 2.70833333333333, 636);
    
        effector:Play();
        
        --compositionRatRunning1:Play();
        
        return compositionRatRunning1;
    end
    
    local createCompositionRatRunning2 = function()
    
        local compositionRatRunning2 = CreateCompositionSequence();
        local compositionDuration = 2.5;
        compositionRatRunning2:SetDuration(compositionDuration);
        
        -- Layer Rat_running
        local element = BuildRatRunningComposition();
        element:SetName("Rat_running_2");
        compositionRatRunning2:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(compositionRatRunning2);
        element:SetScaleX(0.21);
        element:SetScaleY(0.21);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 5);
    
        effector:SetKey(_TRACK_X, 0, 900);
        effector:SetKey(_TRACK_Y, 0, 550);
        effector:SetKey(_TRACK_X, 1.30000813802083, 344.178771972656);
        effector:SetKey(_TRACK_Y, 1.30000813802083, 567.770950317383);
    
        effector:Play();
        
        --compositionRatRunning2:Play();
        
        return compositionRatRunning2;
    end
    
    local composes = {};
    composes.ratRunning1 = createCompositionRatRunning1();
    --composes.ratRunning1:SetCyclic(true);
    composes.ratRunning1:SetZ(43);
    composes.ratRunning1:SetSpeed(0.8);
    composes.ratRunning1:AttachTo(play_field);
    composes.ratRunning1:SetDestroysOnEnd(false);
    composes.ratRunning1:SetEndAction("RatRunningEnded");
    RatRunningEnded(composes.ratRunning1);
    
    composes.ratRunning2 = createCompositionRatRunning2();
    --composes.ratRunning2:SetCyclic(true);
    composes.ratRunning2:SetZ(44);
    composes.ratRunning2:SetSpeed(0.8);
    composes.ratRunning2:AttachTo(play_field);
    composes.ratRunning2:SetDestroysOnEnd(false);
    composes.ratRunning2:SetEndAction("RatRunningEnded");
    RatRunningEnded(composes.ratRunning2);
    
    local compos = createComposition17_Theatre_Entrance_rat();
    compos:SetCyclic(true);
    compos:SetZ(45);
    compos:SetSpeed(0.8);
    compos:AttachTo(play_field);
    compos:SetCyclic(true);

end

function RatRunningEnded(compos)
    compos:PlayAt(math.random(5, 10));
end

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

function OnUpdate(dt)
    DefaultOnUpdate(dt)

	if HasGameState('ToolApplied', '08_rag') and
	   HasGameState('ToolFound', '08_stone')
	then
		game_state.ignore_on_highlight['area_Street_Evening'] = false
	end
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

    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/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_ho',anim,488,405,100,0,16,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_Street_Evening',anim,735,421,100,0,10,20)
	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_Zoom_Book',anim,453,579,100,0,18,7)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Layers/BG_Library.JPG',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Library',anim,512,384,40,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Layers/dver_svet.png',0,0,101,186,1,0)
	anim:SetHotSpot(50,93)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('dver_svet',anim,727,401,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Zoom_Book/BG_Zoom_Book.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Zoom_Book',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Zoom_Book/01.png',0,0,452,269,1,0)
	anim:SetHotSpot(226,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('glass_mud_1',anim,503,365,156,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Zoom_Book/02.png',0,0,452,269,1,0)
	anim:SetHotSpot(226,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('glass_mud_2',anim,503,365,155,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Zoom_Book/03.png',0,0,452,269,1,0)
	anim:SetHotSpot(226,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('glass_mud_3',anim,503,365,154,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Zoom_Book/04.png',0,0,452,269,1,0)
	anim:SetHotSpot(226,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('glass_mud_4',anim,503,365,153,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Tools/Layer_Rag.png',0,0,114,55,1,0)
	anim:SetHotSpot(57,3)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Rag',anim,706,641,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Layers/dust.png',0,0,141,42,1,0)
	anim:SetHotSpot(70,21)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('book_dust_main',anim,442,577,99,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_books_1',anim,237,219,100,0,14,15)
	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_books_2',anim,214,402,100,0,20,24)
	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_books_3',anim,866,213,100,0,16,20)
	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_books_4',anim,869,425,100,0,16,28)
	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_spider_web',anim,91,135,100,0,20,30)
	local anim = CreateAnimation('data/Texture/Scenes/08_Library/Zone_Main/Zoom_Book/area_glass.png',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('area_glass',anim,514,384,151,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
