dofile('data/Scripts/scenes/12_grannies_room/anim/12_grannies_room_anim_bg.lua')
dofile('data/Scripts/scenes/12_grannies_room/anim/12_grannies_room_anim_z_table.lua')
dofile('data/Scripts/scenes/12_grannies_room/comics/12_grannies_room_comics_dupin.lua')

class 'l_12_grannies_room' (LevelEventHandler)

function l_12_grannies_room:__init(name)
	LevelEventHandler.__init(self, name)
	
	-- animations ----------------------------------
	self.anim_bg = AnimationBundle(Loader_12_BG_Animations(Object('bg')))
	self.anim_bg:PlayAnimation('back')
	self.anim_bg:PlayAnimation('front')
	
	self.anim_bg:GetAnimation('back'):GetElementByName('Layer_1'):SetOpacity(0)
	
	-- link scenes ---------------------------------
	LinkScene('area_link_11', 'CursorDoorDown',   'l_11_corridor')
	
	-- main logic ----------------------------------
	ObjectVisible('area_zoom_safe',  false)
	ObjectVisible('area_zoom_clock', false)
	ObjectVisible('area_zoom_floor', false)
	ObjectOpacity('layer_clock_open', 0)
	-------------------------
	
	local function on_apply_on_dog_veil(e)
	    ObjectVisible('area_zoom_clock', true)
	    ObjectVisible('area_zoom_floor', true)
	    
	    if e.save then
	        if not HasActionCompletedState('DogAnimInBushCompleted') then
	            self.timer:Add( function()
	                                self.zoom_floor:Open()
	                            end,
	                            0)
	        end
	    else
	        self.zoom_floor:Open()
	    end
	end
	-------------------------
	
	RegisterToolApplyOnDogInfo('11_veil', on_apply_on_dog_veil, 'area_zoom_floor')
	
	
	-- zoom zones ----------------------------------
	
	-- ZOOM WINDOW
	do
	    CreateSmartZoomZone('window')
	    ---------------------
	    
	    ObjectOpacity('z_window_contrast', 0)
	    ---------------------
	    
	    ObjectVisible('area_hint_zw_window', false)
	    ---------------------
	    
	    local function on_click_window(e)
	        ObjectVisible(e.target, false)
	        
	        if e.save then
	            ObjectOpacity('z_window_contrast', 1)
	            ObjectVisible('area_hint_zw_window', true)
	            NotebookPushNote('note_12_clock')
	        else
	            local time = 0.5
	            ObjectOpacity('z_window_contrast', 1, time)
	            MakeSoundEntity('12_click_window'):PlaySound(false)
	            
	            AddActionCompletedState('clock_code_found')
	            
	            self.timer:Add( function()
	                                NotebookPushNote('note_12_clock')
	                                ObjectVisible('area_hint_zw_window', true)
	                            end,
	                            time)
	        end
	    end
	    ---------------------
	    
	    RegisterActiveObject('area_click_zw_window', on_click_window)
	    RegisterMonologObject('area_hint_zw_window', 'BBT_12_WINDOW')
	end
	
	-- ZOOM TABLE CREATE
	do
	    CreateSmartZoomZone('table')
	    ---------------------
	    
	    ObjectOpacity('z_table_light',  0)
	    ObjectOpacity('z_table_lamp_1', 0)
	    ObjectOpacity('z_table_lamp_2', 0)
	    ObjectOpacity('z_table_valve',  0)
	    ObjectOpacity('z_table_head_of_ringbird', 0)
	    ---------------------
	    
	    ObjectVisible('area_find_zt_head_of_ringbird', false)
	    ObjectVisible('area_click_zt_note',            false)
	    ObjectVisible('area_click_zt_lamp',            false)
	    ---------------------
	end
	
	-- ZOOM LAMP
	do
	    CreateSmartZoomZone('lamp')
	    ---------------------
	    
	    ObjectVisible('area_click_zl_lamp_valve', false)
	    ---------------------
	    
	    ObjectOpacity('z_lamp_key_1',       0)
	    ObjectOpacity('z_lamp_key_2',       0)
	    ObjectOpacity('z_lamp_key_2_light', 0)
	    ---------------------
	    
	    local function on_apply_lamp_valve(e)
	        ObjectVisible(e.target, false)
	        
	        if e.save then
	            ObjectOpacity('z_table_valve', 1)
	            ObjectOpacity('z_lamp_key_1', 1)
	            ObjectVisible('area_click_zl_lamp_valve', true)
	        else
	            local time = 0.5
	            ObjectOpacity('z_table_valve', 1, time)
	            ObjectOpacity('z_lamp_key_1',  1, time)
	            
	            MakeSoundEntity('12_apply_lamp_valve'):PlaySound(false)
	            
	            self.timer:Add( function()
	                                ObjectVisible('area_click_zl_lamp_valve', true)
	                            end,
	                            time)
	        end
	    end
	    ---------------------
	    
	    local function on_click_lamp_valve(e)
	        ObjectVisible(e.target, false)
	        
	        if e.save then
	            ObjectOpacity('z_table_light', 1)
	            ObjectOpacity('z_table_head_of_ringbird', 1)
	            
	            self.anim_bg:GetAnimation('back'):GetElementByName('Layer_1'):SetOpacity(1)
	            
	            ObjectVisible('area_click_zt_lamp', true)
	            
	            self.zoom_lamp:Disable()
	        else
	            local keyRotateTime = 0.5
	            ObjectOpacity('z_lamp_key_1', 0, keyRotateTime)
	            ObjectOpacity('z_lamp_key_2', 1, keyRotateTime)
	            
	            MakeSoundEntity('12_click_lamp_valve'):PlaySound(false)
	            
	            self.mouseBlocker:Push()
	            self.timer:Add( function()
	                                local lightTime = 0.5
	                                ObjectOpacity('z_lamp_bg_dark',     0, lightTime)
	                                ObjectOpacity('z_lamp_key_2',       0, lightTime)
	                                ObjectOpacity('z_lamp_key_2_light', 1, lightTime)
	                                
	                                ObjectOpacity('z_table_light', 1, lightTime)
	                                ObjectOpacity('z_table_head_of_ringbird', 1, lightTime)
	                                
	                                self.anim_bg:GetAnimation('back'):GetElementByName('Layer_1'):ChangeOpacity(0, 1, lightTime)
	                                
	                                self.timer:Add( function()
	                                                    self.mouseBlocker:Pop()
	                                                    self.zoom_lamp:CloseAndDisable()
	                                                    ObjectVisible('area_click_zt_lamp', true)
	                                                end,
	                                                lightTime)
	                            end,
	                            keyRotateTime)
	        end
	    end
	    ---------------------
	    
	    RegisterToolApplyInfo('12_lamp_valve', 'area_apply_zl_lamp_valve', on_apply_lamp_valve, 'BBT_12_NEED_APPLY_LAMP_VALVE')
	    RegisterActiveObject('area_click_zl_lamp_valve', on_click_lamp_valve)
	end
	
	-- ZOOM TABLE
	do  
	    self.zoom_table.on_open = function()
	        if HasActionCompletedState('NoteFounded') then
	            if not self.anim_head_fall then
	                self.anim_head_fall = AnimationBundle(Loader_12_Zoom_Table_Animations(Object('z_table_bg')))
	            end
	            
	            if not HasActionCompletedState('animFallHeadCompleted') then
	                AddActionCompletedState('animFallHeadCompleted')
	                
	                self.mouseBlocker:Push()
	                self.timer:Add( function()
                                        local animTime = 0.5
                                        ObjectOpacity('z_table_head_of_ringbird', 0, 0.2)
                                        self.anim_head_fall:PlayAnimation('action')
                                        
                                        MakeSoundEntity('12_head_fall'):PlaySound(false)
                                        
                                        self.timer:Add( function()
                                                            self.mouseBlocker:Pop()
                                                            ObjectVisible('area_find_zt_head_of_ringbird', true)
                                                        end,
                                                        animTime)
                                    end,
                                    ZOOM_ZONE_APPEARING_TIME)
	            else
	                self.anim_head_fall:GetAnimation('action'):SetVisible(true)
	                self.anim_head_fall:GetAnimation('action'):Rewind(true)
	                
	                ObjectVisible('area_find_zt_head_of_ringbird', true)
	            end
	        end
	    end
	    ---------------------
	    
	    local function on_click_lamp(e)
	        ObjectVisible(e.target, false)
	        
	        if e.save then
	            ObjectOpacity('z_table_lamp_1', 1)
	            ObjectVisible('area_click_zt_note', true)
	        else
	            
	            
	            local time = 0.5
	            ObjectOpacity('z_table_lamp_1', 1, time)
	            
	            MakeSoundEntity('12_click_lamp'):PlaySound(false)
	            
	            self.timer:Add( function()
	                                ObjectVisible('area_click_zt_note', true)
	                            end,
	                            time)
	        end
	    end
	    ---------------------
	    
	    local function on_click_note(e)
	        ObjectVisible(e.target, false)
	        
	        if e.save then
	            ObjectOpacity('z_table_lamp_2', 1)
	            ObjectOpacity('z_table_lamp_1', 0)
	            
	            NotebookPushNote('note_12_junk_shop')
	            
	            if IsToolFound('12_note') then
	                self:createComicsDupin()
	            else
	                FindTool('12_note')
	            end
	        else
	            local time = 0.2
	            ObjectOpacity('z_table_lamp_2', 1, time)
	            ObjectOpacity('z_table_lamp_1', 0, time)
	            
	            AddActionCompletedState('NoteFounded')
	            
	            NotebookPushFlyNote('note_12_junk_shop',
                                    e.save,
                                    'data/Texture/Notes/note_12_junk_shop_fly.png',
                                    Vector(Object(e.target):GetScrX(), Object(e.target):GetScrY()),
                                    Vector(0.2, 0.2),
                                    nil,
                                    function(e)
                                        self.mouseBlocker:Push()
                                        self.timer:Add( function()
                                                            self.zoom_table:Close()
                                                            
                                                            self.timer:Add( function()
                                                                                self.mouseBlocker:Pop()
                                                                                FindTool('12_note')
                                                                            end,
                                                                            ZOOM_ZONE_APPEARING_TIME)
                                                        end,
                                                        e.flyingTime)
                                    end)
                                    
                local anim = CreateAnimationByTextureName('data/Texture/Notes/note_12_junk_shop_fly_txt.png')
                local obj  = play_field:CreateObject('note_12_junk_shop_fly_txt',anim,0,0,1,0,1,1)
                obj:AttachTo(Object('note_12_junk_shop_fly'))
                play_field:IgnoreOnTestPoint('note_12_junk_shop_fly_txt')
	        end
	    end
	    ---------------------
	    
	    local function on_find_head_of_ringbird(e)
	        ObjectVisible(e.target, false)
	    end
	    ---------------------
	    
	    RegisterActiveObject('area_click_zt_lamp', on_click_lamp)
	    RegisterActiveObject('area_click_zt_note', on_click_note)
	    RegisterToolFindInfo('12_head_of_ringbird', 'area_find_zt_head_of_ringbird', on_find_head_of_ringbird)
	end
	
	-- ZOOM FLOOR
	do
	    CreateSmartZoomZone('floor')
	    ---------------------
	    
	    ObjectOpacity('z_floor_clock_open', 0)
	    ---------------------
	    
	    ObjectVisible('area_find_zf_lamp_valve', false)
	    ObjectVisible('area_click_zf_board',     false)
	    ObjectVisible('area_hint_zf_bottle',     false)
	    ---------------------
	    
	    Object('z_floor_board'):SetClippingRect(self.zoom_floor:GetClippingInfo())
	    ---------------------
	    
	    self.zoom_floor.on_open = function()
	        if not HasActionCompletedState('DogAnimInBushCompleted') then
	            -- create dog anim
	            self.dog_video = CreateVideoEntity('data/Video/Scenes/12_grannies_room/12_dog.ogg', '', false)
                self.dog_video:AttachTo(Object('z_floor_bg'))
                self.dog_video:SetZ(30)
                self.dog_video:SetXY(0, 0)
                self.dog_video:SetLoop(false)
                self.dog_video:SetHideOnStop(false)
                self.dog_video:SetOnFirstFrame()
                
                self:GetPanelLuaObject():SetDogIcoVisible(false)
	            
	            self.mouseBlocker:Push()
	            self.timer:Add( function()
	                                self.dog_video:PlayVideo()
	                                MakeSoundEntity('12_dog'):PlaySound(false)
	                                
	                                self.timer:Add( function()
	                                                    self.mouseBlocker:Pop()
	                                                    AddActionCompletedState('DogAnimInBushCompleted')
	                                                end,
	                                                self.dog_video:GetDuration())
	                            end,
	                            ZOOM_ZONE_APPEARING_TIME)
	        end
	    end
	    ---------------------
	    
	    self.zoom_floor.on_close = function()
	        if self.dog_video then
	            self:GetPanelLuaObject():SetDogIcoVisible(true)
	            
	            self.dog_video:SetVisible(false)
	            self.dog_video = nil
	        end
	    end
	    ---------------------
	end
	
	-- ZOOM CLOCK
	do
	    local zoom, zoneObject = CreateSmartZoomZone('clock')
	    ---------------------
	    
	    self.zoom_clock.on_open = function()
	        if HasActionCompletedState('clock_code_found') and
	            IsToolApplied('10_weight_1') and
	            IsToolApplied('10_weight_2') and
	            not HasActionCompletedState('mmg_started')
            then
                zoneObject:startMMG()
	        end
	    end
	    ---------------------
	end
	
	-- ZOOM FLOOR (LOGIC)
	do
	    local function on_click_board(e)
	        ObjectVisible(e.target, false)
	        
	        local x = 138
	        local y = 233
	        local a = -12 * M_PI/180
	        
	        if e.save then
	            local object = Object('z_floor_board')
	            object:SetX(x)
	            object:SetY(y)
	            object:SetAngle(a)
	            
	            ObjectOpacity('layer_board', 0)
	            ObjectVisible('area_find_zf_lamp_valve', true)
	        else
	            local time   = 0.5
	            local object = Object('z_floor_board')
	            
	            do
                    local eff = CreateAnimationEffector()
                    eff:AttachTo(object)
                    eff:SetDestroysOnEnd(true)
                    eff:SetMode(HGEANIM_FWD + HGEANIM_NOLOOP)
                    eff:SetKey(_TRACK_X, 0, object:GetX())
                    eff:SetKey(_TRACK_Y, 0, object:GetY())
                    eff:SetKey(_TRACK_A, 0, object:GetAngle())
                    
                    eff:SetKey(_TRACK_X, time, x)
                    eff:SetKey(_TRACK_Y, time, y)
                    eff:SetKey(_TRACK_A, time, a)
                    eff:Play()
                end
                
                ObjectOpacity('layer_board', 0, time)
                MakeSoundEntity('12_click_board'):PlaySound(false)
	            
	            self.timer:Add( function()
	                                ObjectVisible('area_find_zf_lamp_valve', true)
	                            end,
	                            time)
	        end
	    end
	    ---------------------
	    
	    local function on_find_lamp_valve(e)
	        ObjectVisible(e.target, false)
	        ObjectOpacity('z_floor_lamp_valve', 0)
	        ObjectVisible('area_hint_zf_bottle', true)
	    end
	    ---------------------
	    
	    RegisterActiveObject('area_click_zf_board', on_click_board)
	    RegisterToolFindInfo('12_lamp_valve', 'area_find_zf_lamp_valve', on_find_lamp_valve, false)
	    RegisterMonologObject('area_hint_zf_scratch', 'BBT_12_SCRATCH')
	    RegisterMonologObject('area_hint_zf_bottle', 'BBT_12_BOTTLE')
	end
	
	-- ZOOM SAFE
	do
	    CreateSmartZoomZone('safe')
	    ---------------------
	    
	    RegisterMonologObject('area_hint_zs_grid', 'BBT_12_GRID')
	end
