# -*- coding: utf-8 -*-


class BruteDictionaryIter(object):


    def __init__(
        self,
        maxLength  = 3,
        characters = None,
        lowercase  = None,
        uppercase  = None,
        numbers    = None,
        specials   = None
    ):
        if(characters):
            self.characters = characters

        else:
            self.characters = ''
            
            if(lowercase):
                self.characters += 'abcdefghijklmnopqrstuvwxyz'

            if(uppercase):
                self.characters += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

            if(numbers):
                self.characters += '0123456789'

            if(specials):
                self.characters += '\\/\'"@#$%&/()[]{}=?¿!¡+-*_.:,;^'

        self.maxLength = maxLength
        self.currentIndexStack = 0

        # Stack of indexes of characters
        self.stack = []
        for n in range(0, maxLength):
            self.stack.append(None)

        # Statics
        self.totalWords       = (len(self.characters) ** maxLength)
        self.currentWordIndex = 0

    
    def __iter__(self):

        return self


    def __next__(self):

        # For user statics only
        self.currentWordIndex += 1

        if(
            (not self.characters) or
            (len(self.stack) == 0)
        ):
            raise StopIteration

        if(self.stack[self.currentIndexStack] is None):
            
            # First value
            self.stack[self.currentIndexStack] = 0
        else:
            # Next value
            self.stack[self.currentIndexStack] += 1

        # Overflow?
        if(self.stack[self.currentIndexStack] > (len(self.characters) - 1)):

            # Reset counter
            self.stack[self.currentIndexStack] = 0

            # Sum previous index
            prevIndex = self.currentIndexStack - 1
            while(True):

                # No more previous index?, need append a unity
                if(prevIndex < 0):

                    # Next index
                    self.currentIndexStack += 1

                    if(self.currentIndexStack > (len(self.stack) - 1)):

                        # No more indexes on stack
                        raise StopIteration

                    # First value of next stack
                    self.stack[self.currentIndexStack] = 0

                    break

                else:
                    # Next value of previous index
                    self.stack[prevIndex] += 1

                    # Overflow?
                    if(self.stack[prevIndex] > (len(self.characters) - 1)):

                        self.stack[prevIndex] = 0

                        # Try next with previous index
                        prevIndex -= 1

                    else:
                        # Normal operation
                        break
            

        # Make the final string
        characters = ''

        for indexWord in self.stack:

            # Check for right padding using None
            if(indexWord is None):
                continue

            characters += self.characters[indexWord]

        return characters
