dofile('data/Scripts/scenes/05_office/anim/05_office_anim_bg.lua')
dofile('data/Scripts/scenes/05_office/anim/05_office_anim_chair.lua')
dofile('data/Scripts/scenes/05_office/anim/05_office_anim_addition.lua')
dofile('data/Scripts/scenes/05_office/anim/05_office_anim_z_window.lua')
dofile('data/Scripts/scenes/05_office/anim/05_office_anim_z_lock.lua')

dofile('data/Scripts/scenes/05_office/comics/05_office_comics_villain.lua')
dofile('data/Scripts/scenes/05_office/comics/05_office_comics_dupin_after_mg.lua')
dofile('data/Scripts/scenes/05_office/comics/05_office_comics_dupin_after_villain.lua')
dofile('data/Scripts/scenes/05_office/comics/05_office_comics_dupin_after_window.lua')
dofile('data/Scripts/scenes/05_office/comics/05_office_comics_dupin_after_parchment.lua')

class 'l_05_office' (LevelEventHandler)

function l_05_office:__init(name)
	LevelEventHandler.__init(self, name)
	
	-- animations ----------------------------------
	self.anim_bg = AnimationBundle(Loader_05_BG_Animations(Object('bg')))
	self.anim_bg:PlayAnimation('bg')
	
	self.anim_addition = AnimationBundle(Loader_05_Addition_Animations(Object('bg')))
	self.anim_addition:GetAnimation('lock' ):SetVisible(true)
	
	self.anim_chair = AnimationBundle(Loader_05_Chair_Animations(Object('bg')))
	self.anim_chair:GetAnimation('chair'):SetVisible(true)
	
	-- main logic ----------------------------------
	
	-- link scenes ---------------------------------
	LinkScene('area_link_04', 'CursorDoorDown',   'l_04_guest_room')
	LinkScene('area_link_mg', 'CursorGlass',      'l_05_Majong')
	
	-- sounds --------------------------------------
	self.sound_fireplace_idle     = MakeSoundEntity('05_idle_fireplace')
	self.sound_rocking_chair_idle = MakeSoundEntity('05_idle_rocking_chair')
	
	self.sound_fireplace_idle:PlaySound(true)
	self.sound_rocking_chair_idle:PlaySound(true)
	
	self:create_comics_villain()
	
	-- zoom zones ----------------------------------
	
	-- ZOOM LOCK
	do
	    CreateSmartZoomZone('lock')
	    ---------------------
	    
	    self.anim_lock = AnimationBundle(Loader_05_Zoom_Lock_Animations(Object('z_lock_bg')))
	    self.anim_lock:GetAnimation('action'):SetVisible(true)
	    self.anim_lock:GetAnimation('action'):SetClippingRect(self.zoom_lock:GetClippingInfo())
	    ---------------------
	    
	    ObjectVisible('area_link_mg', false)
	    ---------------------
	    
	    local function on_apply_ruler(e)
	        ObjectVisible(e.target, false)
	        
	        if e.save then
	            self.zoom_lock:Disable()
	            self.anim_addition:GetAnimation('lock' ):Rewind(true)
	            ObjectVisible('area_link_mg', true)
	        else
	            self.anim_lock:PlayAnimation('action')
	            MakeSoundEntity('05_apply_ruler'):PlaySound(false)
	            
	            self.mouseBlocker:Push()
                self.timer:Add( function()
                                    self.zoom_lock:CloseAndDisable()
                                    
                                    self.timer:Add( function()
                                                        self.mouseBlocker:Pop()
                                                        self.anim_addition:GetAnimation('lock' ):Play()
                                                        MakeSoundEntity('05_open_cover'):PlaySound(false)
                                                        
                                                        ObjectVisible('area_link_mg', true)
                                                    end,
                                                    ZOOM_ZONE_APPEARING_TIME)
                                end,
                                self.anim_lock:GetAnimation('action'):GetDuration())
	        end
	    end
	    ---------------------
	    
	    RegisterToolApplyInfo('05_ruler', 'area_apply_zl_ruler', on_apply_ruler, 'BBT_05_NEED_APPLY_RULER')
	end
	
	-- ZOOM FIREPLACE
	do
	    CreateSmartZoomZone('fireplace')
	    ---------------------
	    
	    if HasActionCompletedState('paperAppeared') then
	        ObjectOpacity('z_fireplace_paper_1', 0)
	    else
	        ObjectOpacity('z_fireplace_paper_2', 0)
	        ObjectVisible('area_find_zf_parchment', false)
	    end
	    ---------------------
	    
	    self.zoom_fireplace.on_open = function()
	        if not self.sound_zoom_fireplace then
	            self.sound_zoom_fireplace = MakeSoundEntity('05_idle_zoom_fireplace')
	        end
	        self.sound_zoom_fireplace:PlaySound(true)
	        
	        if not self.video_fireplace then
	            self.video_fireplace = CreateVideoEntity('data/Video/Scenes/05_office/05_office_z_fireplace.ogg', '', false)
                self.video_fireplace:AttachTo(Object('z_fireplace_bg'))
                self.video_fireplace:SetZ(1)
                self.video_fireplace:SetXY(0, 0)
                self.video_fireplace:SetLoop(true)
                self.video_fireplace:SetHideOnStop(true)
	        end
	        
	        self.video_fireplace:PlayVideo()
	        
	        
	        if not HasActionCompletedState('paperAppeared') then
	            local time = 0.5
	            
	            ObjectOpacity('z_fireplace_paper_1', 0, time)
	            ObjectOpacity('z_fireplace_paper_2', 1, time)
	            
	            self.mouseBlocker:Push()
	            self.timer:Add( function()
	                                self.mouseBlocker:Pop()
	                                ObjectVisible('area_find_zf_parchment', true)
	                                
	                                AddActionCompletedState('paperAppeared')
	                            end,
	                            time)
	        end
	    end
	    ---------------------
	    
	    self.zoom_fireplace.on_close = function()
	        self.timer:Add( function()
	                            self.video_fireplace:StopVideo()
	                            self.sound_zoom_fireplace:StopSound()
	                        end,
	                        ZOOM_ZONE_APPEARING_TIME)
	    end
	    ---------------------
	    
	    local function on_find_parchment(e)
	        self.anim_addition:GetAnimation('paper'):SetVisible(false)
	    end
	    ---------------------
	    
	    RegisterToolFindInfo('05_parchment_01', 'area_find_zf_parchment', on_find_parchment)
	end
	
	-- ZOOM WINDOW
	do
	    CreateSmartZoomZone('window')
	    ---------------------
	    
	    ObjectVisible('area_apply_zw_nut_pliers', false)
	    ObjectVisible('area_find_zw_ruler',      false)
	    ---------------------
	    
	    self.anim_window = AnimationBundle(Loader_05_Zoom_Window_Animations(Object('z_window_bg')))
	    self.anim_window:PlayAnimation('back')
	    self.anim_window:PlayAnimation('front')
	    
	    do
	        local x,y,w,h = self.zoom_window:GetClippingInfo()
	        self.anim_window:GetAnimation('back' ):SetClippingRect(x,y,w,h)
	        self.anim_window:GetAnimation('front'):SetClippingRect(x,y,w,h)
	    end
	    ---------------------
	    
	    self.zoom_window.on_close = function()
	        if IsToolFound('05_piece_of_cloth') and
                not IsToolApplied('04_nut_pliers')
            then
	            self:show_comics_dupin_after_window()
	        end
	    end
	    ---------------------
	    
	    local function on_find_piece_of_cloth(e)
	        ObjectVisible(e.target, false)
	        ObjectVisible('area_apply_zw_nut_pliers', true)
	        ObjectOpacity('z_window_piece_of_cloth', 0)
	    end
	    ---------------------
	    
	    local function on_apply_nut_pliers(e)
	        ObjectVisible(e.target, false)
	        
	        if e.save then
	            ObjectOpacity('z_window_nail', 0)
	            
	            FindTool('05_nail_with_blood')
	            NotebookPushNote('note_05_nail')
	            
	            ObjectVisible('area_find_zw_ruler', true)
	        else
	            local time = 0.5
	            ObjectOpacity('z_window_nail', 0, time)
	            
	            MakeSoundEntity('05_apply_nut_pliers'):PlaySound(false)
	            
	            self.mouseBlocker:Push()
	            self.timer:Add( function()
	                                self.mouseBlocker:Pop()
	                                
	                                FindTool('05_nail_with_blood')
	                                NotebookPushNote('note_05_nail')
	                                
	                                ObjectVisible('area_find_zw_ruler', true)
	                            end,
	                            time)
	        end
	    end
	    ---------------------
	    
	    local function on_find_ruler(e)
	        ObjectVisible(e.target, false)
	        
	    end
	    ---------------------
	    
	    RegisterToolFindInfo('05_piece_of_cloth', 'area_find_zw_piece_of_cloth', on_find_piece_of_cloth, false)
	    RegisterToolApplyInfo('04_nut_pliers', 'area_apply_zw_nut_pliers', on_apply_nut_pliers, 'BBT_05_NEED_APPLY_NUT_PLIERS')
	    RegisterToolFindInfo('05_ruler', 'area_find_zw_ruler', on_find_ruler)
	end
	
	if IsToolFound('05_parchment_01') and HasGameState(TOOL_INFO_SHOWN, '05_parchment_01') then
	    self:create_comics_dupin_after_parchment()
	end
	
	if IsToolFound('05_deciphered_parchment_01') and HasGameState(TOOL_INFO_SHOWN, '05_deciphered_parchment_01') then
	    self:create_comics_dupin_after_mg()
	end
