class 'ComicsFrame'  (Node)
-------------------------------------------------------------------------------

ComicsFrame.DEFAULT_APPEARING_TIME = 0.25	-- Время проявления фрейма по умолчанию.
-------------------------------------------------------------------------------

function ComicsFrame:__init(name, comics)
	Node.__init(self, comics:getMainEntity(), name, 0)

	self.name	= name
	self.comics	= comics
	self.timer  = comics.playfield.timer

	self.action	= nil
	self.idle	= nil

	self.textKey		= nil
	self.charName		= nil
	self.appearingTime	= nil
	self.textHeight     = nil
	self.oldTextHeight  = nil

	self.auto_complete_frame = nil
end
-------------------------------------------------------------------------------

function ComicsFrame:createContent(framesPath)
	local createNode = function(type, bSkipExistingCheck)
		local name = self.name..'_'..type
		local path = framesPath..name..'.lua'
		local node = nil

		if resourceExists(path) then
			dofile(path)

			local className = 'l_'..name
			node = Helpers.CallFunctionByName(className, className, self)

		elseif bSkipExistingCheck then
			local className = 'l_'..name
			node = Node(self.comics:getMainEntity(), name, 1)
			node.CreateLoaderTable = function() return {} end
		end

		return node
	end

	do
		self.action = createNode('action', true)
		self.idle   = createNode('idle')

		assert(self.action and self.action.CreateLoaderTable)

		self.textKey       = self.action.textKey		or 'C_'..string.upper(self.name)
		self.charName      = self.action.characterName	or 'defualtChar'
		self.appearingTime = self.action.appearingTime	or self.appearingTime or ComicsFrame.DEFAULT_APPEARING_TIME
		self.textHeight    = self.action.textHeight
	end
end
-------------------------------------------------------------------------------

function ComicsFrame:setDuration(duration)
	self.duration = duration
end
-------------------------------------------------------------------------------

function ComicsFrame:show(onLoadedCallback)
	if self.action and self.action.CreateLoaderTable then
		self.table_resource_action = self.action:CreateLoaderTable()
		LoadScene(self.action:getMainEntity(), self.table_resource_action)
		
		self.action:hide()
		self.action:getMainEntity():setActive(false)
		
		local function onActionLoaded()
            if self.load_action_connector then
                self.load_action_connector:remove()
                self.load_action_connector = nil
            end

            if (onLoadedCallback) then
        		onLoadedCallback();
        	end
                    
		    self.max_duration_entity = self:getMaxDurationEntity(self.table_resource_action, self.action)
		
		    if self.comics.on_frame_show then
                self.comics.on_frame_show(self.comics.frameIndex)
			end

            self:getMainEntity():show()
            self:getMainEntity():ChangeOpacity(0, 1, self.appearingTime)

            self.action:show()
            self.action:getMainEntity():setActive(true)

            self:playSound()
            self:playAnims(self.table_resource_action, self.action)

            self.comics.playfield:getPanel():getComicsPanel():setText(self.textKey, ComicsFrame.DEFAULT_APPEARING_TIME)

            if self.textHeight then
                local textField = self.comics.playfield:getPanel():getComicsPanel().showTextField
                self.oldTextHeight = textField:getHeight()
                textField:setHeight(self.textHeight)
            end
            
            if self.idle and self.idle.CreateLoaderTable then
                local function onIdleLoaded()
                    if self.load_idle_connector then
                        self.load_idle_connector:remove()
                        self.load_idle_connector = nil
                    end

                    self.idle:show()
                    self:playAnims(self.table_resource_idle, self.idle)
                    
                    self.action:hide()
                    self.action:getMainEntity():destroyChilds()

                    if self.comics.on_idle_frame_show then
                        self.comics.on_idle_frame_show(self.comics.frameIndex)
                    end
                end

                local function onActionCompleted()
                    self.idle_start_signal:remove()
                    self.idle_start_signal = nil

                    self.table_resource_idle = self.idle:CreateLoaderTable()
                    LoadScene(self.idle:getMainEntity(), self.table_resource_idle)
                    
                    self.idle:hide()
                    
                    local notification = getLastResourceLoadedNotification()
                    if notification then
                        self.load_idle_connector = notification:add( onIdleLoaded )
                    else
                        onIdleLoaded()
                    end
                end

                self.idle_start_signal = self.max_duration_entity.onFinish:add( onActionCompleted )
            elseif self.auto_complete_frame then
                self.auto_complete_signal = self.max_duration_entity.onFinish:add(	function()
                                                                                        self.action:hide()
                                                                                        self.action:getMainEntity():destroyChilds()
                                                                                        self.comics:updateComics()
                                                                                    end)
            end
		end
		
		local notification = getLastResourceLoadedNotification()
        if notification then
            self.load_action_connector = notification:add( onActionLoaded )
        else
            onActionLoaded()
        end
	end
