import PikaUI as ui
from PikaStdLib import MemChecker as mem


calculated = False
result = ui.Container(
    text='0',
    width=120,
    height=30,
    align=ui.ALIGN.TOP_RIGHT,
    pos=(0, -5)
)


class Key(ui.Button):
    def onclick(self, event):
        global calculated
        print('Key:', self.text)
        if result.text == '0' or calculated:
            result.text = ''
            calculated = False
        if self.text == '=':
            result.text = str(eval(result.text))
            calculated = True
        else:
            result.text += self.text
        result.update()

    def __init__(self, text, pos):
        super().__init__(
            text=text,
            align=ui.ALIGN.CENTER,
            pos=pos,
            height=35,
            width=35,
            onclick=self.onclick
        )


class KeyBoard(ui.Container):
    def build(self):
        return [
            Key(text='1', pos=(-60, -60)),
            Key(text='2', pos=(-20, -60)),
            Key(text='3', pos=(20, -60)),
            Key(text='4', pos=(-60, -20)),
            Key(text='5', pos=(-20, -20)),
            Key(text='6', pos=(20, -20)),
            Key(text='7', pos=(-60, 20)),
            Key(text='8', pos=(-20, 20)),
            Key(text='9', pos=(20, 20)),
            Key(text='0', pos=(-60, 60)),
            Key(text='.', pos=(-20, 60)),
            Key(text='=', pos=(20, 60)),
            Key(text='+', pos=(60, -60)),
            Key(text='-', pos=(60, -20)),
            Key(text='*', pos=(60, 20)),
            Key(text='/', pos=(60, 60)),
        ]


class Calculator(ui.Container):
    def build(self):
        return [
            KeyBoard(
                width=200,
                height=200,
                align=ui.ALIGN.BOTTOM_MID
            ),
            ui.Text(
                text='Calculator',
                align=ui.ALIGN.TOP_LEFT
            ),
            result
        ]


class Page1(ui.Page):
    def onclick_next(self, event):
        print('Page1: onclick_next')
        app.pageManager.enter(Page2())
        mem.now()

    def build(self):
        return [
            Calculator(
                width=240,
                height=270,
                pos=(0, 30)
            ),
            ui.Button(
                text='Next',
                align=ui.ALIGN.CENTER,
                pos=(150, 150),
                height=30,
                width=80,
                onclick=self.onclick_next
            )]


class Page2(ui.Page):
    def on_click_back(self, event):
        app.pageManager.back()
        mem.now()

    def build(self):
        return ui.Widget(
            width=400,
            height=200,
            pos=(0, 50)
        ).add(
            ui.Text(
                text='Hello Page2',
                align=ui.ALIGN.CENTER
            ),
            ui.Button(
                text='Back',
                align=ui.ALIGN.CENTER,
                pos=(0, 50),
                height=30,
                width=80,
                onclick=self.on_click_back
            )
        )


app = ui.App()
app.show(Page1())
