class 'CellBase' (Node)

CellBase.uniqueId = 0

-------------------------------------------------------------------------------
-- STATIC METHODS -------------------------------------------------------------
-------------------------------------------------------------------------------

function CellBase.staticGetUniqueId()
	local id = CellBase.uniqueId
	CellBase.uniqueId = CellBase.uniqueId + 1
	return id	
end

-------------------------------------------------------------------------------
-- INITS ----------------------------------------------------------------------
-------------------------------------------------------------------------------

function CellBase:__init(organizer)
	assert(organizer)

	local id	= CellBase.staticGetUniqueId()
	local name	= string.format('InventoryCell_%d', id)

	Node.__init(self, organizer:getMainEntity(), name, 1)

	self.iconScale			 = 1
	self.iconPos			 = Vector2f(0, 0)
	self.hintTextOffset		 = Vector2f(10, -30)
	self.collectionTextPos	 = Vector2f(0, 30)
	self.collectionTextScale = 0.8
	self.hintTextFont		 = 'Data/Fonts/Options.fnt'
	self.collectionTextFont	 = 'Data/Fonts/Options.fnt'

	self.shadowOrder		 = 1
	self.toolOrder			 = 2
	self.collectionTextOrder = 3

	self.id				= id
	self.pOrganizer		= organizer
	self.toolInfo		= nil
	self.takeTarget		= self:createTakeTarget()	-- extity, при клике по которой будет браться тулза
	self.takeBlocker	= self:createTakeBlocker()
	self.shadowEntity	= nil						-- иконка тулзы, которая будет отображаться в ячейке во время перетаскивания тулзы
	self.hintText		= nil
	self.collectionText	= nil
	self.bVisible		= true
	self.bWaitingTool	= false
end
-------------------------------------------------------------------------------

function CellBase:destroy()
	self.main_entity:destroy()

	if self.hintText then
		self.hintText:destroy()
	end
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- CREATE METHODS -------------------------------------------------------------
-------------------------------------------------------------------------------

-- @override
function CellBase:createMainEntity(parent, name, order)
	assert(self.main_entity == nil)

	self.main_entity = CreateSpriteEntityInstant('Data/Textures/GUI/Inventory/inventory_cell.png')
	self.main_entity:setName		( name )
	self.main_entity:attachTo		( parent )
	self.main_entity:setOrder		( order )
	self.main_entity.onDestroy:add	( self.onEntityDestroy )
end
-------------------------------------------------------------------------------

function CellBase:createTakeTarget()
	local entity = CreateSpriteEntityInstant('Data/Textures/Common/Area_Round.png')
	entity:attachTo	( self.main_entity )
	entity:setName	( string.format('ToolTakeTarget_%d', self.id) )
	entity:setOrder	( 10 )
	entity:setColor	( Colorf(0, 0, 0, 0.1) )
	entity:setScale	( self.main_entity:getWidth()  / entity:getWidth(),
					  self.main_entity:getHeight() / entity:getHeight() )

	entity:setSendEventsToParent( false )
	entity:setCursorOverName	( 'CursorActive' )

	entity.onFocusIn:add  ( bind(self.onFocusIn,	self) )
	entity.onFocusOut:add ( bind(self.onFocusOut,	self) )
	entity.onFocusKeep:add( bind(self.onFocusKeep,	self) )

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

function CellBase:createHintText()
	local parent	 = self.pOrganizer:getHintTextParent()
	local textEntity = CreateTextEntity()

	textEntity:attachTo		 ( parent )
	textEntity:setFont		 ( self.hintTextFont )
	textEntity:setText		 ( self.toolInfo:getHintText() )
	textEntity:setRespondable( false )
	textEntity:setMaxOrder	 ()

	return textEntity
end
-------------------------------------------------------------------------------

function CellBase:createCollectionText()
	local textEntity = CreateTextEntity()

	textEntity:attachTo		 ( self.main_entity )
	textEntity:setFont		 ( self.collectionTextFont )
	textEntity:setPosition	 ( self.collectionTextPos )
	textEntity:setScale		 ( self.collectionTextScale )
	textEntity:setOrder		 ( self.collectionTextOrder )
	textEntity:setRespondable( false )

	return textEntity
end
-------------------------------------------------------------------------------

function CellBase:createTakeBlocker()
	assert(self.takeTarget)
	local blocker = MouseBlocker:new(self.takeTarget)

	blocker.onBlock	  = bind(self.onBlock,   self)
	blocker.onUnblock = bind(self.onUnblock, self)
	blocker:push()

	return blocker
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- SETTERS --------------------------------------------------------------------
-------------------------------------------------------------------------------

function CellBase:setToolInfo(toolInfo)
	assert(not self.toolInfo)
	self.toolInfo = toolInfo

	local dragObject	= toolInfo:getDragObject()
	local toolEntity	= dragObject:getDraggedEntity()
	self.shadowEntity	= toolInfo:createToolShadowEntity()

	if toolInfo:isCollection() then
		self.collectionText = self:createCollectionText()
	end

	self:placeIcon(toolEntity,		  self.toolOrder)
	self:placeIcon(self.shadowEntity, self.shadowOrder)

	dragObject:activate(self.takeTarget)
	self.takeBlocker:pop()
