#!/usr/bin/python

# working only from API_Reference_for_Python_1_0.pdf and Examples/applicationskeleton.py,
# this took a little under an hour from empty buffer to actually useful...

import e32
import appuifw

# import RavsLib?
e32.ao_yield()

# this is the mary's menu framework.  Duplicate the palm app at least,
# then support getting menu updates...


class ravscalc:
    def exit_key_handler(self):
        # this is called from some symbian UI thread, and can only signal our UI 
        self.exit_handler_selected = 1
        self.main_thread_done_yet_lock.signal()

    def cleanup_before_exit(self):
        # mmmravs doesn't actually have any cleanup, but might post someday
        pass

    def run_app_until_done(self):
        try:
            while not self.exit_handler_selected:
                self.main_thread_done_yet_lock.wait()
                self.react_and_update_display()
        finally:
            self.cleanup_before_exit()

    def __init__(self):
        # use the special lock that doesn't lock against the rest of the UI!
        self.main_thread_done_yet_lock = e32.Ao_lock()

        # set up the base values
        self.initialize_data()

        # set up the frame/decorations
        appuifw.app.title = u'MMM RAVS' # why do the examples bother saving the title?
        # because the app "returns" to the interpreter if not py2sis'ed
        self.exit_handler_selected = None
        appuifw.app.exit_key_handler = self.exit_key_handler

        # set up the rest
        # function can't be None, list can't be empty
        appuifw.app.body = appuifw.Listbox([u"(waiting...)"], self.ignore)
        # twocol doesn't really work, but remember how
        # appuifw.app.body = appuifw.Listbox([(u"(waiting", u"...)")], self.ignore)
        appuifw.app.menu = [
            (u"Add 1/2 ravs", self.wrap(self.add_half_ravs)),
            (u"Add rice", self.wrap(self.add_rice)),
            (u"Add soup", self.wrap(self.add_soup)),
            (u"Add KPCD", self.wrap(self.add_kpcd)),
            (u"Add fried bread", self.wrap(self.add_bread)),
            (u"Select 5% tax + 15% tip", self.wrap(self.handle_cheap)),
            (u"Select 5% tax + 20% tip", self.wrap(self.handle_fancy)),
            (u"Clear", self.wrap(self.initialize_data)),
            (u"About", self.display_product_info),
            ]


    def initialize_data(self):
        self.tax = 0.05
        self.handle_fancy()
        self.food_cost = 0.0
        
    def ignore(self):
        # we're just using the Listbox as a form/display
        pass

    def wrap(self, f):
        def wrapped_f():
            f()
            self.react_and_update_display()
        return wrapped_f

    def handle_cheap(self):
        self.tip = 0.15
    def handle_fancy(self):
        self.tip = 0.20
    def add_half_ravs(self):
        self.food_cost += 3.95
    def add_rice(self):
        self.food_cost += 1.00
    def add_soup(self):
        self.food_cost += 4.95
    def add_kpcd(self):
        self.food_cost += 8.95
    def add_bread(self):
        self.food_cost += 1.35

    def react_and_update_display(self):
        tax = self.tax * self.food_cost
        tip = self.tip * self.food_cost
        total = self.food_cost + tax + tip
        new_contents_onecol = [
            u'Food: %.2f' % self.food_cost,
            u'Tax: %.2f' % tax,
            u'Tip: %.2f' % tip,
            u'-' * 16,
            u'Total: %.2f' % total,
            ]
        # twocol doesn't really work for this interface
        new_contents_twocol = [
            (u'Food', u'%.2f' % self.food_cost),
            (u'Tax', u'%.2f' % tax),
            (u'Tip', u'%.2f' % tip),
            (u'-' * 8, u'-' * 8), 
            (u'Total', u'%.2f' % total),
            ]
        appuifw.app.body.set_list(new_contents_onecol)

    def display_product_info(self):
        appuifw.note(u'MMMRAVS $Id$', "info")
        
def mmmravs():
    app = ravscalc()
    # could try to download update here
    app.run_app_until_done()
    # could also try-finally, but run_app_until_done is already doing that

if __name__ == "__main__":
    mmmravs()