dofile('Data/Scripts/Common/Comics/ComicsFrame.lua')
-------------------------------------------------------------------------------

class 'Comics' (Node)
-------------------------------------------------------------------------------

Comics.DEFAULT_LOCK_FRAME_TIME = 3.5
Comics.DEFAULT_INSTANT_CHANGE_FRAMES  = true -- Смена кадров фрейма без Fade-эффекта

-- path for textures
Comics.PATH_TEXTURES = 'Data/Textures/Screens/Phone/'
-------------------------------------------------------------------------------

function Comics:__init(playfield, name, comics_type, on_frame_change)
	self.playfield  = playfield
	Node.__init(self, self.playfield:getMainEntity(), name, 0)
	
	do
		local size = Vector2f(WSCR_W, WSCR_H)

		self:getMainEntity():setMaxOrder()
		self:getMainEntity():setAnimationSize(size)
		self:getMainEntity():setHotSpot((size.x - WORK_W) / 2, (size.y - WORK_H) / 2)
		self:getMainEntity():setColor(Colorf(1,1,1,0.001))
		self:hide()
	end
	
	self.shortName      = name
	self.name           = self.playfield:getName()..'_'..name
	self.frames         = {}
	self.frameIndex     = 0
	self.visible		= false
	self.lockFrameTime  = nil
	self.prevPanelMode	= nil

	self.on_frame_change = on_frame_change

	self.on_finish = nil
	
	do
		local framesPath = GetParentFolderPath( self.playfield:getPathLua() )..'Comics/'
		local frameIndex = 1

		repeat
			local frameName    = self.name..'_'..frameIndex
			local stringName   = 'C_'..string.upper(frameName)
			local path         = framesPath..frameName..'_action.lua'
			
			local isFrameExist			
			isFrameExist = resourceExists(path)			

			if isFrameExist then
				local frame = ComicsFrame(frameName, self)				

				if Comics.DEFAULT_INSTANT_CHANGE_FRAMES and frameIndex > 1 then
					frame:setAppearingTime(0)
				end
				
				frame:createContent(framesPath)

				table.insert(self.frames, frame)
				frameIndex = frameIndex + 1
			end
		until not isFrameExist
	end
	
	self:setLockFrameTime( Comics.DEFAULT_LOCK_FRAME_TIME )

	self:getMainEntity().onClickBegin:add(function() self:updateComics() end)
	self:getMainEntity():setHandlingChildEvents(true)

	-- add mask for comics -------------------------------------
	if (comics_type == ComicsTypes.PHONE) then
		self:createPhoneCallBorders()
	end
	------------------------------------------------------------
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function Comics:createPhoneCallBorders()
	local path = self.PATH_TEXTURES

	self.border_phone = CreateSpriteEntity(path .. 'phone_cover_mask.png')
	self.border_phone:attachTo(self.main_entity)
	self.border_phone:setName('comics_border')
	self.border_phone:setXY(513, 386)
	self.border_phone:setRespondable(false)
	self.border_phone:setOrder(10)

	local bttn_close = CreateSpriteEntity(path .. 'fake_bttn_close.png')
	bttn_close:attachTo(self.border_phone)
	bttn_close:setName('comics_fake_bttn_close')
	bttn_close:setXY(452, -205)
	bttn_close:setRespondable(false)
	bttn_close:setOrder(12)

	self.phone_call_layer = CreateSpriteEntity(path .. 'phone_call_layer.png')
	self.phone_call_layer:attachTo(self.main_entity)
	self.phone_call_layer:setName('comics_call_layer')
	self.phone_call_layer:setXY(512, 540)
	self.phone_call_layer:setRespondable(false)
	self.phone_call_layer:setOrder(8)
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function Comics:setLockFrameTime(time)
	self.lockFrameTime = math.max(time, ComicsFrame.DEFAULT_APPEARING_TIME)
end
-------------------------------------------------------------------------------

function Comics:getFrame(index)
	return self.frames[index or self.frameIndex]
end
-------------------------------------------------------------------------------

function Comics:getCurrentFrame()
	return self.frames[self.frameIndex]
