class 'l_04_Gazebo_Z_Andrea' (ZoomZone)

function l_04_Gazebo_Z_Andrea:__init(name, mode, PlayField)
	ZoomZone.__init(self, name, mode, PlayField)
	
	self.playfield:linkScene('area_link_za_mg',   'CursorGlass',    '04_Guitar')
	
	self:entityHide('area_link_za_mg', 'area_apply_za_paper_plane_letter')
	
	---------------------------------------------------------------------------------
	do	
		local apply_guitar_tools_counter = 2

		local function on_apply_za_guitar_tools(e)
		    self:getEntityNamed(e.target):setCursorOverName('CursorGlass')
			
			if (e.toolName) == "03_string" and not HasGameState('take_strings_video_end', 'true') then 
				self:AndreaInter('take_strings') SetGameState('take_strings_video_end', 'true') end 
			if (e.toolName) == "04_pick" and not HasGameState('take_mediator_video_end', 'true')  then	
				self:AndreaInter('take_mediator')  SetGameState('take_mediator_video_end', 'true') end
		    apply_guitar_tools_counter = apply_guitar_tools_counter - 1
		    
		    if apply_guitar_tools_counter <=0 then
			    self:entityShow('area_link_za_mg')
			    self:entityHide(e.target)

		    	self.timer:add(function()
			        if not e.save then
			            startLevel('l_04_Guitar')
			        end
	    	    end, 4)
		    end
		end
		local info = self:registerToolApplyInfo('03_string', 'area_apply_za_guitar_tools', on_apply_za_guitar_tools, 'BBT_04_NEED_GUITAR_TOOLS')
		local info = self:registerToolApplyInfo('04_pick', 'area_apply_za_guitar_tools', on_apply_za_guitar_tools, 'BBT_04_NEED_GUITAR_TOOLS')	    

		info.on_target_click = function ()
			if not (IsToolFound('03_string') and IsToolFound('04_pick')) then
				self:AndreaInter('Dialog_5_Andrea_2_3')		
			elseif  (IsToolFound('03_string') or IsToolFound('04_pick')) then
				self:AndreaInter('Dialog_5_Andrea_2_3')
			end	
		end

	end
	---------------------------------------------------------------------------------
	
	---------------------------------------------------------------------------------
	if IsToolFound('04_fury_hour_glass') and not IsToolFound('04_letter_to_laura') then
		self:open()
		self:checkPlayComics('Andrea_5')
	end	
	---------------------------------------------------------------------------------

	---------------------------------------------------------------------------------
	if not self.playfield:isComicsShown('Andrea_4') and self.playfield:isComicsShown('Andrea_5') and not isSceneVisited('06_Town') then
		self.playfield:getEntityNamed('compositionall_anim'):hide()
   		self.playfield:getEntityNamed('compositionanim'):hide()
		self.playfield:getEntityNamed('compositionanim_idel'):show()
		self.playfield:getEntityNamed('compositionanim_idel'):play()
		
		self.playfield:playMusicAndrea()
	else
		self.playfield:getEntityNamed('compositionall_anim'):play()		
    end	
    ---------------------------------------------------------------------------------

    ---------------------------------------------------------------------------------
	if isMiniGameCompleted('04_Guitar') then
	    self.timer:add(function()
    	        completeGameTask('06')
	        end, 0)
	
        self:entityHide('area_link_za_mg')
        
        if HasGameState('04_Guitar_Return_from_mg') then
	        RemoveGameState('04_Guitar_Return_from_mg')
	    end
    
        if not self.playfield:isComicsShown('Andrea_3') then	    	
	        self:open()
	        self:checkPlayComics('Andrea_3')
	    end
	elseif HasGameState('04_Guitar_Return_from_mg') then
	    RemoveGameState('04_Guitar_Return_from_mg')
	    
        self:open()
	end
	---------------------------------------------------------------------------------

	---------------------------------------------------------------------------------	
	if IsToolFound('06_paper_plane_letter') then
	    self:entityShow('area_apply_za_paper_plane_letter')
	end
	---------------------------------------------------------------------------------
	
	---------------------------------------------------------------------------------
	local function on_apply_za_paper_plane_letter(e)
        self:entityHide(e.target)

        addGameTask('13')
        self.timer:add( function()
				    	    completeGameTask('12')
					    end, 0)
        	        
        if not e.save then
            self:checkPlayComics('Andrea_4')
        elseif not self.playfield:isComicsShown('Andrea_4') then
            self:open()
            self:checkPlayComics('Andrea_4')
        end
	end
	self:registerToolApplyInfo('06_paper_plane_letter', 'area_apply_za_paper_plane_letter', on_apply_za_paper_plane_letter, 'BBT_04_NEED_PAPER_PLANE_LETTER')
	---------------------------------------------------------------------------------
