dofile('data/Scripts/scenes/01_dock/anim/01_dock_anim_bg.lua')
dofile('data/Scripts/scenes/01_dock/anim/01_dock_anim_z_hatch.lua')
dofile('data/Scripts/scenes/01_dock/comics/01_dock_comics_dupin.lua')
dofile('data/Scripts/scenes/01_dock/comics/01_dock_comics_villain.lua')

-------------------------------------------------------------------------------
class 'l_01_dock' (LevelEventHandler)
-------------------------------------------------------------------------------

function l_01_dock:__init(name)
	LevelEventHandler.__init(self, name)
	
	-- init ----------------------------------------
    if not IsComicsShown('01_comics_villain') then
        local comics_villain = l_01_dock_comics_villain()
		comics_villain:LoadData(play_field)
		
		ObjectOpacity('comics_villain_bg', 0)
	end
	
	
	-- animations ----------------------------------
	self.anim_bg = AnimationBundle(Loader_01_BG_Animations(Object('bg')))
	self.anim_bg:PlayAnimation('back')
	self.anim_bg:PlayAnimation('bridge')
	self.anim_bg:PlayAnimation('front')
	self.anim_bg:PlayAnimation('boat')
	self.anim_bg:DisableAnimation('fog')
	self.anim_bg:PlayAnimation('fog')
	
	self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_2'):GetElementByName('Layer_1'):SetOpacity(0)
	
	-- link scenes ---------------------------------
	LinkScene('area_link_02',   'CursorDoorUp', 'l_02_legran_gate')
	LinkScene('area_link_boat', 'CursorGlass',  'l_01a_dock_boat')
	
	if IsToolFound('01_puppy_dog') and
	   IsToolFound('MG_03_01_item_01')
    then
        ObjectVisible('area_link_boat', false)
    end
	
	self.ignore_on_highlight['area_link_boat'] = nil
	
	-- main logic ----------------------------------
	ObjectOpacity('layer_man', 0)
	
	self:SetAdditionalHintObject('area_link_boat')
	
	local function on_find_boathook(e)
	    if e.save then
	        ObjectOpacity('layer_boathook', 0)
	    else
	        local time = 0.5
	        ObjectOpacity('layer_boathook', 0, time)
	    end
	end
	-------------------------
	
	self:interactive_help_entry()
	self:create_comics_dupin()
	RegisterToolFindInfo('01_boathook', 'area_find_boathook', on_find_boathook)
	-------------------------
	
	-- zoom zones ----------------------------------
	
	-- zoom hatch
	do
	    CreateSmartZoomZone('hatch')
	    ---------------------
	    
	    self.anim_hatch = AnimationBundle(Loader_01_Zoom_Hatch_Animations(Object('z_hatch_bg')))
	    self.anim_hatch:GetAnimation('action'):SetVisible(true)
	    self.anim_hatch:GetAnimation('action'):SetClippingRect(self.zoom_hatch:GetClippingInfo())
	    ---------------------
	    
	    ObjectVisible('area_find_zh_MG_02_01_item_01', false)
	    ObjectVisible('area_click_zh_open',            false)
	    ---------------------
	    
	    self.zoom_hatch.on_close = function()
	        self.timer:Add( function()
                                if not IsToolFound('MG_02_01_item_01') and InteractiveHelp.IsActive() and not InteractiveHelp.IsTipShown('SPARKLE') then
                                    InteractiveHelp.ShowTip('SPARKLE', 0.75)
                                end
                            end,
                            0)
	    end
	    ---------------------
	    
	    local function on_find_bag_of_millet(e)
            ObjectOpacity('z_hatch_bag_of_millet', 0)
            ObjectVisible('area_click_zh_open', true)
            self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_2'):GetElementByName('Layer_2'):SetOpacity(0)
	    end
	    ---------------------
	    
	    local function on_click_open(e)
	        ObjectVisible(e.target, false)
	        
	        local compos = self.anim_hatch:GetAnimation('action')
	        
	        if e.save then
	            compos:Rewind(true)
	            self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_2'):GetElementByName('Layer_1'):SetOpacity(1)
	            ObjectVisible('area_find_zh_MG_02_01_item_01', true)
	        else
	            local time = compos:GetDuration()
	            compos:Play()
	            self.anim_bg:GetAnimation('boat'):GetElementByName('Layer_2'):GetElementByName('Layer_1'):ChangeOpacity(0, 1, time)
	            MakeSoundEntity('01_open_hatch'):PlaySound(false)
	            
	            self.timer:Add( function()
	                                ObjectVisible('area_find_zh_MG_02_01_item_01', true)
	                            end,
	                            time)
	        end
	    end
	    ---------------------
	    
	    local function on_find_MG_02_01_item_01(e)
	    end
	    ---------------------
	    
	    RegisterToolFindInfo('01_bag_of_millet', 'area_find_zh_bag_of_millet', on_find_bag_of_millet, false)
	    RegisterActiveObject('area_click_zh_open', on_click_open)
	    RegisterToolFindInfo('MG_02_01_item_01', 'area_find_zh_MG_02_01_item_01', on_find_MG_02_01_item_01)
	end
	
	self:check_interactive_help_tips()
