# -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.db.models import Q

from picklefield import PickledObjectField

from drill.models import Source, Semtype, Word, Tag, Form

class Question(models.Model):
	qid = models.CharField(max_length=200)
	task = models.CharField(max_length=20)
	string = models.CharField(max_length=200)
	qtype = models.CharField(max_length=20)
	gametype = models.CharField(max_length=5)
	source = models.ManyToManyField(Source)

	answers = models.ManyToManyField('Answer')
	def __unicode__(self):
		return self.qid + ': ' + self.string


class Answer(Question):
	pass

# TODO: Remove the reliance on Wordqelement_set, or improve it.

class QElement(models.Model):
	"""
		QElements are individual elements of a question, such as a pronoun, subject, N-ACC, etc.
		They contain a set of WordQElements which represent each possible Word item in the database
		which could be filled in for a given slot in a question.
		
		WordQElements are filtered when installed by the database, as such there should be no need
		to filter in qagame (???)
		
		
	"""
	question = models.ForeignKey(Question, null=True)
	syntax = models.CharField(max_length=50)
	gametype = models.CharField(max_length=5)
	
	# Meta
	identifier = models.CharField(max_length=20)
	game = models.CharField(max_length=20)
	copy = models.BooleanField(default=False)
	task = models.BooleanField(default=False)
	agreement = models.CharField(max_length=10, null=True)

	# Instead of storing a ton of WordQElements, we'll store the
	# query arguments relative to a word element
	# {'word__semtype__semtype': 'PERSON', 'word__form__tag': 'N+Sg+Nom'}
	qkwargs = PickledObjectField(blank=True)

	def __unicode__(self):
		return self.question.string + ': ' + self.identifier

	def __init__(self, *args, **kwargs):
		super(QElement, self).__init__(*args, **kwargs)
		self.possible_elements = Form.objects.filter(**self.qkwargs)

# class WordQElement(models.Model):
# 	"""
# 		
# 	"""
# 	word = models.ForeignKey(Word, null=True)
# 	qelement = models.ForeignKey(QElement, null=True)
# 	#semtype = models.ForeignKey(Semtype, null=True)
	

