class 'InteractiveHelpScreenBase' (BaseScreen)

function InteractiveHelpScreenBase:__init(name)
	BaseScreen.__init(self, name)

	self.closeConnectors = {}
	self.confirmCallback = nil
	self.forced_object	 = nil

	self:setupButtons()
	self:getMainEntity():setSize(0, 0)
	
	InteractiveHelp.tutorialScreen = self
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:__finalize()
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:setupButtons()
	local bttn_skip_tutorial = self:getEntityNamed('bttn_skip_tutorial')
	bttn_skip_tutorial.onPress:add(bind(self.onSkipTutorialButtonClick, self))
	bttn_skip_tutorial:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )

	local fadeInEntity = self:getEntityNamed('fade_in_on_load')
	fadeInEntity:setVisible(false)

	table.insert(self.closeConnectors,
		fadeInEntity.onPress:add( bind(self.onScreenDestroy, self) ))
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:onUpdate(dt)
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:onScreenDestroy(target, screenFocus)
	PopUpScreensMgr:pop()
	
	if self.forced_object then
		current_scene:getPlayField():setForcedObject(nil)
	end

	if self.confirmCallback then
		self.confirmCallback(screenFocus)
	end

	self:removeConnectors()
	
	AddGameState(InteractiveHelp.States.HELP_SHOWN, self.messageTextAlias)
	InteractiveHelp.tutorialScreen = nil
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:onSkipTutorialButtonClick()
	self:removeConnectors()

	if self.play_field_forced_object then
		current_scene:getPlayField():setForcedObject(self.play_field_forced_object)
		self.play_field_forced_object = nil
	else
		current_scene:getPlayField():setForcedObject(nil)
	end

	InteractiveHelp.tutorialScreen = nil
	InteractiveHelp.skip()

	PopUpScreensMgr:pop()
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:removeConnectors()
	if self.forced_object_connection then
		self.forced_object_connection:remove()
		self.forced_object_connection = nil
	end

	for i, connector in ipairs(self.closeConnectors) do
		connector:remove()
	end
	self.closeConnectors = nil

	self:getEntityNamed('bttn_skip_tutorial').onPress:removeAll()
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:setData(pos_window, messageTextAlias, instructionsTextAlias)
	self.messageTextAlias		= messageTextAlias
	self.instructionsTextAlias	= instructionsTextAlias

	self:getEntityNamed('InteractiveHelpWindow'):setXY(pos_window.x, pos_window.y)
	self:getEntityNamed('textMessage'):setText( StringManager:getString(messageTextAlias) )
	self:getEntityNamed('textInstruction'):setText( StringManager:getString(instructionsTextAlias) )
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:addArrowPointerAt(x, y, angle)
	local angleReckoner = function ()
		local windowPos = self:getEntityNamed('InteractiveHelpWindow'):getPosition()
		local v = windowPos - Vector2f(x, y)
		local delta = windowPos - v
		local delta2 =  v - windowPos
		local angle = math.atan2(delta2.y, delta2.x) + M_PI
		return angle
	end
	angle = angle or angleReckoner()
	
	local arrow = self:createArrow(x, y, angle)
	
	local eff = CreateAnimationEffector()
	eff:setDestroysOnEnd(true)
	eff:attachTo(arrow)
	eff:setKey(_TRACK_SX, 0.0, 1.0)
	eff:setKey(_TRACK_SX, 1.0, 1.3)
	eff:setKey(_TRACK_SX, 2.0, 1.0)
	eff:setMode(_ANIM_PINGPONG + _ANIM_LOOP)
	eff:play()
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:createArrow(x, y, angle)
	local arrow = CreateSpriteEntity('Data/Textures/Screens/InteractiveHelp/TutorialArrow.png')
	arrow:attachTo( self:getMainEntity() )
	arrow:setRespondable(false)
	arrow:setName('TutorialArrow')
	arrow:setColor( Colorf(1, 1, 0, 1) )
	arrow:setHotSpot(185, 39)
	arrow:setRotationRad(angle)
	arrow:setXY(x, y)
	arrow:setRespondable(false)
	arrow:setOrder(1000)
	return arrow
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:addArrowPointerAtObject(gameObject, angle)
	self:addArrowPointerAt(gameObject:getX(), gameObject:getY(), angle)
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:setRect(x, y, w, h, cursor_name)
	local entity = CreateSpriteEntity('Data/Textures/Common/Fade.png')
	entity:attachTo(self:getMainEntity())
	entity:setName('block')
	entity:setXY(512, 384)
	entity:setScale(5, 5)
	entity:setOrder(7)
	entity:setColor( Colorf(1, 1, 1, 0.01) )
	
	local click_object = CreateSpriteEntity('')
	click_object:attachTo(self:getMainEntity())
	click_object:setName('click_object in InteractiveHelpScreen')
	click_object:setXY(x, y)
	click_object:setScale(1, 1)
	click_object:setOrder(8)
	click_object:setAnimationSize(w, h)
	click_object:setHotSpot(w/2, h/2)
	click_object:setColor( Colorf(1, 0, 0, 0.01) )
	
	if cursor_name then
		click_object:setCursorOverName(cursor_name)
	end

	table.insert(self.closeConnectors,
		click_object.onPress:add( bind(self.onScreenDestroy, self) ))

	self:entityHide('fade_in_on_load')
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:setRects(cursor_name, ...) --parameters are Rectanglef 
    local rects = {...}
    assert(rects, 'need create rects where tutorial is active')
    
    local entity = CreateSpriteEntity('Data/Textures/Common/Fade.png')
	entity:attachTo(self:getMainEntity())
	entity:setName('block')
	entity:setXY(512, 384)
	entity:setScale(5, 5)
	entity:setOrder(7)
	entity:setColor( Colorf(1, 1, 1, 0.01) )
	
	for i, rect in ipairs(rects) do 
	    local click_object = CreateSpriteEntity('')
	    click_object:attachTo(self:getMainEntity())
	    click_object:setName('InteractiveHelpScreenClickRect'.. i)
	    click_object:setXY(rect:getLeftTop().x, rect:getLeftTop().y)
	    click_object:setScale(1, 1)
	    click_object:setOrder(8)
	    click_object:setAnimationSize(rect:getWidth(), rect:getHeight())
	    click_object:setHotSpot(rect:getWidth()/2, rect:getHeight()/2)
	    click_object:setColor( Colorf(1, 0, 0, 0.01) )
	    
	    if cursor_name then
		    click_object:setCursorOverName(cursor_name)
	    end
	    table.insert(self.closeConnectors,
	    	click_object.onPress:add( bind(self.onScreenDestroy, self) ))
	end
	self:entityHide('fade_in_on_load')
end
-------------------------------------------------------------------------------

function InteractiveHelpScreenBase:setForcedObject(object)
	if object then
		self.forced_object_connection = object.onClickBegin:add( bind(self.onScreenDestroy, self) )
		self.play_field_forced_object = nil
		if current_scene:getPlayField().forced_object_data then
			self.play_field_forced_object = current_scene:getPlayField().forced_object_data.object
		end
		current_scene:getPlayField():setForcedObject(object)
	else
		if self.forced_object_connection then
			self.forced_object_connection:remove()
			self.forced_object_connection = nil
		end
		current_scene:getPlayField():setForcedObject(nil)
	end

	self.forced_object = object
	self:entityHide('fade_in_on_load')
end
-------------------------------------------------------------------------------

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

function InteractiveHelpScreenBase:go()
	BaseScreen.go(self)
	
	if not getPlayModeParameters().tutorialEnabled  then --GetGameState(PLAY_MODE):get(0) ~= EPlayMode.CASUAL
        self:onSkipTutorialButtonClick()
    end
end
-------------------------------------------------------------------------------