from django.conf import settings
from ldt.indexation import STORE
import lucene
import uuid

__BOOLEAN_DICT = {
    'false':False,
    'true':True,
    '0':False,
    '1':True,
    't': True,
    'f':False
}

def boolean_convert(bool):
    if bool is None:
        return False
    if bool is True or bool is False:
        return bool
    key = str(bool).lower()
    return __BOOLEAN_DICT.get(key, False)


def generate_uuid():
    return unicode(uuid.uuid1())


#def normalize_tags(list):
#    nlist=[]
#    for tag in list:
#        tag = tag.lower()
#        nlist.append(tag)
#    taglist = dict().fromkeys(nlist).keys()    
#    
#    return taglist


class TextSearch(object):

    def query(self, field, query):
        indexSearcher = lucene.IndexSearcher(STORE)
        queryParser = lucene.QueryParser(lucene.Version.LUCENE_30, field, lucene.FrenchAnalyzer(lucene.Version.LUCENE_30))
        queryParser.setDefaultOperator(lucene.QueryParser.Operator.AND)
        queryObj = queryParser.parse(query)
        hits = indexSearcher.search(queryObj, settings.LDT_MAX_SEARCH_NUMBER)
    
        res = []
        for hit in hits.scoreDocs:
            doc = indexSearcher.doc(hit.doc)
            res.append({"external_id":doc.get("external_id"), "title":doc.get("title")})
        indexSearcher.close()
        return res

    def query_all(self, query):        
        return self.query("all", query)
    

