import time, board, terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal

# the current working directory (where this file is)
cwd = ("/" + __file__).rsplit("/", 1)[0]
counter = 0

matrixportal = MatrixPortal(
    url= "https://api.coingecko.com/api/v3/simple/price?ids=terra-luna,bitcoin,ethereum,avalanche-2,polkadot&vs_currencies=usd",
    json_path=[["bitcoin", "usd"], ["ethereum", "usd"], ["polkadot", "usd"], ["terra-luna", "usd"], ["avalanche-2", "usd"]],
    status_neopixel=board.NEOPIXEL,
    default_bg=cwd + "/bitcoin_logo.bmp",
    debug=False,
)

matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(25, 16),
    text_color=0x3d1f5c,
)

matrixportal.preload_font(b"$012345789")
matrixportal.preload_font((0x00A3, 0x20AC))



while True:
    try:
        value = matrixportal.fetch()
        print("Response is", value)
        time.sleep(3)
        matrixportal.set_background(cwd + "/ethereum_logo.bmp")
        matrixportal.set_text(round(value[1],1))
        time.sleep(3)
        matrixportal.set_background(cwd + "/polkadot_logo.bmp")
        matrixportal.set_text(value[2])
        time.sleep(3)
        matrixportal.set_background(cwd + "/terra-luna_logo.bmp")
        matrixportal.set_text(value[3])
        time.sleep(3)
        matrixportal.set_background(cwd + "/avalanche_logo.bmp")
        matrixportal.set_text(value[4])
        time.sleep(3)
        matrixportal.set_background(cwd + "/bitcoin_logo.bmp")
        matrixportal.set_text(value[0])

    except (ValueError, RuntimeError) as e:
        print("Some error occured, retrying! -", e)
        time.sleep(10)  # wait 10 seconds