end
-------------------------------------------------------------------------------

function CellBase:placeIcon(entity, order)
	order = order or 1

	entity:attachTo	  (self.main_entity)
	entity:setScale	  (self.iconScale)
	entity:setPosition(self.iconPos)
	entity:setOrder	  (order)
end
-------------------------------------------------------------------------------

function CellBase:setVisible(bVisible, time)
	if self.bVisible ~= bVisible then
		self.bVisible = bVisible

		time = time or 0
		local opacity = bVisible and 1 or 0

		self.main_entity:setRespondable(bVisible)
		self.main_entity:destroyEffectors()

		if time == 0 then
			self.main_entity:setOpacity(opacity)
		else
			self.main_entity:ChangeOpacity(nil, opacity, time)
		end
	end
end
-------------------------------------------------------------------------------

function CellBase:setWaitingTool(bWaitingTool)
	self.bWaitingTool = bWaitingTool
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- GETTERS --------------------------------------------------------------------
-------------------------------------------------------------------------------

function CellBase:getId()
	return self.id
end
-------------------------------------------------------------------------------

function CellBase:getToolInfo()
	return self.toolInfo
end
-------------------------------------------------------------------------------

function CellBase:getTakeBlocker()
	return self.takeBlocker
end
-------------------------------------------------------------------------------

function CellBase:getTakeTarget()
	return self.takeTarget
end
-------------------------------------------------------------------------------

function CellBase:getIconScrPosition()
	return self.main_entity:localToScreen(self.iconPos)
end
-------------------------------------------------------------------------------

function CellBase:getIconScale()
	return self.iconScale
end
-------------------------------------------------------------------------------

function CellBase:getShadowIcon()
	return self.shadowEntity
end
-------------------------------------------------------------------------------

function CellBase:isWaitingTool()
	return self.bWaitingTool
end
-------------------------------------------------------------------------------

function CellBase:getHintTextEntity()
	return self.hintText
end
-------------------------------------------------------------------------------

function CellBase:getToolOrder()
	return self.toolOrder
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- HELPERS --------------------------------------------------------------------
-------------------------------------------------------------------------------

function CellBase:updateCollectionText()
	local collection = self.toolInfo:getCollection()
	
	if collection then
		local toolsCount	  = collection:get_size_tools()
		local toolsFoundCount = collection:get_size_tools_found()
		local text			  = string.format('%d/%d', toolsFoundCount, toolsCount)

		self.collectionText:setText(text)
	end
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- EVENTS ---------------------------------------------------------------------
-------------------------------------------------------------------------------

function CellBase:onBlock(blocker)
	if not self.toolInfo then
		return
	end

	local dragObject = self.toolInfo:getDragObject()

	if not dragObject:isDragging() then
		dragObject:getDraggedEntity():setVisible(false)
	end

	self.shadowEntity:setVisible(true)

	if self.collectionText then
		self.collectionText:setVisible(false)
	end
end
-------------------------------------------------------------------------------

function CellBase:onUnblock(blocker)
	assert(self.toolInfo)
	
	-- if not self.toolInfo then
	-- 	return
	-- end

	local dragObject = self.toolInfo:getDragObject()

	if not dragObject:isDragging() then
		dragObject:getDraggedEntity():setVisible(true)
	end

	self.shadowEntity:setVisible(false)

	if self.collectionText then
		self.collectionText:setVisible(true)
	end
end
-------------------------------------------------------------------------------

function CellBase:onFocusIn(target, screenFocus)
	assert(self.toolInfo)

	if not self.hintText then
		self.hintText = self:createHintText()
	else
		self.hintText:destroyEffectors()
	end

	local appearTime = 0.2
	local effector	 = CreateAnimationEffector()

	effector:setDestroysOnEnd(true)
	effector:attachTo(self.hintText)
	effector:setKey(_TRACK_O, 0, self.hintText:getOpacity())
	effector:setKey(_TRACK_O, appearTime, 1)

	effector:setKey(_TRACK_S, 0, 0.7)
	effector:setKey(_TRACK_S, appearTime * 0.8, 1.1)
	effector:setKey(_TRACK_S, appearTime, 1)
	effector:play()
end
-------------------------------------------------------------------------------

function CellBase:onFocusOut(target, screenFocus)
	self.hintText:destroyEffectors()
	self.hintText:ChangeOpacity(nil, 0, 0.2)
end
-------------------------------------------------------------------------------

function CellBase:onFocusKeep(target, screenFocus)
	local pos = Vector2f(screenFocus:getScrX(), screenFocus:getScrY()) + self.hintTextOffset
	self.hintText:setScrPosition(pos)
end
-------------------------------------------------------------------------------