end
-------------------------------------------------------------------------------

function l_01_dock:check_interactive_help_tips()
    if IsComicsShown('01_comics_villain') and not IsToolFound('01_boathook') then
        InteractiveHelp.ShowTip('FIND_TOOL')
    end
                
    if InteractiveHelp.IsTipShown('FIND_TOOL') and HasGameState(TOOL_INFO_SHOWN, '01_boathook') then
        InteractiveHelp.ShowTip('ZOOM')
    end
    
    if InteractiveHelp.IsTipShown('ZOOM')
        and (not IsToolFound('MG_02_01_item_01') or HasGameState(TOOL_INFO_SHOWN, 'MG_02_01_item_01'))
        and (not IsToolFound('01_bag_of_millet') or HasGameState(TOOL_INFO_SHOWN, '01_bag_of_millet'))
    then
        InteractiveHelp.ShowTip('SPARKLE')
    end
    
    if self.forcedObject == nil and InteractiveHelp.IsTipShown('SPARKLE') then
        InteractiveHelp.ShowTip('TRANSITION')
    end
end
-------------------------------------------------------------------------------

function l_01_dock:interactive_help_entry()
    if IsRegularPlayMode() then
        self.mouseBlocker:Push()
        
        local function action()
            self.mouseBlocker:Pop()
            
            local function callback(e)
                if e.agreed then
                    InteractiveHelp.ShowTip('COMICS')
                else
                    SetForcedObject('area_comics_dupin')
                end
            end
            
            InteractiveHelp.AskDialog.Show(callback)
        end
        
        if InteractiveHelp.WasAskedForHelp() then
            action()
        else
            self.timer:Add(action, 1)
        end
    else
        SetForcedObject('area_comics_dupin')
    end
end
-------------------------------------------------------------------------------

function l_01_dock:fogOut(time, callback)
    time = time or 0
    
    if time == 0 then
        ObjectOpacity('layer_man', 1)
        self.anim_bg:StopAnimation('fog')
        
        if callback then
            callback()
        end
    else
        local time = 0.5
        ObjectOpacity('layer_man', 1, time)
        
        self.mouseBlocker:Push()
        self.timer:Add( function()
                            local compos   = self.anim_bg:GetAnimation('fog')
                            local duration = compos:GetDuration() / compos:GetSpeed()

                            compos:ChangeOpacity(1, 0, duration)
                            
                            self.timer:Add( function()
                                                self.mouseBlocker:Pop()
                                                self.anim_bg:StopAnimation('fog')
                                                
                                                if callback then
                                                    callback()
                                                end
                                            end,
                                            duration)
                        end,
                        time)
    end
end
-------------------------------------------------------------------------------

function l_01_dock:create_comics_dupin()
    local comics_name = '01_comics_on_first_enter'
    
    local comics_finish = function(save)
        NotebookPushNote('note_01_dupin')
        --[[
        if save then
            self:fogOut(0)
        else
            self:fogOut(0.5)
        end
        --]]
        
        if InteractiveHelp.IsActive() and not InteractiveHelp.IsTipShown('NOTEBOOK') then
            InteractiveHelp.ShowTip('NOTEBOOK')
        else
            local fogOutTime = save and 0 or 0.5
            self:fogOut(fogOutTime, bind(self.create_comics_villain, self))
            --self:create_comics_villain()
        end
    end
    
    if IsComicsShown(comics_name) then
        ObjectVisible('area_comics_dupin', false)
        ObjectOpacity('layer_dupin_idle', 0)
        SetForcedObject(nil)
        
        comics_finish(true)
    else
        local comics_dupin = l_01_dock_comics_dupin()
		comics_dupin:LoadData(play_field)
		
        local c = Comics(comics_name,
                            CreateComicsVideoFrame('Dupin', 'C_01_DUPIN_01', nil, {path='data/Video/Scenes/01_dock/C_01_DUPIN_01.ogg', isAlpha=true, x=680, sx=-1}, 'layer_comics_dupin_bg'),
                            CreateComicsVideoFrame('Dupin', 'C_01_DUPIN_02', nil, {path='data/Video/Scenes/01_dock/C_01_DUPIN_02.ogg', isAlpha=true, x=680},        'layer_comics_dupin_bg')
                            )
        
        Object('layer_comics_dupin_bg'):GetAnimation():SetColor('0x01FFFFFF')
        
        c.on_finish = function()
            comics_finish(false)
        end
        
        local function on_click_comics_dupin(e)
            ObjectVisible(e.target, false)
            ObjectOpacity('layer_dupin_idle', 0, ZOOM_ZONE_APPEARING_TIME)
            
            if not InteractiveHelp.IsActive() then
                SetForcedObject(nil)
            end
            
            c:Start()
        end
        
        RegisterActiveObject('area_comics_dupin', on_click_comics_dupin, nil, 'CursorGlass')
    end
