import matplotlib.pyplot as plt
import numpy as np
import math
freq_ru_h = {'О': 0.1097, 'Е': 0.0845, 'А': 0.0801, 'И': 0.0735, 'Н': 0.0670, 'Т': 0.0626, 'С': 0.0547, 'Р': 0.0473,
           'В': 0.0454, 'Л': 0.0440, 'К': 0.0349, 'М': 0.0321, 'Д': 0.0298, 'П': 0.0281, 'У': 0.0262, 'Я': 0.0201,
           'Ы': 0.0190, 'Ь': 0.0174, 'Г': 0.0170, 'З': 0.0165, 'Б': 0.0159, 'Ч': 0.0144, 'Й': 0.0121, 'Х': 0.0097,
           'Ж': 0.0094, 'Ш': 0.0073, 'Ю': 0.0064, 'Ц': 0.0048, 'Щ': 0.0036, 'Э': 0.0032, 'Ф': 0.0026, 'Ъ': 0.0004,
           'Ё': 0.0004}

arr_encrypt_letters = ['о', 'е', 'а', 'и', 'н', 'т', 'с',
                           'р', 'в', 'л', 'к', 'м', 'д', 'п', 'у',
                           'я', 'ы', 'ь', 'г', 'з', 'б', 'ч', 'й',
                           'х', 'ж', 'ш', 'ю', 'ц', 'щ', 'э',
                           'ф', 'ъ', 'ё']

freq_ru_l = {'о': 0.1097, 'е': 0.0845, 'а': 0.0801, 'и': 0.0735, 'н': 0.0670, 'т': 0.0626, 'с': 0.0547,
               'р': 0.0473, 'в': 0.0454, 'л': 0.0440, 'к': 0.0349, 'м': 0.0321, 'д': 0.0298, 'п': 0.0281,
               'у': 0.0262, 'я': 0.0201, 'ы': 0.0190, 'ь': 0.0174, 'г': 0.0170, 'з': 0.0165, 'б': 0.0159,
               'ч': 0.0144, 'й': 0.0121, 'х': 0.0097, 'ж': 0.0094, 'ш': 0.0073, 'ю': 0.0064, 'ц': 0.0048,
               'щ': 0.0036, 'э': 0.0032, 'ф': 0.0026, 'ъ': 0.0004, 'ё': 0.0004}

freq_en_h = {'E': 0.1270, 'T': 0.0906, 'A': 0.0817, 'O': 0.0751, 'I': 0.0697, 'N': 0.0675, 'S': 0.0633, 'H': 0.0609,
           'R': 0.0599, 'D': 0.0425, 'L': 0.0403, 'C': 0.0278, 'U': 0.0276, 'M': 0.0241, 'W': 0.0236, 'F': 0.0223,
           'G': 0.0202, 'Y': 0.0197, 'P': 0.0193, 'B': 0.0149, 'V': 0.0098, 'K': 0.0077, 'X': 0.0015, 'J': 0.0015,
           'Q': 0.0010, 'Z': 0.0005}

freq_en_l = {'e': 0.1270, 't': 0.0906, 'a': 0.0817, 'o': 0.0751, 'i': 0.0697, 'n': 0.0675, 's': 0.0633,
               'h': 0.0609, 'r': 0.0599, 'd': 0.0425, 'l': 0.0403, 'c': 0.0278, 'u': 0.0276, 'm': 0.0241,
               'w': 0.0236, 'f': 0.0223, 'g': 0.0202, 'y': 0.0197, 'p': 0.0193, 'b': 0.0149, 'v': 0.0098,
               'k': 0.0077, 'x': 0.0015, 'j': 0.0015, 'q': 0.0010, 'z': 0.0005}

#stroka = 'GB OR, BE ABG GB OR: GUNG VF GUR DHRFGVBA:JURGURE GVF ABOYRE VA GUR ZVAQ GB FHSSRE GUR FYVATF NAQ NEEBJF BS BHGENTRBHF SBEGHAR, BE GB GNXR NEZF NTNVAFG N FRN BS GEBHOYRF, NAQ OL BCCBFVAT RAQ GURZ? GB OVR: GB FYRRC; AB ZBER; NAQ OL N FYRRC GB FNL JR RAQ GUR URNEG-NPUR NAQ GUR GUBHFNAQ ANGHENY FUBPXF GUNG SYRFU VF URVE GB, GVF N PBAFHZZNGVBA QRIBHGYL GB OR JVFU Q, GB QVR, GB FYRRC; GB FYRRC: CREPUNAPR GB QERNZ: NL, GURER F GUR EHO; SBE VA GUNG FYRRC BS QRNGU JUNG QERNZF ZNL PRZR JURA JR UNIR FUHSSYRQ BSS GUVF ZBEGNY PBVY, ZHFG TVIR HF CNHFR: GURER F GUR ERFCRPG GUNG ZNXRF PNYNZVGL BS FB YBAT YVSR;'
stroka = 'авыпашсвфсв'
result = ''

alf = None
for i in stroka:
    if i in freq_en_h:
        alf = freq_en_h
    elif i in freq_en_l:
        alf = freq_en_l
    elif i in freq_ru_h:
        alf = freq_ru_h
    elif i in freq_ru_l:
        alf = freq_ru_l
    break

text_only = ''
for i in stroka:
    if i in alf:
        text_only += i

freq = {}
for i in text_only:
    if i in freq:
        freq[i] = freq[i] + 1
    else:
        freq[i] = 1

freq = list(freq.items())
freq.sort(key=lambda chastota: chastota[1], reverse = True)
print(freq)

freq_new = []
for i in range(len(freq)):
    freq_new1 = []
    freq_new1.append(freq[i][0])
    freq_new1.append(round(int(freq[i][1]) / len(alf), 4))
    freq_new.append(freq_new1)
print(freq_new)

x = []
y = []
for i in range(len(freq_new)):
    x.append(freq_new[i][0])
    y.append(freq_new[i][1])
fig, ax = plt.subplots()
ax.bar(x, y)
plt.show()

dictionary = {}
arr_encrypt_text = []
for i in stroka:
    dictionary[i] = zip(freq_new[i][0], arr_encrypt_letters[i])
for i in stroka:
    arr_encrypt_text.append(dictionary.get(i))
print(arr_encrypt_text)
print(stroka)

"""output_text = ''
for i in range(len(stroka)):
    if stroka[i] in alf:
        output_text += str(alf[stroka[i]])
    else:
        output_text += stroka[i]
print(output_text)"""
#for i in range(len(freq_new)):
    #for

"""for i in range(len(freq_new)):
    x = np.arange(0, math.pi * 2, 0.05)
    y = np.sin(x)
    plt.plot(x, y)
    x = freq_new[i][0]
    plt.hist(x, density=False, bins=freq[i][1])
plt.ylabel('Frequency')
plt.xlabel('Symbol')
plt.show()"""



#d_freqs = {ch[0]: round(ch[1] / len(text_only), 4) for ch in freq}

