# -*- coding: utf-8 -*-
'''
Created on 20.10.2014

@author: Simon Gwerder
'''

from utilities import utils
from utilities.configloader import ConfigLoader


class TagResults:

    cl = ConfigLoader()
    results = []

    def __init__(self, rdfGraph, rawResults):
        self.results = []
        self.fillResultList(rdfGraph, rawResults)
        #self.results = sorted(self.results, reverse=True, key=self.sortKey)
        self.results = self.sortResults(self.results) # sorting by section


    def getResults(self):
        return self.results

    def sortKey(self, tag):
        return int(tag['countAll'])

    def sortResults(self, unsortedResults):

        byTagPrefLabel = []
        byTermPrefLabel = []
        byTermAltLabel = []
        byTermBroader = []
        byTermNarrower = []
        byTagScopeNote = []
        byNotUsedTags = []

        sortedResults = []

        for result in unsortedResults:

            if not 'searchMeta' in result: return unsortedResults
            
            # TODO: Not showing depricated tags needs to be discussed
            if self.isTagDepricated(result):
                continue
            
            # TODO: Place of unused tags needs to be discussed
            #if result['isTag'] and not self.isTagUsed(result):
            #    byNotUsedTags.append(result)
            #    continue

            searchMeta = result['searchMeta']

            hasTermBroader = 'termBroader' in searchMeta
            hasTermNarrower = 'termNarrower' in searchMeta
            hasTagPrefLabel = 'tagPrefLabel' in searchMeta
            hasTermPrefLabel = 'termPrefLabel' in searchMeta
            hasTermAltLabel = 'termAltLabel' in searchMeta
            hasTagScopeNote = 'tagScopeNote' in searchMeta

            if hasTermBroader and not hasTagPrefLabel and not hasTermPrefLabel and not hasTermAltLabel:
                byTermBroader.append(result)
                continue
            if hasTermNarrower and not hasTagPrefLabel and not hasTermPrefLabel and not hasTermAltLabel:
                byTermNarrower.append(result)
                continue
            if hasTagPrefLabel:
                byTagPrefLabel.append(result)
                continue
            if hasTermPrefLabel:
                byTermPrefLabel.append(result)
                continue
            if hasTermAltLabel:
                byTermAltLabel.append(result)
                continue
            if hasTagScopeNote:
                byTagScopeNote.append(result)
                continue

        sortedResults.extend( sorted(byTagPrefLabel, reverse=True, key=self.sortKey) )
        sortedResults.extend( sorted(byTermPrefLabel, reverse=True, key=self.sortKey) )
        sortedResults.extend( sorted(byTermAltLabel, reverse=True, key=self.sortKey) )
        sortedResults.extend( sorted(byTermBroader, reverse=True, key=self.sortKey) )
        sortedResults.extend( sorted(byTermNarrower, reverse=True, key=self.sortKey) )
        sortedResults.extend( sorted(byTagScopeNote, reverse=True, key=self.sortKey) )
        sortedResults.extend( sorted(byNotUsedTags, reverse=True, key=self.sortKey) )

        return sortedResults

    def isTagUsed(self, tag):
        if tag['node']['use'] or tag['way']['use'] or tag['area']['use'] or tag['relation']['use']:
            return True
        else:
            return False
        
    def isTagDepricated(self, result):
        #hasZeroCount = (tag['node']['count'] == 0 and tag['way']['count'] == 0 and tag['area']['count'] == 0 and tag['relation']['count'] == 0)
        if (result['isTag'] and not self.isTagUsed(result) and result['countAll'] == 0) or (result['isKey'] and result['countAll'] == 0):
            return True
        return False