end
-------------------------------------------------------------------------------

function l_01_dock:create_comics_villain()
    local comics_name = '01_comics_villain'
    
    local comics_finish = function(save)
        if save then
            ObjectOpacity('layer_man', 0)
        end
        
        NotebookPushNote('note_01_villain')
    end
    ---------------------
    
    if IsComicsShown(comics_name) then
        ObjectVisible('area_comics_villain', false)
        comics_finish(true)
    else
        SetForcedObject('area_comics_villain')
		
		local c = Comics(comics_name,
		                CreateComicsVideoFrame('Villain', 'C_01_VILLAIN_01', nil, {path='data/Video/Scenes/01_dock/C_01_VILLAIN_01.ogg', x=709}, 'comics_villain_bg')
                        )
                                      
        c.on_finish = function()
            comics_finish(false)
        end
        ---------------------
        
        local function createRavenIdleVideo()
            self.raven_idle_video = CreateVideoEntity('data/Video/Scenes/01_dock/01_raven_idle.ogg', '', true)
            self.raven_idle_video:AttachTo(Object('comics_villain_bg'))
            self.raven_idle_video:SetZ(103)
            self.raven_idle_video:SetXY(-87, -21)
            self.raven_idle_video:SetLoop(true)
            self.raven_idle_video:PlayVideo()
        end
        ---------------------
        
        local function createRavenInVideo()
            self.raven_in_video = CreateVideoEntity('data/Video/Scenes/01_dock/01_raven_in.ogg', '', true)
            self.raven_in_video:AttachTo(Object('comics_villain_bg'))
            self.raven_in_video:SetZ(102)
            self.raven_in_video:SetXY(-250, -175)
            self.raven_in_video:SetLoop(false)
            self.raven_in_video:PlayVideo()
            
            MakeSoundEntity('01_raven_in'):PlaySound(false)
        end
        ---------------------
        
        local function createRavenOutVideo()
            self.raven_out_video = CreateVideoEntity('data/Video/Scenes/01_dock/01_raven_out.ogg', '', true)
            self.raven_out_video:AttachTo(Object('comics_villain_bg'))
            self.raven_out_video:SetZ(1)
            self.raven_out_video:SetXY(-259, 42)
            self.raven_out_video:SetLoop(false)
            self.raven_out_video:PlayVideo()
            
            MakeSoundEntity('01_raven_out'):PlaySound(false)
        end
        ---------------------
        
        local function createVillainBagOutVideo()
            self.villain_bag_out_video = CreateVideoEntity('data/Video/Scenes/01_dock/01_villain_bag_out.ogg', '', true)
            self.villain_bag_out_video:AttachTo(Object('comics_villain_bg'))
            self.villain_bag_out_video:SetZ(105)
            self.villain_bag_out_video:SetScale(SCR_W/640)
            self.villain_bag_out_video:SetXY(0, 0)
            self.villain_bag_out_video:SetLoop(false)
            self.villain_bag_out_video:SetHideOnStop(false)
            self.villain_bag_out_video:PlayVideo()
        end
        ---------------------
        
        c.on_frame_hide = function(e)
            if e.frame.caption == 'C_01_VILLAIN_01' then
                Object('comics_villain_bg'):DestroyEffectors()
                
                self.mouseBlocker:Push()
                --self.timer:Add( function()
                                    -- main logic
                                    do
                                        local speechVideo = e.frame:GetSpeechVideo()
                                        speechVideo:SetVisible(false)
                                        createVillainBagOutVideo()
                                        
                                        self.timer:Add( function()
                                                            ObjectOpacity('comics_villain_bg', 0, ZOOM_ZONE_APPEARING_TIME)
                                                            
                                                            local compos   = self.anim_bg:GetAnimation('fog')
                                                            local duration = compos:GetDuration() / compos:GetSpeed()
                                                            
                                                            self.anim_bg:PlayAnimation('fog')
                                                            compos:ChangeOpacity(1,0, duration)
                                                            ObjectOpacity('layer_man', 0)
                                                            
                                                            self.timer:Add( function()
                                                                                self.mouseBlocker:Pop()
                                                                                self.anim_bg:StopAnimation('fog')
                                                                                
                                                                                InteractiveHelp.ShowTip('FIND_TOOL')
                                                                            end,
                                                                            duration)
                                                        end,
                                                        self.villain_bag_out_video:GetDuration())
                                    end
                                    
                                    -- raven logic
                                    do
                                        if self.raven_in_action then
                                        else
                                            createRavenOutVideo()
                                            
                                            if self.raven_idle_video then
                                                self.raven_idle_video:StopVideo()
                                                self.raven_idle_video = nil
                                            end
                                            
                                            self.timer:Add( function()
                                                                self.raven_out_video = nil
                                                            end,
                                                            self.villain_bag_out_video:GetDuration())
                                        end
                                    end
                           --     end,
                         --       e.showContentTime)
            end
        end
        ---------------------
        
        local function on_click_comics_villain(e)
            ObjectVisible(e.target, false)
            SetForcedObject(nil)
            
            if e.save then
                createRavenIdleVideo()
            else
                self:GetPanelLuaObject().bIgnoreMouseBlockerForNotebook = nil
                createRavenInVideo()
                
                local fade_time = 0.1
                self.raven_in_action = self.timer:Add(  function()
                                                            self.raven_in_action = nil
                                                            createRavenIdleVideo()
                                                            
                                                            self.raven_idle_video:ChangeOpacity(0, 1, fade_time)
                                                            self.raven_in_video:ChangeOpacity(1, 0, fade_time)
                                                            
                                                            self.timer:Add( function()
                                                                                self.raven_in_video:StopVideo()
                                                                                self.raven_in_video = nil
                                                                            end,
                                                                            fade_time)
                                                        end,
                                                        self.raven_in_video:GetDuration() - fade_time)
            end
            
            c:Start()
        end
        
        RegisterActiveObject('area_comics_villain', on_click_comics_villain, nil, 'CursorGlass')
    end