end
-------------------------------------------------------------------------------

function ComicsFrame:hide(time)
	time = time or ComicsFrame.DEFAULT_APPEARING_TIME
	local eff = self:getMainEntity():ChangeOpacity(nil, 0, time)

	eff.onFinish:add(	function()
							self:getMainEntity():hide()

							if self.action then
								self.action:hide()
								self.action:getMainEntity():destroyChilds()
							end
							if self.idle then
								self.idle:hide()
								self.idle:getMainEntity():destroyChilds()
							end
							if self.idle_start_signal then
								self.idle_start_signal:remove()
								self.idle_start_signal = nil
							end
							if self.load_action_connector then
                                self.load_action_connector:remove()
                                self.load_action_connector = nil
                            end
							if self.load_idle_connector then
								self.load_idle_connector:remove()
								self.load_idle_connector = nil
							end
							if self.auto_complete_signal then
								self.auto_complete_signal:remove()
								self.auto_complete_signal = nil
							end
							self:stopSound()
						end)

	self.timer:add(	function()
						if self.oldTextHeight then
							local textField = self.comics.playfield:getPanel():getComicsPanel().hideTextField
							textField:setHeight(self.oldTextHeight)
						end
					end, ComicsFrame.DEFAULT_APPEARING_TIME)
end
-------------------------------------------------------------------------------

function ComicsFrame:setAppearingTime(time)
	self.appearingTime = time
end
-------------------------------------------------------------------------------

function ComicsFrame:getAppearingTime()
	return self.appearingTime
end
-------------------------------------------------------------------------------

function ComicsFrame:getMaxDuration(loader_table, parent)
	local entity = self:getMaxDurationEntity()
	return entity and entity:getDuration() or 0
end
-------------------------------------------------------------------------------

function ComicsFrame:getMaxDurationEntity(loader_table, parent)
	local entityMax   = nil;
	local durationMax = 0;

	for _, info in pairs(loader_table) do
		local entity     = parent:getEntityNamed(info.name);
		local entityType = info.entityType;

		local isProperTypeNotLooped = 	
				(entityType == 'CompositionSequence' and not entity:isCyclic())
			or  (entityType == 'VideoEntity'         and not entity:isLooped());

		if (isProperTypeNotLooped and durationMax < entity:getDuration())	then
			durationMax = entity:getDuration();
			entityMax   = entity;
		end
	end

	return entityMax
end
-------------------------------------------------------------------------------

function ComicsFrame:playAnims(loader_table, parent)
	for _, info in pairs(loader_table) do
		local type = info.entityType
		if --[[type == 'CompositionSequence' or ]] type == 'VideoEntity' then
			local entity = parent:getEntityNamed(info.name)
			entity:play()
        elseif type == 'CompositionSequence' then
            local entity = parent:getEntityNamed(info.name)
            if entity:isVisible() then
			    entity:play()
			end
		elseif type == 'SpriteEntity' then
			local entity = parent:getEntityNamed(info.name)

			if entity:getFramesCount() > 1 then
				entity:play()
			end
		end
	end
end
-------------------------------------------------------------------------------

-- SOUNDS ---------------------------------------------------------------------
function ComicsFrame:playSound()
	if not self.sound then
		-- TODO: поправить все это, когда будут по-человечески сделаны звуки.
		local soundPath
		if IsLevelCollectorsEdition(self.comics.playfield:getName()) then
			soundPath = 'Data/ExtraContent/Sounds/Scenes/'..self.comics.playfield:getName()..'/'..self.comics.shortName..'_'..self.comics.frameIndex..'.ogg'
		else
			soundPath = 'Data/Sounds/Scenes/'..self.comics.playfield:getName()..'/'..self.comics.shortName..'_'..self.comics.frameIndex..'.ogg'
		end
		
		if resourceExists(soundPath) then
			local findVideo = function()
				for _, info in pairs(self.table_resource_action) do
					local type = info.entityType
					if type == 'VideoEntity' then
						return self.action:getEntityNamed(info.name)
					end
				end
			end
			
			local video = findVideo()
			if video then
				video:setVoiceTrack(soundPath)
			else
				self.sound = CreateVoiceTrack(soundPath)
				self.sound:attachTo(self.comics.playfield:getMainEntity())
				self.sound:play()
			end
		end
	else
		self.sound:play()
	end
end
-------------------------------------------------------------------------------

function ComicsFrame:stopSound()
	if self.sound then
		self.sound:stop()
	end
end
-------------------------------------------------------------------------------

function ComicsFrame:setAutoCompleteFrame(is_auto_complete_frame)
	self.auto_complete_frame = is_auto_complete_frame
end
-------------------------------------------------------------------------------