"""
------------------------------------------------------------------------------
--                                                                          --
--                                 LAB 05                                   --
--                                                                          --
--                           G O L D L I B . P Y                            --
--                                                                          --
------------------------------------------------------------------------------
-- <Your Name Here>                                                         --
--                                                                          --
-- <Class Name Here>                                                        --
--                                                                          --
------------------------------------------------------------------------------
-- This file contains a small app that builds 'madlibs' out of a famous     --
-- bit of dialog between James Bond and Auric Goldfinger in the movie       --
-- Goldfinger.                                                              --
--                                                                          --
------------------------------------------------------------------------------
"""

from Tkinter import *
import tkFileDialog
import tkSimpleDialog
import tkMessageBox

class GoldlibMainWindow(Frame):
    """
    The main window of the app.  Has buttons for each function it performs
    """
    def __init__(self, parent = None):
        Frame.__init__(self, parent)
        self.pack()

        self.verb_phrases = []

        self.make_quote = Button(self, height=1, width=17)
        self.make_quote['text'] = 'Make Quote'
        self.make_quote['command'] = self.say_quote
        self.make_quote.grid(row = 0, column = 0)

        Label(self, text = ' ').grid(row = 1, column = 0)

        self.read_list = Button(self, height=1, width=17)
        self.read_list['text'] = 'Read List'
        self.read_list['command'] = self.read_from_file
        self.read_list.grid(row = 2, column = 0)

        self.write_list = Button(self, height=1, width=17)
        self.write_list['text'] = 'Write List to file'
        self.write_list['command'] = self.write_to_file
        self.write_list.grid(row = 3, column = 0)
        
        self.add_item = Button(self, height=1, width=17)
        self.add_item['text'] = 'Add verb to List'
        self.add_item['command'] = self.add_verb
        self.add_item.grid(row = 4, column = 0)

        self.remove_item = Button(self, height=1, width=17)
        self.remove_item['text'] = 'Remove verb from List'
        self.remove_item['command'] = self.remove_verb
        self.remove_item.grid(row = 5, column = 0)

        self.view_list = Button(self, height=1, width=17)
        self.view_list['text'] = 'View list'
        self.view_list['command'] = self.show_list
        self.view_list.grid(row = 6, column = 0)

        self.quit = Button(self, height=1, width=17)
        self.quit['text'] = 'Quit'
        self.quit['command'] = exit
        self.quit.grid(row = 7, column = 0)

    def read_from_file(self):
        """
        Asks the user for a file name to read a list of verb phrases from.
        """
        fname = tkFileDialog.askopenfilename()

        if fname == '' or type(fname) not in (str, unicode):
            return

        print fname

    def write_to_file(self):
        """
        Asks the user for a file name to write the current list verb phrases to.
        """
        fname = tkFileDialog.asksaveasfilename()

        if fname == '' or type(fname) not in (str, unicode):
            return

        print type(fname)
        print fname

    def add_verb(self):
        """
        Asks the user to enter a new verb phrase and then adds that to the list.
        """
        new_verb = tkSimpleDialog.askstring('New Verb', 'Enter a Verb Phrase:', parent=self)

        if new_verb == '' or type(new_verb) not in (str, unicode):
            return

        print new_verb

    def remove_verb(self):
        """
        Asks the user to enter a verb phrase and then removes that phrase from
        the list if it exists.
        """
        doomed_verb = tkSimpleDialog.askstring('Remove Verb', 'Enter a Verb Phrase to remove.  If the phrase is in the list, it will be deleted:', parent=self)

        if doomed_verb == '' or type(doomed_verb) not in (str, unicode):
            return

        if tkMessageBox.askokcancel('Are you sure?', 'Are you sure you want to remove %s from the list?' % (doomed_verb)):
            print doomed_verb

    def say_quote(self):
        """
        Builds a quote by grabbing two random verb phrases from the list of
        phrases and constructs Bond's quote using one, Goldinger's using the
        other.
        """
        bond_line = 'Do you expect me to %s?' % ('talk')
        goldfinger_line = 'No Mr. Bond.  I expect you to %s.' % ('die')

        tkMessageBox.showinfo(
            'Quote',
            '<James Bond> %s\n\n<Auric Goldfinger> %s' % (bond_line, goldfinger_line))

    def show_list(self):
        """
        Opens a new window which displays in alphabetical order all of the verb
        phrases currently in the list
        """
        ListWindow(self.verb_phrases, self)

class ListWindow(Toplevel):
    """
    A window which displays a list of items to the user
    """
    def __init__(self, items, parent = None):
        Toplevel.__init__(self, parent)
        self.transient(parent)
        self.parent = parent

        body = Frame(self)
        self.initial_focus = self
        body.pack()

        listbox = Listbox(body)

        for item in items:
            listbox.insert(END, item)

        listbox.grid(row = 0, column = 0)

        ok = Button(body, text='OK', command=self.cancel)
        ok.grid(row = 1, column = 0)

        self.bind("<Return>", self.cancel)
        self.bind("<Escape>", self.cancel)

    def cancel(self, event=None):
        self.parent.focus_set()
        self.destroy()

if __name__ == '__main__':
    root = Tk()
    root.title('Goldlib')
    app = GoldlibMainWindow(root)
    app.mainloop()