end
-------------------------------------------------------------------------------

function l_01_dock:OnUpdate(dt)
    self.anim_bg:UpdatePeriodicAnimations(dt, {10, 15})
end

function l_01_dock:OnAllItemsFound()
end

function l_01_dock:OnObjectMouseEnter(object)
end

function l_01_dock:OnObjectMouseLeave(object)
end

function l_01_dock:OnScreenObjectMouseDown(object, key)
end

function l_01_dock:OnObjectMouseDownProcessed(is_wrong_click)
end

function l_01_dock:OnMouseMove(previous_pos, current_pos)
end

function l_01_dock:OnObjectMouseUp(object)
end

function l_01_dock:OnObjectFound(object)
end

function l_01_dock:OnToolFound(tool)
end

function l_01_dock:OnToolApply(tool, object)
end

function l_01_dock:OnToolWindowClose(tool)
    if tool == '01_boathook' or
       tool == 'MG_02_01_item_01'
    then
        self.mouseBlocker:Push()
    end
    
    if tool == '01_bag_of_millet' 
        and hidden_zoom_zones.count:GetTop() == 0
    then
        self.mouseBlocker:Push()
        self.bShowInteractiveHelpAfterBagOfMillet = true
    end
end

function l_01_dock:OnToolAddToInventory(tool)
    if tool == '01_boathook' then  
        self.mouseBlocker:Pop()
        InteractiveHelp.ShowTip('ZOOM', 0)
    end
    
    if tool == 'MG_02_01_item_01' then
        self.mouseBlocker:Pop()
        InteractiveHelp.ShowTip('SPARKLE', 0)
    end
    
    if tool == '01_bag_of_millet' and self.bShowInteractiveHelpAfterBagOfMillet then
        self.bShowInteractiveHelpAfterBagOfMillet = nil
        
        self.mouseBlocker:Pop()
        InteractiveHelp.ShowTip('SPARKLE', 0)
    end
end

function l_01_dock:OnObjectReachPanel(object)
end

function l_01_dock:OnButtonPress(title)
end

function l_01_dock:OnSetTool(tool)
end

function l_01_dock:OnLoaded()
end

function l_01_dock:OnDestroy()
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_01_dock:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/Scenes/01_Dock/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/01_dock/main/layers/layer_boathook.png',0,0,200,90,1,0)
	anim:SetHotSpot(100,45)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_boathook',anim,339,498,70,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_link_02',anim,720,263,100,0,1.1,1.4)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,100)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_link_boat',anim,251,401.8,100,0,2.23,1.3)
	
	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_hatch',anim,803,530,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_comics_villain',anim,428,343,100,0,1.5,2)
	
	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_find_boathook',anim,341,499,100,-69,0.8,1.8)
	
	anim = CreateAnimation('data/Texture/Scenes/01_dock/main/layers/layer_man.png',0,0,98,206,1,0)
	anim:SetHotSpot(49,103)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_man',anim,414,358,71,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/01_dock/main/layers/layer_dupin_idle.png',0,0,548,738,1,0)
	anim:SetHotSpot(274,369)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_dupin_idle',anim,713,404,200,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_comics_dupin',anim,803,299,201,10,2.5,4.5)
	
end
-- CLEAR_ON_SAVE_END
