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

#   @---------------- LICENCIA ----------------@
#   | Este archivo es parte de la herramienta  |
#   | DicC.                                    |
#   | DicC es un ssoftware libre bajo la       |
#   | Licencia Pública General GNU publicada   |
#   | por la Free Software Fundation. Esta     |
#   | herramienta está bajo esa licencia en    |
#   | la versión 3 o posterior.                |
#   @------------------------------------------@

#   @------------------ INFO ------------------@
#   | DicC es una herramienta creada con el    |
#   | objetivo de ser útil. El autor de DicC   |
#   | no se hace responsable del mal uso que   |
#   | se le pueda dar a esta herramienta.      |
#   @------------------------------------------@

import sys, os
try:
    from characters import *
    from terminal import *
except:
    from .characters import *
    from .terminal import *

# % = Number          \
# @ = Letter           > Normal Mode
# # = Other character /

# % = Number / Letter \
# # = Number / Other   > Combined Mode
# @ = Letter / Other /

# ? = All

def write(words, file):
    f = open(file, "a")
    for w in words:
        f.write(w + "\n")
    f.close()

def check_size(words, final_size, file):
    if final_size != None:
        size = float(os.path.getsize(file)) / 1000 if os.path.isfile(file) \
            else sys.getsizeof(words) / 1000
        if size >= final_size:
            write(words, file)
            print (OK + " Peso maximo del diccioario alcanzado.")
            sys.exit(0)
    else:
        size = sys.getsizeof(words) / 1000000 # Megabytes
        if size > 50:
            write(words, file)
            words = [""]
    return words


def generator_normal(templates, file, final_size = None):
    words = [""]
    for template in templates:
        for ch in template:
            try:
                if ch != "%" and ch != "@" and ch != "#":
                    for i in range(len(words)):
                        words[i] += ch
                        words = check_size(words, final_size, file)
                elif ch == "%":
                    for i in range(len(words)):
                        for num in NUMBERS:
                            words.append(words[i] + num)
                        words = check_size(words, final_size, file)
                elif ch == "@":
                    for i in range(len(words)):
                        for lett in LETTERS:
                            words.append(words[i] + lett)
                        words = check_size(words, final_size, file)
                elif ch == "#":
                    for i in range(len(words)):
                        for oth in OTHERS:
                            words.append(words[i] + oth)
                        words = check_size(words, final_size, file)
                words = check_size(words, final_size, file)
            except MemoryError:
                write(words, file)
                size = float(os.path.getsize(file)) / 1000000 # Convert size to Mb
                print (OK + " El peso del diccionario es: {0} MB.".format(str(size)))
                words = [""]
                continue
            except KeyboardInterrupt:
                break
    print (OK + " Espere a que el programa termine de escribir los datos restantes.")
    write(words, file)
    size = float(os.path.getsize(file)) / 1000000  # Convert size to Mb
    print (OK + " El peso final del diccionario es: {0} MB.".format(str(size)))



def generator_conbined(templates, file, final_size = None):
    words = [""]
    for template in templates:
        for ch in template:
            try:
                if ch != "%" and ch != "@" and ch != "#":
                    for i in range(len(words)):
                        words[i] += ch
                        words = check_size(words, final_size, file)
                elif ch == "%":
                    for i in range(len(words)):
                        for num in NUMBERS:
                            words.append(words[i] + num)
                            words = check_size(words, final_size, file)
                        for lett in LETTERS:
                            words.append(words[i] + lett)
                            words = check_size(words, final_size, file)
                elif ch == "#":
                    for i in range(len(words)):
                        for lett in NUMBERS:
                            words.append(words[i] + lett)
                            words = check_size(words, final_size, file)
                        for oth in OTHERS:
                            words.append(words[i] + oth)
                            words = check_size(words, final_size, file)
                elif ch == "@":
                    for i in range(len(words)):
                        for lett in LETTERS:
                            words.append(words[i] + lett)
                            words = check_size(words, final_size, file)
                        for oth in OTHERS:
                            words.append(words[i] + oth)
                            words = check_size(words, final_size, file)
                words = check_size(words, final_size, file)
            except MemoryError:
                write(words, file)
                size = float(os.path.getsize(file)) / 1000000  # Convert size to Mb
                print (OK + " El peso del diccionario es: {0} MB.".format(str(size)))
                words = [""]
                continue
            except KeyboardInterrupt:
                break
    print (OK + " spere a que el programa termine de escribir los datos restantes.")
    write(words, file)
    size = float(os.path.getsize(file)) / 1000000  # Convert size to Mb
    print (OK + " El peso final del diccionario es: {0} MB.".format(str(size)))


def generator_all(templates, file, final_size = None):
    words = [""]
    for template in templates:
        for ch in template:
            try:
                if ch != "?":
                    for i in range(len(words)):
                        words[i] += ch
                        words = check_size(words, final_size, file)
                else:
                    for i in range(len(words)):
                        for num in NUMBERS:
                            words.append(words[i] + num)
                            words = check_size(words, final_size, file)
                        for lett in LETTERS:
                            words.append(words[i] + lett)
                            words = check_size(words, final_size, file)
                        for oth in OTHERS:
                            words.append(words[i] + oth)
                            words = check_size(words, final_size, file)
            except MemoryError:
                write(words, file)
                size = float(os.path.getsize(file)) / 1000000  # Convert size to Mb
                print (OK + " El peso del diccionario es: {0} MB.".format(str(size)))
                words = [""]
                continue
            except KeyboardInterrupt:
                break
    print (OK + " Espere a que el programa termine de escribir los datos restantes.")
    write(words, file)
    size = float(os.path.getsize(file)) / 1000000  # Convert size to Mb
    print (OK + " El peso final del diccionario es: {0} MB.".format(str(size)))
