class 'TutorialScreenBase' (BaseScreen)

TutorialScreenBase.MSG_TEXT_PREFIX	 = 'INTERACTIVE_HELP_MSG_'
TutorialScreenBase.INSTR_TEXT_PREFIX = 'INTERACTIVE_HELP_INSTR_'

function TutorialScreenBase:__init(name, tutorial)
	BaseScreen.__init(self, name)
	assert(tutorial)

	self.rectNameFormat	 = 'tutorialRect%d'
	self.arrowNameFormat = 'tutorialArrow%d' 

	self.tutorial	  = tutorial
	self.id			  = nil
	self.forsedObject = nil
	self.arrows		  = {}
	self.rects		  = {}

	self.confirmCallback = nil
	self.skipCallback	 = nil

	self.connectors = {}
	self.forcedObjectConnector = nil

	self:setupButtons()
	self:getFadeObject():hide()

	self.tutorial.tipScreen = self
end
-------------------------------------------------------------------------------

function TutorialScreenBase:setId(id)
	self.id = id
	
	local instruction = TutorialScreenBase.INSTR_TEXT_PREFIX..string.upper(id)
	local message	  = TutorialScreenBase.MSG_TEXT_PREFIX..string.upper(id)

	self:getEntityNamed('textMessage'	 ):setText( StringManager:getString( message ) )
	self:getEntityNamed('textInstruction'):setText( StringManager:getString( instruction ) )
end
-------------------------------------------------------------------------------

function TutorialScreenBase:setupButtons()
	local skipButton = self:getEntityNamed('bttn_skip_tutorial')
	skipButton.onPress:add( bind(self.skip, self) )
	skipButton:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
end
-------------------------------------------------------------------------------

function TutorialScreenBase:addArrowPointerAtObject(entity, angle)
	local scrPos = entity:getScrPosition()
	local pos	 = self:getMainEntity():screenToLocal( scrPos )

	self:addArrowPointerAt(pos, angle)
end
-------------------------------------------------------------------------------

function TutorialScreenBase:addArrowPointerAt(pos, angle)
	local arrow

	angle = angle or self:getAngleToWindow( pos )
	arrow = self:createArrow(pos, angle)

	self:createArrowEffector(arrow)
end
-------------------------------------------------------------------------------