end
-------------------------------------------------------------------------------

function Comics:updateComics()
	if self.visible then		

		if self.frameIndex < #self.frames then
			self:showNextFrame()
		else
			self:finish()
		end
	end
end
-------------------------------------------------------------------------------

function Comics:showNextFrame()
	if self.visible then	

		local prevFrame = self:getCurrentFrame()
		self.frameIndex = self.frameIndex + 1
		local nextFrame = self:getCurrentFrame()

		if (self.on_frame_change) then
			self.on_frame_change(self, self.frameIndex)
		end
	
		prevFrame:hide( nextFrame:getAppearingTime() )
		nextFrame:show()

		self.playfield:getPanel():getComicsPanel():showContinueText( self.lockFrameTime )
		self.playfield:getMouseBlocker():push()
		
		self.actionDelayShowNextFrame = self.playfield.timer:add( 	function()
																		self.actionDelayShowNextFrame = nil
																		self.playfield:getMouseBlocker():pop()
																	end, self.lockFrameTime)
	end
end
-------------------------------------------------------------------------------

function Comics:start()
	assert(#self.frames > 0, "cannot start comics with no frames")

	--local debug = tostring(#self.frames)
	--WriteInfo('COUNT OF THE FRAMES: '..debug)
	--self:getMainEntity():setMaxOrder()	
	if not self.visible then
		local panel = self.playfield:getPanel()
		
		self.playfield:hideBlackBar()
		self:show()

		current_scene:getInventory():dropTool()
		current_scene:volumeTurnDownMusic()
		current_scene:volumeTurnDownInternoise()
		
		self.frameIndex = 1
		self.visible    = true

		if (self.on_frame_change) then
			self.on_frame_change(self, self.frameIndex)
		end

		self.prevPanelMode = (panel:getMode() == EPanelMode.NONE) and EPanelMode.QUEST or panel:getMode()
		panel:setMode(EPanelMode.COMICS)
		
		self:getCurrentFrame():show()
		
		self.playfield.active_comics = self
		self.playfield:getPanel():getComicsPanel():showContinueText( self.lockFrameTime )
		self.playfield:getMouseBlocker():push()

		self.actionDelayShowNextFrame = self.playfield.timer:add( 	function()
																		self.actionDelayShowNextFrame = nil
																		self.playfield:getMouseBlocker():pop()
																	end, self.lockFrameTime)
	end
end
-------------------------------------------------------------------------------

function Comics:finish()
	if self.visible then
		current_scene:volumeRestoreMusic()
		current_scene:volumeRestoreInternoise()
		
		self.playfield:getPanel():setMode(self.prevPanelMode)
		self.prevPanelMode = nil

		self.visible = false
		AddUniqueGameState(COMICS_SHOWN, self.name)

		-- hide border
		if (self.phone_call_layer) then
			self.phone_call_layer:hide()			
		end

		if (self.border_phone) then
			self.border_phone:hide()
		end
		-- hide border end
		
		self:getCurrentFrame():hide()
		self.playfield.timer:add(	function()
										self:hide()

										if self.actionDelayShowNextFrame then
											local buffer = self.actionDelayShowNextFrame
											self.actionDelayShowNextFrame.action()
											buffer:kill()
											buffer = nil
										end
									end,
									ComicsFrame.DEFAULT_APPEARING_TIME)

		if self.on_finish then
			self.on_finish(self)
		end
		self.playfield:getPanel():getComicsPanel().showTextField:setOpacity(0)
		self.playfield:getPanel():getComicsPanel().hideTextField:setOpacity(0)
		self.playfield.active_comics = nil
	end
end
-------------------------------------------------------------------------------

function Comics:skip()
	self:finish()

	if (self.on_frame_change) then
		self.on_frame_change(self, -1)
	end
end
-------------------------------------------------------------------------------

function Comics:isVisible()
	return self.visible
end
-------------------------------------------------------------------------------

function Comics:isShown()
	return HasGameState(COMICS_SHOWN, self.name)
end
-------------------------------------------------------------------------------
