# SPDX-FileCopyrightText: 2025 DJDevon3
# SPDX-License-Identifier: MIT
# Coded for Circuit Python 9.2.3
# Sunton ESP32S3 8048S070 Example

import board
import time
import terminalio
import displayio
import dotclockframebuffer
from framebufferio import FramebufferDisplay
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
displayio.release_displays()

fb = dotclockframebuffer.DotClockFramebuffer(**board.TFT_PINS, **board.TFT_TIMINGS)
display = FramebufferDisplay(fb, auto_refresh=True)
# print(f"TFT Timings: {board.TFT_TIMINGS}")

DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 480

# Quick Colors for Labels
TEXT_BLACK = 0x000000
TEXT_BLUE = 0x0000FF
TEXT_CYAN = 0x00FFFF
TEXT_GRAY = 0x8B8B8B
TEXT_GREEN = 0x00FF00
TEXT_LIGHTBLUE = 0x90C7FF
TEXT_MAGENTA = 0xFF00FF
TEXT_ORANGE = 0xFFA500
TEXT_PINK = 0xFF007F
TEXT_PURPLE = 0x800080
TEXT_RED = 0xFF0000
TEXT_WHITE = 0xFFFFFF
TEXT_YELLOW = 0xFFFF00


Arial_16 = bitmap_font.load_font("/fonts/Arial-16.bdf")
GoodTimes_60 = bitmap_font.load_font("/fonts/GoodTimesRg-Regular-60.bdf")

# Load Bitmap to tile grid first (Background layer)
DiskBMP = displayio.OnDiskBitmap("/images/Astral_Fruit_800x480-24bit.bmp")
wallpaper = displayio.TileGrid(
    DiskBMP,
    pixel_shader=DiskBMP.pixel_shader,
    tile_width=DISPLAY_WIDTH,
    tile_height=DISPLAY_HEIGHT,
)

header_label = label.Label(Arial_16)
header_label.anchor_point = (0.5, 1.0)
header_label.anchored_position = (DISPLAY_WIDTH/2, 30)
header_label.scale = (1)
header_label.color = TEXT_WHITE

footer_label = label.Label(terminalio.FONT)
footer_label.anchor_point = (1.0, 1.0)
footer_label.anchored_position = (DISPLAY_WIDTH, DISPLAY_HEIGHT-20)
footer_label.scale = (1)
footer_label.color = TEXT_WHITE

temp_label = label.Label(GoodTimes_60)
temp_label.anchor_point = (0.5, 1.0)
temp_label.anchored_position = (DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2)
temp_label.scale = (1)
temp_label.color = TEXT_ORANGE

main_group = displayio.Group()
wallpaper_group = displayio.Group()
text_group = displayio.Group()
wallpaper_group.append(wallpaper)
text_group.append(header_label)
text_group.append(footer_label)
text_group.append(temp_label)
main_group.append(wallpaper_group)
main_group.append(text_group)
display.root_group = main_group

header_label.text = "Sunton ESP32-S3 8048S070 Dotclock Display"
temp_label.text = "CIRCUITPYTHON"
footer_label.text = "7\" 800x480 TFT Display"
print(f"Board Uptime in Seconds: {time.monotonic()}") # example for board uptime