import string
import random
import copy
import re

RAW1 = "What you're supposed to do when you don't like a thing is change it. If you can't change it, change the way you think about it. Don't complain."
RAW2 = "Thomas Edison's last words were It's very beautiful over there. I don't know where there is, but I believe it's somewhere, and I hope it's beautiful."
RAW3 = "Yes: I am a dreamer. For a dreamer is one who can only find his way by moonlight, and his punishment is that he sees the dawn before the rest of the world"
RAW4 = "I finally understood what true love meant...love meant that you care for another person's happiness more than your own, no matter how painful the choices you face might be."
RAW5 = "Books are the quietest and most constant of friends; they are the most accessible and wisest of counselors, and the most patient of teachers."
RAW6 = "So I walked back to my room and collapsed on the bottom bunk, thinking that if people were rain, I was drizzle and she was a hurricane."
RAW7 = "A great book should leave you with many experiences, and slightly exhausted at the end. You live several lives while reading."
RAW8 = "Fate is like a strange, unpopular restaurant filled with odd little waiters who bring you things you never asked for and don't always like."

RAWS = [RAW1, RAW2, RAW3, RAW4, RAW5, RAW6, RAW7, RAW8]

def chi(text1, text2, i, j):
    count1, count2 = [0]*26, [0]*26

    for c in text1:
        count1[ord(c) - 97] += 1
    for c in text2:
        count2[ord(c) - 97] += 1

    total1, total2, chi = 0, 0, 0

    for c in range(26):
        n, m = count1[c], count2[c]

        total1 += n
        total2 += m
        chi += (n * m)

    chi = (chi / (total1 * total2))

    print("Chi test {} vs. {}: {}".format(i + 1, j + 1, round(chi, 4)))

def gen_alphabets():
    alphabet = list(string.ascii_lowercase)
    alphabets = []
    for i in range(3):
        random.shuffle(alphabet)
        alphabets.append(copy.deepcopy(''.join(alphabet)))
        alphabets.append(copy.deepcopy(''.join(alphabet)))
    for i in range(2):
        random.shuffle(alphabet)
        alphabets.append(copy.deepcopy(''.join(alphabet)))
    return alphabets

def clean(message, regex):
    return regex.sub('', message.lower())

def clean_all(messages, regex):
    new_messages = []
    for m in messages:
        new_messages.append(clean(m, regex))
    return new_messages

def substitute(message, alphabet):
    new_message = []
    for c in message:
        new_message.append(alphabet[ord(c) - 97])
    return ''.join(new_message)

def substitute_all(messages, alphabets):
    new_messages = []
    for i in range(8):
        cipher = substitute(messages[i], alphabets[i])
        new_messages.append(cipher)
    return new_messages

if __name__ == "__main__":
    #regex = re.compile('[^a-z]')
    #messages = clean_all(RAWS, regex)
    #alphabets = gen_alphabets()
    #ciphers = substitute_all(messages, alphabets)

    #for i, c in enumerate(ciphers):
    #    print("RAW{}: {}".format(i+1, c.upper()))
    ciphers = [x.lower() for x in [
        "WZXBSBXYLBYGVVFZSQEYTOMZMGQHMBSBYUFBMQMTRMSFSRMUMRMTBYUMGUXFZSUIEYTXYLXYLZGRMCMMUOXETSMUBTMDVSMBAZGTVYFFMFZGFSUSFQMVESQGFTMOMUBYLQFZSUI",
        "CFAMRRSORAMQAOZNSOVRSRIVVQDVAOCZAMFCRISPRDSCMDSEVXSPRSMRIVVQDVWSPOVVANNBCMQOSTRICMDOWSPZAMROVVTXSYRIVZVMRVX",
        "HFHCAPHBCUETWYEBEQWYTWNQDSMHUHRWUTMBWQUPHCPHBCUGPTESHCEXBNJUPQEHGPQGTEPUQETWYBMGBAEOTWIBEQWYBUUPHUQRNPQOBMQFHCHFHCAQWHXHNQDHEBSQHU",
        "OVTDCGIHTPDFZPAZKGGJWHPTDJOPAAETSHCPDKSGEZPACGIGISZETYSWHPTDJOPAAETPDSVTNTAADTMSSGCGIYZCPDKJZUDSVZSOZYWID",
        "MWKXMQJMWGNOEMQDZHKRICNGZQJRDKIMIFQZPFGQIFNMQFIBZNIBZXTGOOCMOOCFNZMEBFIBZNSMXDZIZSPFNMNGOXSMXDZMIIBZTNFQWIGSZSMXDZIFFOMIZFNSMXDZCFNZLZN",
        "PGKRJFAYEKZFAESXRLYRXZKLXEDYRGFAEBKKUBDXCKYBKZFRHPGKRJFABXSFRYEKHGFSZXRHKEYBKZFRHBKKUBDXCKYRGFAESGYSBEKDYSXMXSJ",
        "HMHBWQCGBECTYPUEPHWHFHCMQQJHIWTNHEPHMQQJHIMTJHBCUBWIBCUGBEWUERSSQEHIUQMQQJWTNHTUGBEERSSQEHIUQDBJHAQROHHMEQDHUPTWY",
        "PGKRXSZFUKBHFPRSFXSXDKSSGKUSGXRCPGYSSGKJPYRSXWSGKJZYEKKRFALGSFIFSGKEPXSGPGYSXHFSGKRXUYDEKYHJIKSSKESGYRSGKU"
    ]]

    for i in range(8):
        for j in range(i + 1, 8):
            chi(ciphers[i], ciphers[j], i, j)