end

function l_05_office:create_comics_villain()
    local comics_name = '05_comics_villain'
    
    local comics_finish = function(save)
        ObjectOpacity('layer_villain', 0)
        
        if save then
            self.anim_addition:GetAnimation('paper'):SetVisible(true)
            self.anim_addition:GetAnimation('paper'):Rewind(true)
            
            self.anim_chair:GetAnimation('chair'):Rewind(true)
            
            self:create_comics_dupin_after_villain()
        else
            self.anim_addition:GetAnimation('paper'):SetVisible(true)
            
            self.mouseBlocker:Push()
            self.timer:Add( function()
                                self.anim_addition:PlayAnimation('paper')
                                self.anim_chair:PlayAnimation('chair')
                                
                                MakeSoundEntity('05_fall_chair'):PlaySound(false)
                                
                                self.timer:Add( function()
                                                    self.mouseBlocker:Pop()
                                                    self:create_comics_dupin_after_villain()
                                                end,
                                                self.anim_addition:GetAnimation('paper'):GetDuration())
                            end,
                            ZOOM_ZONE_APPEARING_TIME/2)
        end
    end
    
    if IsComicsShown(comics_name) then
        comics_finish(true)
    else
        local comics_villain = l_05_office_comics_villain()
		comics_villain:LoadData(play_field)
		
		local c = Comics(comics_name,
		                CreateComicsVideoFrame('Villain',  'C_05_VILLAIN_01', nil, {path='data/Video/Scenes/05_office/C_05_VILLAIN_01.ogg', x=602}, 'layer_comics_villain')
                        )
                        
        c.on_finish = function()
            comics_finish(false)
        end
        
        self.mouseBlocker:Push()
        self.timer:Add( function()
                            self.mouseBlocker:Pop()
                            c:Start()
                        end,
                        0.5)
    end