function TutorialScreenBase:createArrow(pos, angle)
	local entityName = string.format(self.arrowNameFormat, #self.arrows)

	local entity = CreateSpriteEntity('Data/Textures/Screens/Tutorial/TutorialArrow.png')
	table.insert(self.arrows, entity)

	entity:attachTo		 ( self:getMainEntity() )
	entity:setName		 ( entityName )
	entity:setColor		 ( Colorf(1, 1, 0, 1) )
	entity:setHotSpot	 ( 64, 16 )
	entity:setRotationRad( angle )
	entity:setPosition	 ( pos )
	entity:setOrder		 ( 10 )
	entity:setRespondable( false )

	return entity
end
-------------------------------------------------------------------------------

function TutorialScreenBase:createArrowEffector(arrow)
	local scaleX = arrow:getScale().x

	local eff = CreateAnimationEffector()
	eff:setDestroysOnEnd(true)
	eff:attachTo(arrow)
	eff:setKey(_TRACK_SX, 0, scaleX * 1)
	eff:setKey(_TRACK_SX, 1, scaleX * 1.3)
	eff:setKey(_TRACK_SX, 2, scaleX * 1)
	eff:setMode(_ANIM_PINGPONG + _ANIM_LOOP)
	eff:play()
end
-------------------------------------------------------------------------------

function TutorialScreenBase:getAngleToWindow(fromPos)
	local windowPos	= self:getEntityNamed('TutorialWindow'):getPosition()
	local deltaPos	= fromPos - windowPos
	return math.atan2(deltaPos.y, deltaPos.x)
end
-------------------------------------------------------------------------------

function TutorialScreenBase:addRect(pos, size, cursorName)
	local name = string.format(self.rectNameFormat, #self.rects)
	local rect = CreateSpriteEntity('')
	table.insert(self.rects, rect)

	rect:attachTo			( self:getMainEntity() )
	rect:setName			( name )
	rect:setAnimationSize	( size )
	rect:setHotSpot			( size.x/2, size.y/2 )
	rect:setColor			( Colorf(1, 0, 0, 0.01) )
	rect:setPosition		( pos )
	rect:setOrder			( 8 )

	if cursorName then
		rect:setCursorOverName( cursorName )
	end

	local callback = function(target, screenFocus)
		self.clickedEntity = target
		self:close()
	end

	rect.onPress:add(callback)
	return rect
end
-------------------------------------------------------------------------------

function TutorialScreenBase:setForcedObject(object)
	self.forsedObject = object

	if object then
		local rectangle = object:getBoundingBox()

		--local rect = CreateRenderedObject()
		local rect = CreateSpriteEntity('')
		table.insert(self.rects, rect)
		rect:attachTo			( self:getMainEntity() )
		rect:setPosition		( self:getMainEntity():screenToLocal( object:getScrPosition() ) )
		rect:setSize			( Vector2f(rectangle:getWidth(), rectangle:getHeight()) )
		-- rect:setBoundingBox		( object:getBoundingBox() )
		rect:setHotSpot			( object:getHotSpot() )
		rect:setScale			( object:getScrScale().x, object:getScrScale().y )
		rect:setRotationRad		( object:getScrRotationRad() )
		rect:setCursorOverName	( object:getCursorOverName() )
		rect:setName			( object:getName()..'_tutorialRect' )
		rect:setColor			( Colorf(1, 1, 1, 0.01) )
		rect:setOrder			( 8 )

		local callback = function(target, screenFocus)
			self.clickedEntity = target
			self:close()

			object.onPress:notify(object, screenFocus)
		end

		self.forcedObjectConnector = rect.onPress:add( callback )

		return rect
	else
		--TODO: проверить, нужно ли в этом месте удалять rect
		if self.forcedObjectConnector then
			self.forcedObjectConnector:remove()
			self.forcedObjectConnector = nil
		end
	end
end
-------------------------------------------------------------------------------

function TutorialScreenBase:closeOnPress()
	local callback = function(target, screenFocus)
		self.clickedEntity = target
		self.bClosedOnPress = true
		self:close()
	end

	self:getMainEntity().onPress:add( callback )
end
-------------------------------------------------------------------------------

function TutorialScreenBase:setWindowPosition(pos)
	self:getEntityNamed('TutorialWindow'):setPosition(pos)
end
-------------------------------------------------------------------------------

function TutorialScreenBase:setConfirmCallback(callback)
	self.confirmCallback = callback
end
-------------------------------------------------------------------------------

function TutorialScreenBase:setSkipCallback(callback)
	self.skipCallback = callback
end
-------------------------------------------------------------------------------

function TutorialScreenBase:close(bDontConfirm)
	PopUpScreensMgr:pop()

	if bDontConfirm then
		return
	end

	self.tutorial:setTipShown(self.id)

	if self.confirmCallback then
		local event	 = {}
		event.sender = self
		event.target = self.clickedEntity

		self.confirmCallback(event)
	end
end
-------------------------------------------------------------------------------

function TutorialScreenBase:skip()
	self.tutorial.tipScreen = nil
	self.tutorial:skip()

	PopUpScreensMgr:pop()

	if self.skipCallback then
		local event	 = {}
		event.sender = self

		self.skipCallback(event)
	end
end
-------------------------------------------------------------------------------

function TutorialScreenBase:destroyScreen()
	BaseScreen.destroyScreen(self)
	self.tutorial.tipScreen = nil
end
-------------------------------------------------------------------------------

function TutorialScreenBase:go()
	BaseScreen.go(self)
	
	if GetGameState(PLAY_MODE):get(0) ~= EPlayMode.CASUAL then
		self:skip()
	end
end
-------------------------------------------------------------------------------