import crypto_tools
from itertools import cycle


def vigenere_little_doc():
    return "encrypt/decrypt using vigenere cypher"


def vigenere_full_doc():
    return """
    Advanced caesar we change dict on each char
    """


def vigenere_str_to_list(string, vigenere_dict):
    result = list()
    for char in string:
        try:
            result.append(vigenere_dict.index(char))
        except ValueError:
            err_msg = f"There is no {key[inx]} in alphabet"
            raise ValueError(err_msg)
    return result


def vigenere_processing(data, key, lang, encrypt):
    vigenere_dict = crypto_tools.get_param_json_data("alphabets.json", lang)
    num_data = vigenere_str_to_list(data, vigenere_dict)
    num_key = vigenere_str_to_list(key, vigenere_dict)
    dict_size = len(vigenere_dict)
    num_key = cycle(num_key)
    if (encrypt == "encrypt"):
        num_result = [(a + b) % dict_size for a, b in zip(num_data, num_key)]
    else:
        num_result = [
            (a + dict_size - b) % dict_size for a, b in zip(num_data, num_key)
        ]
    result_str = ""
    for val in num_result:
        result_str += vigenere_dict[val]
    return result_str


@crypto_tools.file_manipulation()
def vigenere(data):
    lang = crypto_tools.cterm('input', 'Data language: ', 'ans')
    key = crypto_tools.cterm('input', 'Enter key(str): ', 'ans')
    encrypt = crypto_tools.cterm('input',
                                 'You want encrypt or decrypt: ', 'ans')
    if encrypt != "encrypt" and encrypt != "decrypt":
        raise ValueError("Incorrect action")
    data = crypto_tools.utf_decoder(data)
    return vigenere_processing(data, key, lang, encrypt)


vigenere.little_doc = vigenere_little_doc
vigenere.full_doc = vigenere_full_doc

[((44, 1, 44, 33), 'crypto_tools.file_manipulation', 'crypto_tools.file_manipulation', ({}, {}), '()', False, 'import crypto_tools\n'), ((27, 20, 27, 76), 'crypto_tools.get_param_json_data', 'crypto_tools.get_param_json_data', ({(27, 53, 27, 69): '"""alphabets.json"""', (27, 71, 27, 75): 'lang'}, {}), "('alphabets.json', lang)", False, 'import crypto_tools\n'), ((31, 14, 31, 28), 'itertools.cycle', 'cycle', ({(31, 20, 31, 27): 'num_key'}, {}), '(num_key)', False, 'from itertools import cycle\n'), ((46, 11, 46, 64), 'crypto_tools.cterm', 'crypto_tools.cterm', ({(46, 30, 46, 37): '"""input"""', (46, 39, 46, 56): '"""Data language: """', (46, 58, 46, 63): '"""ans"""'}, {}), "('input', 'Data language: ', 'ans')", False, 'import crypto_tools\n'), ((47, 10, 47, 64), 'crypto_tools.cterm', 'crypto_tools.cterm', ({(47, 29, 47, 36): '"""input"""', (47, 38, 47, 56): '"""Enter key(str): """', (47, 58, 47, 63): '"""ans"""'}, {}), "('input', 'Enter key(str): ', 'ans')", False, 'import crypto_tools\n'), ((48, 14, 49, 72), 'crypto_tools.cterm', 'crypto_tools.cterm', ({(48, 33, 48, 40): '"""input"""', (49, 33, 49, 64): '"""You want encrypt or decrypt: """', (49, 66, 49, 71): '"""ans"""'}, {}), "('input', 'You want encrypt or decrypt: ', 'ans')", False, 'import crypto_tools\n'), ((52, 11, 52, 41), 'crypto_tools.utf_decoder', 'crypto_tools.utf_decoder', ({(52, 36, 52, 40): 'data'}, {}), '(data)', False, 'import crypto_tools\n')]