end

function l_12_grannies_room:createComicsDupin()
    local comics_name = '12_grannies_room_dupin'
    
    local comics_finish = function(save)
        if save then
        else
            self.mouseBlocker:Push()
            self.timer:Add( function()
                                self.mouseBlocker:Pop()
                                self.zoom_table:Open()
                            end,
                            ZOOM_ZONE_APPEARING_TIME)
        end
    end
    
    if IsComicsShown(comics_name) then
        comics_finish(true)
    else
        local comics_dupin = l_12_grannies_room_comics_dupin()
		comics_dupin:LoadData(play_field)
		
        local c = Comics(comics_name,
                            CreateComicsVideoFrame('Dupin',  'D_12_DUPIN_01', nil, {path='data/Video/Scenes/12_grannies_room/D_12_DUPIN_01.ogg', isAlpha=true, x=750}, 'layer_comics_dupin_bg')
                            )
                                      
        c.on_finish = function()
            comics_finish(false)
        end
        
        Object('layer_comics_dupin_bg'):GetAnimation():SetColor('0x01FFFFFF')
        c:Start()
    end
end

function l_12_grannies_room:OnUpdate(dt)
end

function l_12_grannies_room:OnAllItemsFound()
end

function l_12_grannies_room:OnObjectMouseEnter(object)
end

