from swyzxwb_photovote.common.DataConvert import Str2Date,str2int
from django.template.loader import render_to_string
from django.conf import settings

class AddableDict(dict):
	def __add__(d1,d2):
		return AddableDict(d1.items()+d2.items())

class FilterCondition:
	def __init__(self):
		self.fcd={
			"index":[],
			"ceq":AddableDict(),
			"cueq":AddableDict(),
			"cc":AddableDict(),
			"cuc":AddableDict(),
			"csw":AddableDict(),
			"cew":AddableDict(),
			"cnsw":AddableDict(),
			"cnew":AddableDict(),
			"deq":AddableDict(),
			"drg":AddableDict(),
			"neq":AddableDict(),
			"nneq":AddableDict(),
			"ngt":AddableDict(),
			"nlt":AddableDict(),
			"csc":AddableDict(),
			"cmc":AddableDict(),
			"sf":AddableDict(),
		}
	def addTextEqual(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['ceq'].update({name:(title,fieldname)})
	def addTextUnequal(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['cueq'].update({name:(title,fieldname)})
	def addTextContain(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['cc'].update({name:("*"+title+"*",fieldname)})
	def addTextUncontain(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['cuc'].update({name:("*"+title+"*",fieldname)})
	def addTextStartWith(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['csw'].update({name:(title+"*",fieldname)})
	def addTextEndWith(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['cew'].update({name:("*"+title,fieldname)})
	def addTextNotStartWith(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['cnsw'].update({name:(title+"*",fieldname)})
	def addTextNotEndWith(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['cnew'].update({name:("*"+title,fieldname)})
	def addDateEqual(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['deq'].update({name:(title,fieldname)})
	def addDateRange(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['drg'].update({name:(title,fieldname)})
	def addNumberEqual(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['neq'].update({name:(title,fieldname)})
	def addNumberUnEqual(self,name,title,fieldname):
		self.fcd['index'].append(name)
		self.fcd['nneq'].update({name:(title,fieldname)})
	def addNumberGreatThan(self,name,title,fieldname,equal):
		self.fcd['index'].append(name)
		self.fcd['ngt'].update({name:(title,fieldname,equal)})
	def addNumberLessThan(self,name,title,fieldname,equal):
		self.fcd['index'].append(name)
		self.fcd['nlt'].update({name:(title,fieldname,equal)})
	def addSingleChoice(self,name,title,fieldname,clist,alias=None):
		self.fcd['index'].append(name)
		self.fcd['csc'].update({name:(title,fieldname,clist,alias)})
	def addMultiChoice(self,name,title,fieldname,clist,alias=None):
		self.fcd['index'].append(name)
		self.fcd['cmc'].update({name:(title,fieldname,clist,alias)})
	def AddSortField(self,name,title,fieldname):
		self.fcd['sf'].update({name:(title,fieldname)})
	def RenderHTML(self,request):
		def fcondget(kn):
			try:
				r=fcond.get(kn)
			except:
				r=""
			if not r:
				r=""
			return r
		fcond = request.GET
		dtext=self.fcd['ceq']+self.fcd['cueq']+self.fcd['cc']+self.fcd['cuc']+self.fcd['csw']+self.fcd['cew']+self.fcd['cnsw']+self.fcd['cnew']
		dval=self.fcd['neq']+self.fcd['nneq']+self.fcd['ngt']+self.fcd['nlt']
		#dtext=self.fcd['ceq']+self.fcd['cueq']
		iText=[]
		iVal=[]
		iDateE=[]
		iDateR=[]
		iSC=[]
		iMC=[]
		kl=filter(lambda x:x in dtext.keys(),self.fcd['index'])
		for i in kl:
			iText.append({"ctname":i,"ctid":"ipc_"+i,"title":dtext[i][0],"value":fcondget(i)})
		kl=filter(lambda x:x in dval.keys(),self.fcd['index'])
		for i in kl:
			iVal.append({"ctname":i,"ctid":"ipc_"+i,"title":dval[i][0],"value":fcondget(i)})
		kl=filter(lambda x:x in self.fcd['deq'].keys(),self.fcd['index'])
		for i in kl:
			iDateE.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['deq'][i][0],"value":fcondget(i)})
		kl=filter(lambda x:x in self.fcd['drg'].keys(),self.fcd['index'])
		for i in kl:
			try:
				vst,ved=fcond.get(i).split('~')
			except:
				vst,ved="",""
			if not vst:
				vst=""
			if not ved:
				ved=""
			iDateR.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['drg'][i][0],"valuest":vst,"valueed":ved})
		kl=filter(lambda x:x in self.fcd['csc'].keys(),self.fcd['index'])
		for i in kl:
			fc=fcondget(i)
			if fc=="":
				fc="all"
			if self.fcd['csc'][i][3]:
				iSC.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['csc'][i][0],"oplst":self.fcd['csc'][i][3],"value":fc})
			else:
				iSC.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['csc'][i][0],"oplst":self.fcd['csc'][i][2],"value":fc})
		kl=filter(lambda x:x in self.fcd['cmc'].keys(),self.fcd['index'])
		for i in kl:
			if self.fcd['cmc'][i][3]:
				iMC.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['cmc'][i][0],"oplst":self.fcd['cmc'][i][3],"value":fcondget(i)})
			else:
				iMC.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['cmc'][i][0],"oplst":self.fcd['cmc'][i][2],"value":fcondget(i)})
		kwvars={
			"textitems":iText,
			"valitems":iVal,
			"dateeqi":iDateE,
			"datergi":iDateR,
			"scitems":iSC,
			"mcitems":iMC
		}
		return render_to_string("common/Filter/maindiv.html",kwvars)

