from machine import Pin, SPI
import st7789
import utime
import vga1_bold_16x32 as font

# ------------------ CONFIG ------------------
JPY_PER_AUD = 98.22
ROTATION = 1  # set to 0 or 1 depending on how you wired/rotate the display

# nominal LCD logical size for Waveshare 1.14" ST7789
PHYSICAL_W = 135
PHYSICAL_H = 240

# compute working WIDTH/HEIGHT based on rotation
if ROTATION in (0, 2):
    WIDTH = PHYSICAL_W
    HEIGHT = PHYSICAL_H
else:
    # rotation 1 or 3: width/height are swapped for many drivers
    WIDTH = PHYSICAL_H
    HEIGHT = PHYSICAL_W

# ------------------ SPI + LCD ---------------------------------
spi = SPI(1, baudrate=40000000, sck=Pin(10), mosi=Pin(11))
LCD = st7789.ST7789(
    spi,
    PHYSICAL_W,
    PHYSICAL_H,
    reset=Pin(12, Pin.OUT),
    cs=Pin(9, Pin.OUT),
    dc=Pin(8, Pin.OUT),
    backlight=Pin(13, Pin.OUT),
    rotation=ROTATION,
)
LCD.init()
LCD.fill(st7789.BLACK)

# ------------------ INPUT PINS (per Waveshare pinout) ----------
joystick_up = Pin(2, Pin.IN, Pin.PULL_UP)     # GP2
joystick_down = Pin(18, Pin.IN, Pin.PULL_UP)  # GP18
joystick_left = Pin(16, Pin.IN, Pin.PULL_UP)  # GP16
joystick_right = Pin(20, Pin.IN, Pin.PULL_UP) # GP20
joystick_press = Pin(3, Pin.IN, Pin.PULL_UP)  # GP3 (center) - unused
button_a = Pin(15, Pin.IN, Pin.PULL_UP)      # GP15
button_b = Pin(17, Pin.IN, Pin.PULL_UP)      # GP17 (User key B) - clears the amount

# ------------------ STATE -------------------------------------
mode = "JPY"           # when mode=="JPY", left side is editable JPY; otherwise left is AUD
left_value = 0          # editable integer value

# layout helpers
CENTER_X = WIDTH // 2
DIV_W = 2
LEFT_X = 8
RIGHT_X = CENTER_X + 8
LABEL_Y = 6
VALUE_Y = HEIGHT // 2 - 8
HINT_Y = HEIGHT - 40

# ------------------ CONVERSION --------------------------------
def convert_left_to_right(val, mode):
    if mode == "JPY":
        return val / JPY_PER_AUD
    else:
        return val * JPY_PER_AUD

# ------------------ DRAW --------------------------------------
def draw_screen():
    # clear
    LCD.fill(st7789.BLACK)

    # dividing vertical line centered
    x = CENTER_X - (DIV_W // 2)
    LCD.fill_rect(x, 0, DIV_W, HEIGHT, st7789.WHITE)

    # top labels
    # left label (editable unit)
    if mode == "JPY":
        LCD.text(font, "JPY", LEFT_X, LABEL_Y, st7789.CYAN)
        LCD.text(font, "AUD", RIGHT_X, LABEL_Y, st7789.CYAN)
    else:
        LCD.text(font, "AUD", LEFT_X, LABEL_Y, st7789.CYAN)
        LCD.text(font, "JPY", RIGHT_X, LABEL_Y, st7789.CYAN)

    # left editable value (bottom-ish on left)
    LCD.text(font, "%d" % left_value, LEFT_X, VALUE_Y, st7789.YELLOW)

    # converted value on right
    converted = convert_left_to_right(left_value, mode)
    if mode == "JPY":
        # show AUD with 2 decimals
        LCD.text(font, "{:.2f}".format(converted), RIGHT_X, VALUE_Y, st7789.GREEN)
    else:
        # show JPY as integer
        LCD.text(font, "%d" % int(converted), RIGHT_X, VALUE_Y, st7789.GREEN)

# initial draw
draw_screen()

# ------------------ MAIN LOOP ---------------------------------
last_a = 1
last_b = 1

while True:
    # toggle mode when A pressed (edge detect)
    if button_a.value() == 0 and last_a == 1:
        # convert current left_value into the new left_value in the other unit
        if mode == "JPY":
            # left was JPY, switching so left becomes AUD (rounded AUD units)
            mode = "AUD"
            left_value = int(round(left_value / JPY_PER_AUD))
        else:
            mode = "JPY"
            left_value = int(round(left_value * JPY_PER_AUD))
        print("Mode switched to", mode, "left_value now", left_value)
        draw_screen()
        utime.sleep(0.2)
    last_a = button_a.value()

    # clear amount when B pressed (edge detect)
    if button_b.value() == 0 and last_b == 1:
        left_value = 0
        print("B pressed - cleared left_value")
        draw_screen()
        utime.sleep(0.2)
    last_b = button_b.value()

    # UP: small increase
    if joystick_up.value() == 0:
        if mode == "JPY":
            left_value += 100
        else:
            left_value += 1
        print("UP ->", left_value)
        draw_screen()
        utime.sleep(0.12)

    # DOWN: small decrease
    if joystick_down.value() == 0:
        if mode == "JPY":
            left_value = max(0, left_value - 100)
        else:
            left_value = max(0, left_value - 1)
        print("DOWN ->", left_value)
        draw_screen()
        utime.sleep(0.12)

    # RIGHT: big increase
    if joystick_right.value() == 0:
        if mode == "JPY":
            left_value += 1000
        else:
            left_value += 10
        print("RIGHT ->", left_value)
        draw_screen()
        utime.sleep(0.12)

    # LEFT: big decrease
    if joystick_left.value() == 0:
        if mode == "JPY":
            left_value = max(0, left_value - 1000)
        else:
            left_value = max(0, left_value - 10)
        print("LEFT ->", left_value)
        draw_screen()
        utime.sleep(0.12)

    utime.sleep(0.02)
