"""
------------------------------------------------------------------------------
--                                                                          --
--                              CLASS PROJECT                               --
--                                                                          --
--                            C A L C U I . P Y                             --
--                                                                          --
------------------------------------------------------------------------------
-- <Your Name Here>                                                         --
--                                                                          --
-- <Class Name Here>                                                        --
--                                                                          --
------------------------------------------------------------------------------
-- This file contains the User interface for the calculator project.        --
--                                                                          --
------------------------------------------------------------------------------
"""

from Tkinter import *
import tkMessageBox
import functools
import calcfunc

class CalculatorFrame(Frame):
    button_names = [
        '^',
        '/',
        '*',
        '-',
        '7',
        '8',
        '9',
        '+',
        '4',
        '5',
        '6',
        '(',
        '1',
        '2',
        '3',
        ')',
        '0',
        '.',
        'Ans',
        'Rec'
    ]

    def echo_text(self, txt):
        self.input_panel.insert(END, txt)

    def call_calc(self):
        answer, self.stored_answers = calcfunc.calculate_and_store(
            self.input_panel.get(),
            self.stored_answers)

        self.input_panel.delete(0, END)
        self.input_panel.insert(END, str(answer))

    def show_stored(self):
        tkMessageBox.showinfo(
                'Recent Calculations',
                calcfunc.recall_string(self.stored_answers))

    def on_key_press(self, event):
        if event.keysym in ('Return', 'KP_Enter'):
            self.call_calc();

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

        self.stored_answers = []

        self.input_panel = Entry(self)
        self.input_panel.grid(row = 0, column = 0, columnspan = 4)

        self.buttons = {}
        row = 1
        col = 0
        for name in CalculatorFrame.button_names:
            self.buttons[name] = Button(self, height = 1, width = 1)
            self.buttons[name]['text'] = name
            self.buttons[name].grid(row = row, column = col)
            self.buttons[name]['command'] = functools.partial(self.echo_text, txt = name)
            
            col += 1
            if col == 4:
                col = 0
                row += 1

        self.buttons['Ans']['command'] = self.call_calc
        self.buttons['Rec']['command'] = self.show_stored
        self.bind_all('<KeyPress>', self.on_key_press)