end

function l_05_office:create_comics_dupin_after_villain()
    local comics_name = '05_comics_dupin_after_villain'
    
    local comics_finish = function(save)
        --NotebookPushNote('note4_05_dupin')
        
        if save then
        else
            self.timer:Add( function()
                                HighlightObject('area_zoom_fireplace', 2, nil, 2)
                            end,
                            ZOOM_ZONE_APPEARING_TIME)
        end
    end
    
    if IsComicsShown(comics_name) then
        comics_finish(true)
    else
        local comics_dupin = l_05_office_comics_dupin_after_villain()
		comics_dupin:LoadData(play_field)
		
		local c = Comics(comics_name,
		                CreateComicsVideoFrame('Dupin',  'C_05_DUPIN_AFTER_VILLAIN_01', nil, {path='data/Video/Scenes/05_office/C_05_DUPIN_AFTER_VILLAIN_01.ogg', isAlpha=true, x=320}, 'layer_comics_dupin_after_villain_bg'),
		                CreateComicsVideoFrame('Dupin',  'C_05_DUPIN_AFTER_VILLAIN_02', nil, {path='data/Video/Scenes/05_office/C_05_DUPIN_AFTER_VILLAIN_02.ogg', isAlpha=true, x=320}, 'layer_comics_dupin_after_villain_bg')
                        )
        
        Object('layer_comics_dupin_after_villain_bg'):GetAnimation():SetColor('0x01FFFFFF')
        
        c:SetInstantlyFrames('C_05_DUPIN_AFTER_VILLAIN_02')
        
        c.on_finish = function()
            comics_finish(false)
        end
        
        c:Start()
    end
end

function l_05_office:show_comics_dupin_after_window()
    local comics_name = '05_comics_dupin_after_window'
    
    if not IsComicsShown('05_comics_dupin_after_window') then
        if not self.comics_dupin_after_window then
            local comics_dupin = l_05_office_comics_dupin_after_window()
            comics_dupin:LoadData(play_field)
            
            local c = Comics(comics_name,
                            CreateComicsVideoFrame('Dupin',  'C_05_DUPIN_AFTER_WINDOW_01', nil, {path='data/Video/Scenes/05_office/C_05_DUPIN_AFTER_WINDOW_01.ogg', isAlpha=true, x=260}, 'layer_comics_dupin_after_window_bg')
                            )
                            
            Object('layer_comics_dupin_after_window_bg'):GetAnimation():SetColor('0x01FFFFFF')
            
            c.on_finish = function()
            end
            
            self.comics_dupin_after_window = c
        end
        
        self.comics_dupin_after_window:Start()
    end
