import Tkinter
import ScrolledText
import tkFileDialog
import tkSimpleDialog
import random

def open_command():
    global textWidget
    file = tkFileDialog.askopenfile()
    if file != None:
        contents = file.read()
        textWidget.delete("1.0", ScrolledText.END)
        textWidget.insert(ScrolledText.END, contents)

def save_command():
    global textWidget
    file = tkFileDialog.asksaveasfile()
    if file != None:
        contents = textWidget.get("1.0", ScrolledText.END)
        file.write(contents)
        file.close()
    
def exit_program():
    global root
    root.destroy()

def caesarEncrypt(char, rotateBy):
    return chr((ord(char)+rotateBy)%126+1)

def caesarDecrypt(char, rotateBy):
    return chr((ord(char)-rotateBy-1)%126)

def encrypt(inString, password):
    outString = ""
    index = 0
    for ch in inString:
        ch = caesarEncrypt(ch, ord(password[index]))
        outString = outString + ch
        index = (index+1)%len(password)
    return outString

def decrypt(inString, password):
    outString = ""
    index = 0
    for ch in inString:
        ch = caesarDecrypt(ch, ord(password[index]))
        outString = outString + ch
        index = (index+1)%len(password)
    return outString

def encrypt_command():
    global textWidget
    password = tkSimpleDialog.askstring("Password", "Please Enter your Password")
    if password != None:
        contents = textWidget.get("1.0", ScrolledText.END)
        encrypted = encrypt(contents, password)
        textWidget.delete("1.0", ScrolledText.END)
        textWidget.insert(ScrolledText.END, encrypted)

def decrypt_command():
    global textWidget
    password = tkSimpleDialog.askstring("Password", "Please Enter your Password")
    if password != None:
        contents = textWidget.get("1.0", ScrolledText.END)
        encrypted = decrypt(contents, password)
        textWidget.delete("1.0", ScrolledText.END)
        textWidget.insert(ScrolledText.END, encrypted)

def encrypt_substitution():
    global encrypt_dict
    contents = textWidget.get("1.0", ScrolledText.END)

    encrypted = ""
    for ch in contents:
        encrypted = encrypted + encrypt_dict[ch]
        
    textWidget.delete("1.0", ScrolledText.END)
    textWidget.insert(ScrolledText.END, encrypted)

def decrypt_substitution():
    global decrypt_dict
    contents = textWidget.get("1.0", ScrolledText.END)

    decrypted = ""
    for ch in contents:
        decrypted = decrypted + decrypt_dict[ch]
        
    textWidget.delete("1.0", ScrolledText.END)
    textWidget.insert(ScrolledText.END, decrypted)

if __name__ == "__main__":
    root = Tkinter.Tk()
    textWidget = ScrolledText.ScrolledText(root, width=80, height=50)
    textWidget.pack()

    menuBar = Tkinter.Menu(root, tearoff=0)
    fileMenu = Tkinter.Menu(menuBar, tearoff=0)
    fileMenu.add_command(label="Open", command=open_command)
    fileMenu.add_command(label="Save", command=save_command)
    fileMenu.add_separator()
    fileMenu.add_command(label="Quit", command=exit_program)
    menuBar.add_cascade(label="File", menu=fileMenu)
    encryptMenu = Tkinter.Menu(menuBar, tearoff=0)
    encryptMenu.add_command(label="Encrypt", command=encrypt_command)
    encryptMenu.add_command(label="Decrypt", command=decrypt_command)
    encryptMenu.add_command(label="Encrypt Substitution",
                            command=encrypt_substitution)
    encryptMenu.add_command(label="Decrypt Substitution",
                            command=decrypt_substitution)
    menuBar.add_cascade(label="Encrypt", menu=encryptMenu)

    root.config(menu=menuBar)

    keys = range(1,127)
    values = range(1,127)
    random.shuffle(values)

    encrypt_dict = dict()
    decrypt_dict = dict()
    for (i,j) in zip(keys,values):
        encrypt_dict[chr(i)] = chr(j)
        decrypt_dict[chr(j)] = chr(i)
    
    root.mainloop()
