'''
Klasa obslugujaca plWordnet wykorzystujac polaczenie do bazy danych
'''
class DbPlwnHandler:
	def __init__(self, dbconnection):
		self._dbconnection = dbconnection
		self._cursor = dbconnection.cursor() if dbconnection else None
	
	def dbconnection(self):
		return self._dbconnection

	def cursor(self):
		if not self._cursor:
			self._cursor = self.dbconnection().cursor()
		return self._cursor

	def get_relationsynsets(self, synsetid, relation_types):
		'''
		For given synset (ID) returns all synsets connected by 
		relations in list with types of available relations
		with other synset.

		Returns list of pairs {child_synset_id, relation_type}
		'''
		child_relations = []
		syn_pa_relation_set = set()
		# db_query = 'SELECT CHILD_ID FROM synsetrelation WHERE PARENT_ID=? AND REL_ID=?;'

		for reltype in relation_types:
			db_query = 'SELECT CHILD_ID FROM synsetrelation WHERE PARENT_ID=%d AND REL_ID=%d;' % (
					int(synsetid),
					int(reltype)
			)
			self.cursor().execute(db_query)
			# self.cursor().execute(db_query, (str(synsetid), str(reltype)))
			for row in self.cursor().fetchall():
				child_syns_id = row[0]
				# child_relations.append((child_syns_id, reltype))
				child_relations.append((child_syns_id, self.explain_relationtypeid(reltype)))
				if int(reltype) == 208: # rel_id:208, rel_shortcut:syn_pa, rel_name:Syn_plWN-PWN
					syn_pa_relation_set.add(child_syns_id)

		for parent_syns_id in syn_pa_relation_set:
			db_query = 'SELECT CHILD_ID FROM synsetrelation WHERE PARENT_ID=%d AND REL_ID=173;' % (
				int(parent_syns_id))
			self.cursor().execute(db_query)
			for row in self.cursor().fetchall():
				child_syns_id = row[0]
				child_relations.append((child_syns_id, self.explain_relationtypeid(210)))

		return child_relations

	def get_lu_in_synset(self, synsetid):
		'''
		Returns all lexical units in given synset id. List of the lexical units 
		is sorted by unitindex from UnitAdnSynset table

		Returns sorted list of:
		[
			(lu_id_1, lemma_1, variant_1, POS_1, domain_1),
			...
			(lu_id_n, lemma_n, variant_n, POS_n, domain_1),
		]
		'''
		db_query = 'SELECT ' \
				'LEX_ID, lemma, variant, pos, domain '\
				' FROM unitandsynset UAS JOIN lexicalunit LU ON (UAS.LEX_ID = LU.ID) ' \
				' WHERE SYN_ID = %d ORDER BY UAS.unitindex;' % (int(synsetid))
		self.cursor().execute(db_query)
		return self.cursor().fetchall()

	def explain_relationtypeid(self, reltypeid):
		'''
		For given ID of relation returns explenation about this relation.

		Returns pair of {shortcut_relation, fullname_relation}
		'''
		db_query = 'SELECT shortcut, name FROM relationtype WHERE ID = %d;' % (int(reltypeid))
		self.cursor().execute(db_query)
		for row in self.cursor().fetchall():
			return row[0], row[1]
		return ''

	def get_all_synset_ids(self):
		synids = []
		db_query = 'SELECT ID FROM synset;'
		self.cursor().execute(db_query)
		return [int(row[0]) for row in self.cursor().fetchall()]

	def explain_posid(self, posid):
		if posid == 6:
			return 'n'
		if posid == 4:
			return 'przym.'
		if posid == 2:
			return 'rzecz.'
		if posid == 1:
			return 'czas.'
		return str(posid)