end

function l_04_Gazebo_Z_Andrea:checkPlayComics(comics_name, forced)
    if not forced and self.playfield:isComicsShown(comics_name) then
        return
    end

    local i = string.sub(comics_name, -2)   

    local function on_frame_change(comics, frame_index)
    	if (i == '_1' or i == '_2') and frame_index == 1 then
			self.timer:add( function()	
								self:playSound('04_skull_show')

								local effector = CreateAnimationEffector()
								effector:attachTo(comics:getEntityNamed('andread_dead'))
								effector:setLifeTime(0, 2)
								effector:setKey(_TRACK_O, 0, 0)
								effector:setKey(_TRACK_O, 0.15791829427083, 0.16807965000863)
								effector:setKey(_TRACK_O, 0.39473470052083, 0.03412043826209)
								effector:setKey(_TRACK_O, 0.63460286458333, 0.33438315359313)
								effector:setKey(_TRACK_O, 0.75, 0.33438315359313)
								effector:setKey(_TRACK_O, 0.94734700520833, 0.09583497528728)
								effector:setKey(_TRACK_O, 1.18424479166667, 0.23930873494992)
								effector:setKey(_TRACK_O, 1.5, 0)

								effector:play()

							end, ComicsFrame.DEFAULT_APPEARING_TIME)    
   		end
	end

    local comics = self.playfield:createComics(comics_name, nil, on_frame_change)    
   
    
    comics.on_finish = function()
            if i == '_1' then
                self:close()
                
                self.playfield:requestShowHOTutorial()
            elseif i == '_2' then
    	        addGameTask('06')
            elseif i == '_3' then
                self.playfield:addNote('Note_04_Maze_Path')
            
                
	            self:findTool('04_fury_hour_glass', nil, function ()
                	 self:checkPlayComics('Andrea_5')
                end)

		    elseif i == '3a' then
                self:close()
                
            elseif i == '_4' then
                self.playfield:entityHide('andrea_idle')
                reopenScene('04_Gazebo_HO')         

                self:findTool('04_crowbar_button')

                self:closeAndDisable()
				self.playfield:getEntityNamed('compositionall_anim'):hide()
                self.playfield:getEntityNamed('compositionanim_idel'):play()
              
				self.playfield:playMusicAndrea()

            elseif i == '_5' then -- раннее кадр комикса Андре 3-1 (дает письмо, после получения часов)
         		self:findTool('04_letter_to_laura', nil,
    			function()
    				local achievment = current_scene:getAchievmentsManager():getAchievment('NoSkipMG')
					if achievment and achievment:isCompleted() then
    					current_scene:getInformManager():show_message('achievment_mg')
    				end
    			end) 	

                self.playfield:entityHide('area_zoom_Andrea')
                self.playfield:entityShow('area_click_maze_path_dialogue')
                self:close()

                if not HasGameState('04_Anrea_video_1') then
                		
                		SetGameState('04_Anrea_video_1', 'true')
                		
						self.playfield:getEntityNamed('compositionall_anim'):hide()
						--self.playfield:getEntityNamed('compositionanim_idel'):hide()
						--self.playfield:getEntityNamed('compositionanim'):show()
						--self.playfield:getEntityNamed('compositionanim'):play() 

						local function on_end()
							self.playfield:getEntityNamed('compositionanim'):hide()
							self.playfield:getEntityNamed('compositionanim_idel'):show()
							self.playfield:getEntityNamed('compositionanim_idel'):play()
							
							self.playfield:playMusicAndrea()
						end

						self.playfield:getEntityNamed('compositionanim').onFinish:add( on_end )
                else                	
                end

            end            
        end
    comics:start()
