# !Currency.py.ConvrtW/py

import string
import swi
import wimp
import Rates
import Update


window_dp = menu_convert = None
convert_windows = []


class convert_window(wimp.window):

    def __init__(self):
        global window_dp, menu_convert
        wimp.window.__init__(self, 'convert')
        self.pane = _pane(self)
        convert_windows.append(self)
        if window_dp is None:
            window_dp = dp_window()
            menu_convert = wimp.menu('convert', ('task', 'update', ('dp', window_dp)))
        self.init()

    def remove(self):
        wimp.window.remove(self)
        self.pane.remove()
        convert_windows.remove(self)

    def redraw_start(self):
        # Font_FindFont
        self.font = swi.swi(0x40081, '.sii00;i', 'Homerton.Medium', 160, 160)
        # ColourTrans_SetFontColours
        swi.swi(0x4074f, 'iiii', self.font, 0xffffff00, 0x00000000, 14)
        
    def redraw_stop(self):
        # Font_LoseFont
        swi.swi(0x40082, 'i', self.font)

    def open(self, minx, miny, maxx, maxy, scrollx, scrolly, behind):
        self.pane.open(minx, maxy - 80, maxx, maxy, 0, 0, behind)
        wimp.window.open(self, minx, miny, maxx, maxy, scrollx, scrolly, self.pane.handle)
        bb = swi.block(20)
        bb[0] = self.handle
        # Wimp_GetWindowState
        swi.swi(0x400cb, '.b', bb)
        if bb[1] != minx or bb[3] != maxx or bb[4] != maxy:
            self.pane.open(bb[1], bb[4] - 80, bb[3], bb[4], 0, 0, behind)

    close = remove

    def click(self, icon, buttons, x, y, sx, sy):
        if buttons == 2:
            window_dp.parent = self
            window_dp.icon_text(0, str(self.dp))
            reopen = 1
            while reopen:
                selection, reopen = menu_convert.menu(sx - 64, sy)
                if selection is None:
                    pass
                elif selection[0] == 0:
                    Update.update()
                    reopen = 0
                elif selection[0] == 1:
                    window_dp.open_as_menu()
                    reopen = 0
        else:
            wimp.window.click(self, icon, buttons, x, y, sx, sy)
    
    def init(self):
        self.dp = 3
        c = 0
        for currency in Rates.currencies(wimp.lookup('format')):
            self.add_content(_line(self, c, currency))
            c = c + 1
        self.set_extent(0, 0, 800, 80 + c * 32)
        self.title(wimp.lookup('title') % Rates.date())
        self.table = [1.0]
        self.set_conv(0)
        self.convert()

    def set_conv(self, no):
        self.conv_from = no
        self.pane.icon_text(0, Rates.currency(wimp.lookup('format'), self.conv_from))
        self.pane.icon_text(1, '%.*f' % (self.dp, self.table[no]))
    
    def convert(self):
        a = self.pane.icon_text(1)
        if not '.' in a:
            a = a + '.0'
        try:
            amount = eval(a)
        except SyntaxError:
            return 0
        self.table = Rates.convert(self.conv_from, amount)
        return 1


class _pane(wimp.window):

    def __init__(self, parent):
        wimp.window.__init__(self, 'pane')
        self.parent = parent
    
    def key(self, icon, key):
        if key in range(48, 58) + [0x8, 0x7f, 0x18b]:
            if self.parent.convert():
                self.parent.refresh(500, 80, 800, 10000)
        elif key in (0x18e, 0x18f, 0x19e, 0x19f, 0x1ae, 0x1af):
            bb = swi.block(20)
            bb[0] = self.parent.handle
            swi.swi('Wimp_GetWindowState', '.b', bb)
            if key == 0x18e:
                scroll = bb[6] - 32
            elif key == 0x18f:
                scroll = bb[6] + 32
            elif key == 0x19e:
                scroll = bb[6] - (bb[4] - bb[2])
            elif key == 0x19f:
                scroll = bb[6] + (bb[4] - bb[2])
            elif key == 0x1ae:
                scroll = -0x7fffffff
            elif key == 0x1af:
                scroll = 0
            self.parent.open(bb[1], bb[2], bb[3], bb[4], bb[5], scroll, bb[7])
        elif key == 0x1a2:
            self.parent.close()
        else:
            swi.swi('Wimp_ProcessKey', 'i', key)
    

class _line(wimp.content):

    def __init__(self, w, no, name):
        wimp.content.__init__(self, w, 12, 80 + no * 32, 788, 112 + no * 32)
        self.no = no
        self.name = string.replace(name, ' ', chr(160))

    def redraw(self, ox, oy):
        # OS_Plot
        swi.swi(0x45, '4i0', ox + 784)
        # Font_Paint
        swi.swi(0x40086, 'isiii', self.window.font, '%s %.*f' % (self.name, self.window.dp, self.window.table[self.no]), 0x311, ox + 16, oy - self.pos[3] + 8)

    def click(self, buttons, x, y):
        self.window.set_conv(self.no)
        self.window.pane.put_caret(1)
        return 1


class dp_window(wimp.window):
    
    def __init__(self):
        wimp.window.__init__(self, 'dp')
    
    def key(self, icon, key):
        if key == 0xd:
            self.click(3, 4, 0, 0, 0, 0)
        elif key == 0x18e:
            self.click(1, 4, 0, 0, 0, 0)
        elif key == 0x18f:
            self.click(2, 4, 0, 0, 0, 0)
        else:
            swi.swi('Wimp_ProcessKey', 'i', key)

    def click(self, icon, buttons, x, y, sx, sy):
        if icon == 1:
            self.icon_crement(0, 0, 9, -1 + 2 * (buttons == 1))
        elif icon == 2:
            self.icon_crement(0, 0, 9, 1 - 2 * (buttons == 1))
        elif icon == 3:
            self.parent.dp = eval(self.icon_text(0))
            self.parent.refresh(500, 80, 800, 0x7fffffff)
            if buttons != 1:
                wimp.close_menus()


def update():
    for w in convert_windows:
        w.contents = []
        w.init()
        w.refresh(0, 80, 800, 0x7fffffff)
