dofile ('Data/Scripts/Common/HOLogic/HiddenPictureLogic.lua')


function HiddenPictureLogic:registerTask(	dealId, 
											objectName, 
											getObjectName, 
											targetObjectName, 
											onDropTarget, 
											onFind, 
											bCustomObjectPos, 
											targetTextAlias)
	local playfield = self.game_scene:getPlayField()
	local task = self:_createTask(objectName)
	self:_createSubstrate(objectName)
	
	local info            = {}
	info.taskName         = task:getName()
	info.dealId           = dealId
	info.objectName       = objectName
	info.getObjectName    = getObjectName
	info.targetObjectName = targetObjectName
	info.dropTarget       = targetObjectName
	info.onDropTarget     = onDropTarget
	info.onFind           = onFind
	info.targetTextAlias  = targetTextAlias or self:_getTargetAlias(objectName)
	info.state            = self.ETaskState.TS_NONE
	info.inv_scale 		  = 1
	
	table.insert(self.tasksInfo, info)
	
	info.node = path_manager:addTaskNode(getObjectName)
	if info.node then
		info.node:setState(NodeState.PENDING)
	end
	
	if targetObjectName and dealId == self.dealIndex then
		if type(targetObjectName) == 'table' then
			for area in each(targetObjectName) do
				playfield:registerBBT(area, info.targetTextAlias, EBBTAliasOrder.RANDOM, nil, true)
			end
		else
			playfield:registerBBT(targetObjectName, info.targetTextAlias, EBBTAliasOrder.RANDOM, nil, true)
		end
	end
	
	if IsTaskObjectFound(playfield:getName(), objectName) then
		self:_hideGetObject(info.getObjectName, 0)

		local bShowTask = info.targetObjectName
							and not IsDragObjectUsed( info.objectName, playfield:getName() )
							and (self.dealIndex == dealId)

		if bShowTask then
			self:_showTask(task)
			self:_activateTask(task)
		   
		   -- подгонка слоя и позици объекта для реализации сохранки
			do 
				local object = playfield:getEntityNamed(objectName)
				local anchor = self.indicator:getAnchor()
				local pos    = self.indicator:getTaskScrPosition(task)
				local scale  = self.indicator:getTaskScale(task)
				
				object:attachTo(anchor)
				object:setScrXY(pos.x, pos.y)
				object:setScale(scale)
			end
		end
		
		if info.onFind then
			local event = {}
			event.save = true
			event.info = info
			
			info.onFind(event)
		end
		
		self:_registerDrag(task)
	else
		-- объект ставится в позицию предмета для нахождения
		if not bCustomObjectPos then
			local target_object = playfield:getEntityNamed(info.objectName)
			local get_object = playfield:getEntityNamed(info.getObjectName)
			target_object:setScrPosition(get_object:getScrPosition())
		end
	
		if self.dealIndex == dealId then
			self:addTask(task)
			if self.indicator:FreeSlotsExist() then
				self:_showTask(task)
			else
				self:_reserveTask(task)
			end
		else
			playfield:getEntityNamed(objectName):setRespondable(false)
			playfield:getEntityNamed(getObjectName):setRespondable(false)
		end
	end
	
	local playfield = self.game_scene:getPlayField()
	-- local task_objects = task:getObjects()
	-- for _, object_name in pairs(task_objects) do
	-- playfield:getTimer():add(function()
		self.game_scene:getEntityNamed(getObjectName).onClickBegin:add(	function() 
																			self:onClickObject(objectName, getObjectName) 
																		end)
	-- end, 2)
	-- end

	-- self.task_objects_count = self:getTaskObjectsCount()
	-- if self.task_objects_count == 0 then
	--     self:onLastItemFound()
	-- end
	return info
end

function HiddenPictureLogic:_registerDrag(task)
	local playfield = self.game_scene:getPlayField()
	local taskName = task:getName()
	local info     = self:getTaskInfo(taskName)
	
	if info.targetObjectName then
		local function on_get(e)
			self.game_scene:getEntityNamed(info.objectName):setScale(1, 1)
			self.game_scene:getEntityNamed(info.objectName):setOpacity(1)
			self.game_scene:getEntityNamed(info.objectName):show()
			self.indicator:setIndicatorColor(taskName, self.indicator.COLOR_DRAGGING)
			self.indicator:SetIndicatorIgnoreTestPoint(taskName, true)
			for k, v in pairs(self.createdTaskObjects) do
				self.game_scene:getEntityNamed(self:_getStartDragAreaName(v)):setRespondable(false)
			end

			if info.on_get then
				info:on_get()
			end
		end
		
		local function on_drop(e)
			Helpers.ChangeScale(self.game_scene:getEntityNamed(info.objectName), 1, self.indicator:GetIconScale(taskName).x, e.time)
			for k, v in pairs(self.createdTaskObjects) do
				self.game_scene:getEntityNamed(self:_getStartDragAreaName(v)):setRespondable(true)
			end			
			playfield:getTimer():add(function()
											-- self.game_scene:getEntityNamed(info.objectName):setOpacity(0)
											-- self.game_scene:getEntityNamed(info.objectName):setRespondable(false)
											self.game_scene:getEntityNamed(info.objectName):hide()
											self.indicator:setIndicatorColor(taskName, self.indicator.COLOR_DEFAULT)
											self.indicator:SetIndicatorIgnoreTestPoint(taskName, false)											
										end, e.time)
		end
		
		local function on_drop_target(e)
			if not e.save then				
				AddUniqueGameState(TASK_OBJECT_DROPPED, playfield:getName() .. '_' .. info.objectName)
				if self:_isAllTargetsUsed(info) then
					-- self.game_scene:getEntityNamed(info.objectName):setOpacity(0)
					-- self.game_scene:getEntityNamed(info.objectName):setRespondable(false)
					self.game_scene:getEntityNamed(info.objectName):hide()
					self.indicator:setIndicatorColor(taskName, self.indicator.COLOR_DEFAULT)
					
					self:_hideTask(task)
					self:_showNextTask()
				else
					e.time = 0
					
					-- Uncomment if item should be dropped when used on one of a multiple targets

					local function predicate(dragObject)
						return dragObject:getDraggedEntity():getName() == e.objName
					end

					local dragManager = self.game_scene:getPlayField():getDragManager()
					local dragObject  = dragManager:find( predicate )
					on_drop(e)
					dragObject:defaultAfterDrop()
					--on_drop(e)
				end
			end
			
			if info.onDropTarget then
				local event  = {}
				event.save   = e.save
				event.info   = info
				event.target = e.target
				event.taskName = taskName
				event.targetName = e.targetName
			
				info.onDropTarget(event)
			end
			
			for k, v in pairs(self.createdTaskObjects) do
				local start_drag_area = self:_getStartDragAreaName(v)
				if self.game_scene:hasEntityNamed(start_drag_area) then
					self.game_scene:getEntityNamed(start_drag_area):setRespondable(true)
				end
			end
		end
		
		info.state = self.ETaskState.TS_DRAGGABLE
		playfield:registerDragObject(info.objectName, self:_getStartDragAreaName(info.objectName), info.dropTarget, on_get, on_drop, on_drop_target, nil, true)

		if type(info.targetObjectName) == 'table' then
			for area in each(info.targetObjectName) do
				playfield:getHint():setAdditionalHintObject( area )				
			end
		else
			playfield:getHint():setAdditionalHintObject( info.targetObjectName )
		end
	end
end