'''
Created on Nov 10, 2014

@author: chorton
'''

class TicketSortKey(object):
    def __init__(self, diff, key1, key2):
        self.diff = diff
        self.flag = [0,1]
        self.outkey = None
        self.prev1 = None
        self.prev2 = None
        self.key1 = key1
        self.key2 = key2
        
    def __call__(self, elem):
        curr1 = self.key1(elem)
        curr2 = self.key2(elem)
        
        if self.prev1 and (self.prev1 == curr1):
            
            if self.prev2 and abs(self.prev2 - curr2) > self.diff:
                self.flag = self.flag[::-1]
        else:
            self.flag = self.flag[::-1]
        
            
        self.prev1 = curr1
        self.prev2 = curr2
        
        output = []
        for item in curr1:
            output.append(item)
            
        output.append(self.flag[0])
        self.outkey = tuple(output)
        
        return self.outkey   
 
