import logging
import item

class ItemManager():
	"""
	Ususally used as a singleton instance to manage the collections of items.
	"""
	
	def __init__(self,**kwargs):
		"""
		Initializes ItemManager instance.
		Returns: None
		"""
		logging.info("#### ItemManager.__init__[]")
		self.items = {}
		if 'persist' in kwargs:
			self.persist = kwargs['persist']
		else:
			self.persist = False
		if 'cache_worker' not in kwargs:
			logging.error("#### ItemManager.__init__[]: NO CACHE WORKER NAME PROVIDED")
			return None
		self.cache_worker = kwargs['cache_worker']
		return None
		
	def __call__(self):
		"""
		Used to access all loaded items.
		Returns: self.items
		"""
		logging.info("#### ItemManager.__call__[]")
		logging.info("#### ItemManager.__call__: "+len(self.caches)+" items in memory")
		return self.items
		
	def __contains__(self,_id):
		"""
		Used to determine if item is loaded.
		Returns: Boolean
		"""
		logging.info("#### ItemManager.__contains__["+cid+"]")
		logging.info("#### ItemManager.__contains__: "+len(self.caches)+" items in memory")
		return _id in self.items
	
	def __getitem__(self,_id):
		"""
		Returns specific item by _id, if loaded.
		Returns: Item, or None
		"""
		logging.info("#### ItemManager.__getitem__["+_id+"]")
		if _id in self.items:
			return self.items[_id]
		else:
			return None
		
	def __setitem__(self,_id,value):
		"""
		Sets a given item by _id to value.
		Returns: None
		"""
		logging.info("#### ItemManager.__setitem__["+_id+"]")
		if _id in self.items:
			self.items[_id] = value
		return None
		
	def get(self,**kwargs):
		"""
		Gets an item. Items are accessed by kwargs['_id'] if present,
		or with key generated by itemManager.generate_key(). If item is not loaded,
		the item will be instantiated.
		"""
		_id = None
		if '_id' in kwargs:
			_id = kwargs['_id']
		else:
			if 'pars' in kwargs:
				_id = self.generate_key(kwargs['pars'])
		if _id is None:
			logging.info("#### ItemManager.get[] NO ID OR PARS PROVIDED")
		logging.info("#### ItemManager.get["+_id+"]")
		if _id in self.items:
			if not self.persist:
				return self.items[_id]()
			return self.items[_id]
		else:
			logging.info("#### ItemManager.get["+_id+"] DOES NOT EXIST")
			if '_id' in kwargs:
				del kwargs['_id']
			self.items[_id] = item.Item(_id,persist=self.persist,cache_worker=self.cache_worker,**kwargs)
			return self.items[_id]
			
	def generate_key(self,pars):
		"""
		Generates item collection key used to index items. Uses pars['kind'] and/or 
		pars['name'].
		"""
		logging.info("$$$$ ItemManager.generate_key["+str(pars)+"]")
		_id = None
		if 'name' in pars:
			if 'kind' in pars:
				_id = pars['kind'] + "_" + pars['name']
			else:
				_id = pars['name']
		return _id.replace(',','').replace(' ','')
		