function l_12_grannies_room:OnObjectMouseLeave(object)
end

function l_12_grannies_room:OnScreenObjectMouseDown(object, key)
end

function l_12_grannies_room:OnObjectMouseDownProcessed(is_wrong_click)
end

function l_12_grannies_room:OnMouseMove(previous_pos, current_pos)
end

function l_12_grannies_room:OnObjectMouseUp(object)
end

function l_12_grannies_room:OnObjectFound(object)
end

function l_12_grannies_room:OnToolFound(tool)
end

function l_12_grannies_room:OnToolApply(tool, object)
end

function l_12_grannies_room:OnToolWindowClose(tool)
    if tool == '12_note' then
        self.mouseBlocker:Push()
    end
end

function l_12_grannies_room:OnToolAddToInventory(tool)
    if tool == '12_note' then
        self.mouseBlocker:Pop()
        self:createComicsDupin()
    end
end

function l_12_grannies_room:OnObjectReachPanel(object)
end

function l_12_grannies_room:OnButtonPress(title)
end

function l_12_grannies_room:OnSetTool(tool)
end

function l_12_grannies_room:OnLoaded()
end

function l_12_grannies_room:OnDestroy()
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_12_grannies_room:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/layers/bg.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bg',anim,512,384,50,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/layers/layer_clock_close.png',0,0,114,292,1,0)
	anim:SetHotSpot(57,146)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_clock_close',anim,661,370,60,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/layers/layer_clock_open.png',0,0,122,294,1,0)
	anim:SetHotSpot(61,147)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_clock_open',anim,626,368,61,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_link_11',anim,512,700,100,0,128,17)
	
	anim = CreateAnimation('data/Texture/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_zoom_window',anim,56,252,100,0,10,15)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_zoom_table',anim,153,373,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_zoom_floor',anim,672,482,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_zoom_safe',anim,664,311,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_zoom_clock',anim,663,276,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/layers/layer_grid.jpg',0,0,66,48,1,0)
	anim:SetHotSpot(33,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_grid',anim,665,317,52,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/layers/layer_board.png',0,0,44,16,1,0)
	anim:SetHotSpot(22,8)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_board',anim,659,501,59,0,1,1)
	
end
-- CLEAR_ON_SAVE_END
