#!/usr/bin/python3
import enchant
from sys import argv
from random import shuffle, choice
d = enchant.Dict('en-US')
a = "abcdefghijklmnopqrstuvwxyz"
if len(argv) == 3:
    print("No parameters passed, creating empty 5-letter guess...")
    word = ""
    for i in range(0,5):
        word+=choice(a)
    word = list(word)
    shuffle(word)
    word = "".join(word)
    while not d.check(word):
        word = ""
        for i in range(0,5):
            word+=choice(a)
        word = list(word)
        shuffle(word)
        word = "".join(word)
    print("~w " + word)
elif len(argv) == 4:
    word = list(argv[3])
    for i in range(0,len(word)):
        if word[i] == "_":
            word[i] = choice(a)
    word = "".join(word)
    while not d.check(word):
        word = list(argv[3])
        for i in range(0,len(word)):
            if word[i] == "_":
                word[i] = choice(a)
        word = "".join(word)
    print("~w " + word)
elif len(argv) == 5:
    word = list(argv[3])
    lett = list(argv[4])
    if lett[0] != "-":
       shuffle(lett)
       j = 0
       for i in range(0,len(word)):
            if word[i] == "_":
                if choice([True, False]) and j < len(lett):
                    word[i] = lett[j]
                    j+=1
                else:
                    word[i] = choice(a)
       word = "".join(word)
       while not d.check(word):
           word = list(argv[3])
           lett = list(argv[4])
           shuffle(lett)
           j = 0
           for i in range(0,len(word)):
               if word[i] == "_":
                   if choice([True, False]) and j < len(lett):
                       word[i] = lett[j]
                       j+=1
                   else:
                       word[i] = choice(a)
           word = "".join(word)
    else:
        lett.pop(0)
        a = list(a)
        for i in list(lett):
            a.remove(i.lower()) 
        a = ''.join(a)
        for i in range(0,len(word)): 
            if word[i] == "_": 
                word[i] = choice(a)
        word = "".join(word) 
        while not d.check(word): 
            word = list(argv[3]) 
            for i in range(0,len(word)): 
                if word[i]  == "_":
                   word[i] = choice(a)
            word = "".join(word)
    print("~w " + word)
elif len(argv) == 6:
    word = list(argv[3])
    lett = list(argv[4])
    filter = list(argv[5])
    a = list(a)
    for i in list(filter):
        a.remove(i.lower()) 
    ''.join(a)
    shuffle(lett)
    j = 0
    for i in range(0,len(word)):
         if word[i] == "_":
             if choice([True, False]) and j < len(lett):
                 word[i] = lett[j]
                 j+=1
             else:
                 word[i] = choice(a)
    word = "".join(word)
    while not d.check(word):
        word = list(argv[3])
        lett = list(argv[4])
        shuffle(lett)
        j = 0
        for i in range(0,len(word)):
            if word[i] == "_":
                if choice([True, False]) and j < len(lett):
                    word[i] = lett[j]
                    j+=1
                else:
                    word[i] = choice(a)
        word = "".join(word)
    print("~w " + word)