"""
------------------------------------------------------------------------------
--                                                                          --
--                                 LAB 04                                   --
--                                                                          --
--                             L A B 0 4 . P Y                              --
--                                                                          --
------------------------------------------------------------------------------
-- <Your Name Here>                                                         --
--                                                                          --
-- <Class Name Here>                                                        --
--                                                                          --
------------------------------------------------------------------------------
-- This file contains a simple GUI program to calculate an ISP billing      --
-- rate.                                                                    --
--                                                                          --
------------------------------------------------------------------------------
"""

from Tkinter import *

class Dialog(Toplevel):
    def __init__(self, parent, title, text):

        Toplevel.__init__(self, parent)
        self.transient(parent)

        if title:
            self.title(title)

        self.parent = parent

        self.result = None

        body = Frame(self)
        self.initial_focus = self
        body.pack(padx=5, pady=5)

        Message(self, padx = 0, pady = 0, justify = LEFT, aspect = 600, text = text).pack(side = TOP)

        self.buttonbox()

        self.grab_set()

        if not self.initial_focus:
            self.initial_focus = self

        self.protocol("WM_DELETE_WINDOW", self.cancel)

        self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
                                  parent.winfo_rooty()+50))

        self.initial_focus.focus_set()

        self.wait_window(self)

    def buttonbox(self):
        box = Frame(self)

        w = Button(box, text="OK", width=10, command=self.cancel, default=ACTIVE)
        w.pack(side=LEFT, padx=5, pady=5)

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

        box.pack()

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

def bill_amount(data, base):
    return 0

class BillCalculator(Frame):
    def clear_entries(self):
        self.rate.delete(0, END)
        self.data.delete(0, END)

    def calculate_bill(self):
        try:
            base_rate = float(self.rate.get())
        except:
            Dialog(self, 'Invalid Input', 'Rate must be a vaild dollar amount.')
            return

        try:
            data_transferred = int(self.data.get())
        except:
            Dialog(self, 'Invalid Input', 'Data must be a valid number of megabytes (rounded up to the nearest meg - no fractions.)')
            return

        Dialog(
            self,
            'Your Bill',
            '%d MB at a base rate of $%.2f will cost $%.2f' % (data_transferred, base_rate, bill_amount(data_transferred, base_rate)))

    def __init__(self, parent = None):
        Frame.__init__(self, parent)
        self.pack()

        desc = Message(self, padx = 0, pady = 0, justify = LEFT, aspect = 600)
        desc['text'] = 'To find out your total bill, enter a base rate and an amount of data transferred.'
        desc.grid(row = 0, column = 0, columnspan = 3)

        Label(self, text = 'Rate ($):').grid(row = 1, column = 0)
        self.rate = Entry(self)
        self.rate.grid(row = 1, column = 1, columnspan = 2)

        Label(self, text = 'Data (MB):').grid(row = 2, column = 0)
        self.data = Entry(self)
        self.data.grid(row = 2, column = 1, columnspan = 2)

        self.calculate = Button(self)
        self.calculate['text'] = 'Calculate'
        self.calculate['command'] = self.calculate_bill
        self.calculate.grid(row = 3, column = 0)

        self.clear = Button(self)
        self.clear['text'] = 'Clear'
        self.clear['command'] = self.clear_entries
        self.clear.grid(row = 3, column = 1)

        self.quit = Button(self)
        self.quit['text'] = 'Quit'
        self.quit['command'] = exit
        self.quit.grid(row = 3, column = 2)

root = Tk()
root.title('ISP Billing Calculator')
app = BillCalculator(root)
app.mainloop()