end

function l_04_Gazebo_Z_Andrea:onOpen()
    self.playfield:addNote('Note_04_Andre')

    if not self.playfield:isComicsShown('Andrea_1') then
        self:checkPlayComics('Andrea_1')
    elseif not self.playfield:isComicsShown('Andrea_2') then
        self:checkPlayComics('Andrea_2')
    elseif self.playfield:isComicsShown('Andrea_5') and not isSceneVisited('06_Town') then	    	
	    self:checkPlayComics('Andrea_3a', true)
    end
    self.playfield:getEntityNamed('compositionanim_idel'):stop()
    
    if self.playfield.curent_music_andrea then
		self.playfield:stopMusicAndrea()	
	end
end

function l_04_Gazebo_Z_Andrea:onClose()
   if not self.playfield:isComicsShown('Andrea_4') and self.playfield:isComicsShown('Andrea_5') and  not isSceneVisited('06_Town') then
   		self.playfield:getEntityNamed('compositionall_anim'):hide()
   		self.playfield:getEntityNamed('compositionanim'):hide()
		self.playfield:getEntityNamed('compositionanim_idel'):show()
		self.playfield:getEntityNamed('compositionanim_idel'):play()
		
		self.playfield:playMusicAndrea()
	else
		self.playfield:getEntityNamed('compositionall_anim'):play()	
   end	

end

function l_04_Gazebo_Z_Andrea:AndreaInter(nameOfVideo)
		local function on_end()
		 	    self.AndreaInterVideo:ChangeOpacity(1, 0, 0.2)
		        self.timer:add(function()
		        self:getMouseBlocker():pop()
		        self.AndreaInterVideo:destroy()
	    	    end, 0.2)
	    end
	    	local sound_name = nameOfVideo
	    	if (sound_name == 'take_strings' or sound_name == 'take_mediator') then
	    		sound_name = ''
	    	end

			self.AndreaInterVideo = createVideo({video = nameOfVideo, order = 101, sound = sound_name,  parent = self.main_entity, x = 171, y = 16, isLooped = false, isAlpha = false})
	        self:getMouseBlocker():push()
	        self.AndreaInterVideo.onFinish:add( on_end )
	        self.AndreaInterVideo:play()
end	
--------------------------------------------------------------------------------


--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function l_04_Gazebo_Z_Andrea:CreateLoaderTable()
	local objects =
	{
		{ name = 'za_bg',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 0,  isRespondable = true,  scale = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Scenes/04_Gazebo/Z_Andrea/za_bg.jpg',  width = 683,  height = 384,  },
		{ name = 'za_andrea',  entityType = 'SpriteEntity',  x = 683,  y = 769,  order = 10,  isRespondable = true,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Scenes/04_Gazebo/Z_Andrea/za_andrea.png',  width = 603,  height = 1450,  },
		{ name = 'area_apply_za_guitar_tools',  entityType = 'SpriteEntity',  x = 694,  y = 405,  order = 100,  isRespondable = true,  scaleX = 6,  scaleY = 7.5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'area_link_za_mg',  entityType = 'SpriteEntity',  x = 694,  y = 405,  order = 100,  isRespondable = true,  scaleX = 6,  scaleY = 7.5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
		{ name = 'area_apply_za_paper_plane_letter',  entityType = 'SpriteEntity',  x = 694,  y = 405,  order = 100,  isRespondable = true,  scaleX = 6,  scaleY = 7.5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100,  },
	}
	return objects
end