import display
import buttons
import custom_shapes
import utils
import static_pages
import lobby

display_size = display.size()

icon_size = display.pngInfo(utils.ICON_LARGE)
icon_offset = (display_size[0] - icon_size[0]) // 2


def default_text():
    display.drawText(
        int(display.width() * 0.2),
        int(display.height() * 0.7),
        "Create Game",
        utils.TEXT_HIGHLIGHT if arrow_pos == 0 else utils.TEXT_MAIN,
        utils.ARCADE_FONT[8],
    )
    display.drawText(
        int(display.width() * 0.2),
        int(display.height() * 0.8),
        "Join Game",
        utils.TEXT_HIGHLIGHT if arrow_pos == 1 else utils.TEXT_MAIN,
        utils.ARCADE_FONT[8],
    )
    display.drawText(
        int(display.width() * 0.63),
        int(display.height() * 0.7),
        "Help",
        utils.TEXT_HIGHLIGHT if arrow_pos == 2 else utils.TEXT_MAIN,
        utils.ARCADE_FONT[8],
    )
    display.drawText(
        int(display.width() * 0.63),
        int(display.height() * 0.8),
        "About",
        utils.TEXT_HIGHLIGHT if arrow_pos == 3 else utils.TEXT_MAIN,
        utils.ARCADE_FONT[8],
    )


def move_arrow(direction):
    global arrow_pos
    if direction == "DOWN":
        arrow_pos += 1
    elif direction == "UP":
        arrow_pos -= 1
    else:
        arrow_pos += 2
    arrow_pos %= 4


def draw_main_menu():
    utils.draw_background()
    default_text()
    custom_shapes.draw_right_arrow(
        arrow_x[arrow_pos],
        arrow_y[arrow_pos],
        15,
        6,
        True,
        0x9CFF08
    )
    display.flush()


def handle_input(action):
    if action == "PRESS":
        buttons.attach(buttons.BTN_B, lambda pressed: main_menu() if pressed else 0)
        buttons.detach(buttons.BTN_A)
        buttons.detach(buttons.BTN_UP)
        buttons.detach(buttons.BTN_DOWN)
        buttons.detach(buttons.BTN_LEFT)
        buttons.detach(buttons.BTN_RIGHT)
        buttons.detach(buttons.BTN_A)
        buttons.detach(buttons.BTN_PRESS)
        menu[arrow_pos]()
    else:
        move_arrow(action)
        draw_main_menu()


arrow_x = [
    int(display.width() * 0.17),
    int(display.width() * 0.17),
    int(display.width() * 0.60),
    int(display.width() * 0.60),
]
arrow_y = [
    int(display.height() * 0.7 + 2),
    int(display.height() * 0.8 + 2),
    int(display.height() * 0.7 + 2),
    int(display.height() * 0.8 + 2),
]
menu = [lobby.create_lobby, static_pages.join_lobby_input, static_pages.help, static_pages.about]
arrow_pos = 0


def main_menu():
    buttons.attach(buttons.BTN_MENU, lambda pressed: main_menu() if pressed else 0)
    buttons.attach(buttons.BTN_UP, lambda pressed: handle_input("UP") if pressed else 0)
    buttons.attach(buttons.BTN_DOWN, lambda pressed: handle_input("DOWN") if pressed else 0)
    buttons.attach(buttons.BTN_LEFT, lambda pressed: handle_input("LEFT") if pressed else 0)
    buttons.attach(buttons.BTN_RIGHT, lambda pressed: handle_input("RIGHT") if pressed else 0)
    buttons.attach(buttons.BTN_A, lambda pressed: handle_input("PRESS") if pressed else 0)
    buttons.attach(buttons.BTN_PRESS, lambda pressed: handle_input("PRESS") if pressed else 0)
    buttons.detach(buttons.BTN_B)

    draw_main_menu()