def _CommonFilter(request,fco,data):
	fcond = request.GET
	td=data
	wl=filter(lambda x:x in fco.fcd['ceq'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['ceq'][i][1]+"__exact":fcond[i]}
			td=td.filter(**argv)
	wl=filter(lambda x:x in fco.fcd['cueq'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['cueq'][i][1]+"__exact":fcond[i]}
			td=td.exclude(**argv)
	wl=filter(lambda x:x in fco.fcd['cc'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['cc'][i][1]+"__contains":fcond[i]}
			td=td.filter(**argv)
	wl=filter(lambda x:x in fco.fcd['cuc'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['cuc'][i][1]+"__contains":fcond[i]}
			td=td.exclude(**argv)
	wl=filter(lambda x:x in fco.fcd['csw'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['csw'][i][1]+"__startswith":fcond[i]}
			td=td.filter(**argv)
	wl=filter(lambda x:x in fco.fcd['cnsw'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['cnsw'][i][1]+"__startswith":fcond[i]}
			td=td.exclude(**argv)
	wl=filter(lambda x:x in fco.fcd['cew'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['cew'][i][1]+"__endswith":fcond[i]}
			td=td.filter(**argv)
	wl=filter(lambda x:x in fco.fcd['cnew'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['cnew'][i][1]+"__endswith":fcond[i]}
			td=td.exclude(**argv)
	#====================
	wl=filter(lambda x:x in fco.fcd['deq'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['deq'][i][1]+"__exact":Str2Date(fcond[i])}
			td=td.filter(**argv)
	wl=filter(lambda x:x in fco.fcd['drg'].keys(),fcond.keys())
	if wl:
		for i in wl:
			try:
				pl=fcond[i].split("~",2)
				argv={fco.fcd['drg'][i][1]+"__range":(Str2Date(pl[0]),Str2Date(pl[1]))}
				td=td.filter(**argv)
			except:
				pass
	#====================
	wl=filter(lambda x:x in fco.fcd['neq'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['neq'][i][1]+"__exact":str2int(fcond[i])}
			td=td.filter(**argv)
	wl=filter(lambda x:x in fco.fcd['nneq'].keys(),fcond.keys())
	if wl:
		for i in wl:
			argv={fco.fcd['nneq'][i][1]+"__exact":str2int(fcond[i])}
			td=td.exclude(**argv)

	wl=filter(lambda x:x in fco.fcd['ngt'].keys(),fcond.keys())
	if wl:
		for i in wl:
			if fco.fcd['ngt'][i][2]:
				argv={fco.fcd['ngt'][i][1]+"__gte":str2int(fcond[i])}
			else:
				argv={fco.fcd['ngt'][i][1]+"__gt":str2int(fcond[i])}
			td=td.filter(**argv)
	wl=filter(lambda x:x in fco.fcd['nlt'].keys(),fcond.keys())
	if wl:
		for i in wl:
			if fco.fcd['nlt'][i][2]:
				argv={fco.fcd['nlt'][i][1]+"__lte":str2int(fcond[i])}
			else:
				argv={fco.fcd['nlt'][i][1]+"__lt":str2int(fcond[i])}
			td=td.filter(**argv)
	#==============================
	wl=filter(lambda x:x in fco.fcd['csc'].keys(),fcond.keys())
	if wl:
		for i in wl:
			if not fcond[i]=="all":
				iid=str2int(fcond[i])
				idn=fco.fcd['csc'][i][2][iid]
				argv={fco.fcd['csc'][i][1]+"__exact":idn}
				td=td.filter(**argv)
	wl=filter(lambda x:x in fco.fcd['cmc'].keys(),fcond.keys())
	if wl:
		for i in wl:
			idlst=fcond[i].split(',')
			idn=map(lambda x:fco.fcd['cmc'][i][2][str2int(x)],idlst)
			argv={fco.fcd['cmc'][i][1]+"__in":idn}
			td=td.filter(**argv)
	return td

def CommonFilter(request,fco,data):
	if settings.DEBUG:
		return _CommonFilter(request,fco,data)
	else:
		try:
			_CommonFilter(request,fco,data)
		except:
			return data