end

function l_05_office:create_comics_dupin_after_parchment()
    local comics_name = '05_comics_dupin_after_parchment'
    
    local comics_finish = function(save)
        NotebookPushNote('note_05_parchment')
    end
    
    if IsComicsShown(comics_name) then
        comics_finish(true)
    else
        local comics_dupin = l_05_office_comics_dupin_after_parchment()
		comics_dupin:LoadData(play_field)
		
		local c = Comics(comics_name,
		                CreateComicsVideoFrame('Dupin',  'C_05_DUPIN_AFTER_PARCHMENT_01', nil, {path='data/Video/Scenes/05_office/C_05_DUPIN_AFTER_PARCHMENT_01.ogg', isAlpha=true, x=360}, 'layer_comics_dupin_after_parchment_bg')
                        )
                        
        Object('layer_comics_dupin_after_parchment_bg'):GetAnimation():SetColor('0x01FFFFFF')
        
        c.on_finish = function()
            comics_finish(false)
        end
        
        c:Start()
    end
end

function l_05_office:create_comics_dupin_after_mg()
    local comics_name = '05_comics_dupin_after_mg'
    
    local comics_finish = function(save)
        NotebookPushNote('note_05_deciphered_parchment')
    end
    
    if IsComicsShown(comics_name) then
        comics_finish(true)
    else
        local comics_dupin = l_05_office_comics_dupin_after_mg()
		comics_dupin:LoadData(play_field)
		
		local c = Comics(comics_name,
		                CreateComicsVideoFrame('Dupin',  'C_05_DUPIN_AFTER_MG_01', nil, {path='data/Video/Scenes/05_office/C_05_DUPIN_AFTER_MG_01.ogg', isAlpha=true, x=320}, 'layer_comics_dupin_after_mg_bg')
                        )
                        
        Object('layer_comics_dupin_after_mg_bg'):GetAnimation():SetColor('0x01FFFFFF')
        
        c.on_finish = function()
            comics_finish(false)
        end
        
        c:Start()
    end
end

function l_05_office:OnUpdate(dt)
end

function l_05_office:OnAllItemsFound()
end

function l_05_office:OnObjectMouseEnter(object)
end

function l_05_office:OnObjectMouseLeave(object)
end

function l_05_office:OnScreenObjectMouseDown(object, key)
end

function l_05_office:OnObjectMouseDownProcessed(is_wrong_click)
end

function l_05_office:OnMouseMove(previous_pos, current_pos)
end

function l_05_office:OnObjectMouseUp(object)
end

function l_05_office:OnObjectFound(object)
end

function l_05_office:OnToolFound(tool)
end

function l_05_office:OnToolWindowClose(toolName)
    if toolName == '05_deciphered_parchment_01' then
        self.mouseBlocker:Push()
        
        self.timer:Add( function()
                            self.mouseBlocker:Pop()
                            self:create_comics_dupin_after_mg()
                        end,
                        1)
    end
    
    if toolName == '05_parchment_01' then
        self.mouseBlocker:Push()
        
        self.timer:Add( function()
                            self.mouseBlocker:Pop()
                            self:create_comics_dupin_after_parchment()
                        end,
                        1)
    end
end

function l_05_office:OnToolApply(tool, object)
end

function l_05_office:OnToolAddToInventory(tool)
end

function l_05_office:OnObjectReachPanel(object)
end

function l_05_office:OnButtonPress(title)
end

function l_05_office:OnSetTool(tool)
end

function l_05_office:OnLoaded()
end

function l_05_office:OnDestroy()
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_05_office:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/Scenes/05_office/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/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_link_04',anim,512,700,100,0,128,17)
	
	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_link_mg',anim,861,482,100,0,1.5,1)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(43,53)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_zoom_fireplace',anim,679.5,428.6,100,0,1.5,1.4)
	
	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,447,333,100,0,30,20)
	
	anim = CreateAnimation('data/Texture/Scenes/05_office/main/layers/layer_villain.png',0,0,189,311,1,0)
	anim:SetHotSpot(94,155)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_villain',anim,494,325,90,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_lock',anim,853,492,100,0,1.5,1)
	
end
-- CLEAR_ON_SAVE_END
