"""Module 06: recognise up, down, left, and right APDS9960 gestures."""

import time
from adafruit_apds9960.apds9960 import APDS9960
import board
import busio
import adafruit_ssd1306


def show_module(oled, number, title, lines):
    """Draw this lesson's OLED screen without an external helper module."""
    oled.fill(0)
    oled.text("({}) {}".format(number, title)[:21], 0, 0, 1)
    for index, line in enumerate(lines[:4]):
        oled.text(str(line)[:21], 0, 16 + index * 11, 1)
    oled.show()


GESTURES = {1: "Up", 2: "Down", 3: "Left", 4: "Right"}

i2c = busio.I2C(board.GP5, board.GP4)
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
sensor = APDS9960(i2c)
sensor.enable_proximity = True
sensor.enable_gesture = True
current = "Move hand"

try:
    while True:
        gesture = sensor.gesture()
        if gesture in GESTURES:
            current = GESTURES[gesture]
            print("Gesture:", current)
        show_module(oled, 6, "APDS9960", (
            "Mode: Gesture",
            "Up Down Left Right",
            "Gesture: " + current,
        ))
        time.sleep(0.05)
finally:
    sensor.enable_gesture = False
    sensor.enable_proximity = False
    i2c.deinit()
