import display
import custom_shapes
import time
import buttons
import utils
import easydraw
import lobby
import secret

KONAMI_CODE = [
    "UP",
    "UP",
    "DOWN",
    "DOWN",
    "LEFT",
    "RIGHT",
    "LEFT",
    "RIGHT",
    "B",
    "A",
]

KONAMI_COUNTER = 0


def draw_secret(secret):
    # Draw a secret! What could it be?
    display.drawRect(30, 105, 260, 41, True, utils.BG_LIGHT)
    display.drawRect(27, 108, 266, 35, True, utils.BG_LIGHT)
    easydraw.lineCentered(123, secret, utils.ARCADE_FONT[9], utils.BG_DARK)
    display.flush()


def handle_konami(input):
    global KONAMI_COUNTER

    if input == KONAMI_CODE[KONAMI_COUNTER]:
        KONAMI_COUNTER += 1
    else:
        KONAMI_COUNTER = 0

    if KONAMI_COUNTER == len(KONAMI_CODE):
        draw_secret(secret.get_secret())
        KONAMI_COUNTER = 0

        # Allow player to go back to about screen
        buttons.detach(buttons.BTN_A)
        buttons.attach(buttons.BTN_B, lambda pressed: about() if pressed else 0)
        buttons.detach(buttons.BTN_UP)
        buttons.detach(buttons.BTN_DOWN)
        buttons.detach(buttons.BTN_LEFT)
        buttons.detach(buttons.BTN_RIGHT)


def about():
    """
    Display about screen
    Params:
    """
    utils.draw_background(wifi_indicator=True, logo=False)

    easydraw.lineCentered(
        40, "Made with love by Deloitte", utils.ARCADE_FONT[9], utils.TEXT_HIGHLIGHT
    )
    easydraw.lineCentered(
        60, "Mick Cox, Jorai Rijsdijk,", utils.ARCADE_FONT[8], utils.TEXT_MAIN
    )
    easydraw.lineCentered(75, "Diederik Bakker,", utils.ARCADE_FONT[8], utils.TEXT_MAIN)
    easydraw.lineCentered(
        90, "Pavlos Lontorfos, Luuk Hofman,", utils.ARCADE_FONT[8], utils.TEXT_MAIN
    )
    easydraw.lineCentered(105, "Viviane, Fons,", utils.ARCADE_FONT[8], utils.TEXT_MAIN)
    easydraw.lineCentered(
        120, "Rikkert ten Klooster,", utils.ARCADE_FONT[8], utils.TEXT_MAIN
    )
    easydraw.lineCentered(135, "Yasen,", utils.ARCADE_FONT[8], utils.TEXT_MAIN)
    easydraw.lineCentered(
        150, "JurreJelle, JelleJurre", utils.ARCADE_FONT[8], utils.TEXT_MAIN
    )
    easydraw.lineCentered(165, "& Jilles_com", utils.ARCADE_FONT[8], utils.TEXT_MAIN)

    easydraw.lineCentered(
        195, "Based on OpenSpaceteam", utils.ARCADE_FONT[8], utils.TEXT_MAIN
    )
    easydraw.lineCentered(
        210, "by Giuseppe Guerra", utils.ARCADE_FONT[8], utils.TEXT_MAIN
    )
    display.flush()

    buttons.attach(
        buttons.BTN_UP, lambda pressed: handle_konami("UP") if pressed else 0
    )
    buttons.attach(
        buttons.BTN_DOWN, lambda pressed: handle_konami("DOWN") if pressed else 0
    )
    buttons.attach(
        buttons.BTN_LEFT, lambda pressed: handle_konami("LEFT") if pressed else 0
    )
    buttons.attach(
        buttons.BTN_RIGHT, lambda pressed: handle_konami("RIGHT") if pressed else 0
    )
    buttons.attach(buttons.BTN_A, lambda pressed: handle_konami("A") if pressed else 0)
    buttons.attach(buttons.BTN_B, lambda pressed: handle_konami("B") if pressed else 0)


def help():
    """
    Display help screen
    Params:
    """
    utils.draw_background(wifi_indicator=True, logo=False)
    easydraw.lineCentered(20, "HELP", utils.ARCADE_FONT[18], utils.TEXT_HIGHLIGHT)
    line_y = 45
    lines = easydraw.lineSplit(
        "A SpaceTeam-like game. The city is in trouble. An intelligent algorithm went rogue, and the once so peaceful smart city is bullied by its new controller.",
        width=display.width() - 50,
        font=utils.ARCADE_FONT[12],
    )
    for line in lines:
        easydraw.lineCentered(line_y, line, utils.ARCADE_FONT[12], utils.TEXT_MAIN)
        line_y += 20

    display.flush()


def draw_sign(action, i):
    global code_x
    global code_y
    if action == "A" or action == "B":
        display.drawText(
            code_x[i] - 5,
            code_y - 5,
            action,
            utils.TEXT_HIGHLIGHT,
            utils.ARCADE_FONT[18],
        )
    else:
        custom_shapes.draw_code_arrow(code_x[i], code_y, action)
    display.flush()


def handle_input(action):
    global code, i
    if i < 6:
        draw_sign(action, i)
        code.append(action)
        i += 1
    if i == 6:
        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_B)
        lobby.join_lobby(code)


def join_lobby_input():
    """
    Display join lobby screen
    Params:
    """
    if not utils.connect_wifi():
        utils.draw_background()
        easydraw.lineCentered(
            180, "WiFi is required to play", utils.ARCADE_FONT[9], utils.TEXT_MAIN
        )
        display.flush()
        return

    if not utils.check_version():
        return

    global code, i
    code = []
    i = 0

    utils.draw_background(wifi_indicator=True, logo=False)
    easydraw.lineCentered(
        80, "Enter lobby code", utils.ARCADE_FONT[12], utils.TEXT_MAIN
    )
    easydraw.lineCentered(
        100, "Provided by host", utils.ARCADE_FONT[12], utils.TEXT_MAIN
    )

    display.drawRect(50, 130, 220, 50, False, utils.HIGHLIGHT)
    display.flush()

    # Sleep so we do not immediately register inputs
    time.sleep_ms(500)

    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("A") if pressed else 0)
    buttons.attach(buttons.BTN_B, lambda pressed: handle_input("B") if pressed else 0)


# 220 pixels in which we need 6 chars. Limits of square will be: 50,86,122,158,194,230,266. So center of the square is: 68,104,148,178,212,248
code = []
i = 0
code_x = [68, 104, 140, 176, 212, 248]
code_y = 155
