# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Simple test script for 1.54" 200x200 Tri-Color display.

Supported products:
  * Adafruit 1.54" Monochrome ePaper Display Breakout
    * https://www.adafruit.com/product/4196 or 4668
  """

import time
import board
import displayio
import fourwire
import adafruit_ssd1681
import adafruit_ds3231
from adafruit_display_text import label
from adafruit_display_shapes.rect import Rect
from adafruit_bitmap_font import bitmap_font
import terminalio

#i2c = board.I2C()  # uses board.SCL and board.SDA
i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
rtc = adafruit_ds3231.DS3231(i2c)

BLACK = 0x000000
WHITE = 0xFFFFFF
RED = 0xFF0000


FOREGROUND_COLOR = BLACK
BACKGROUND_COLOR = WHITE
RED_FOREGROUND_COLOR = RED

monthsOfYear = ("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "Dec")

# Lookup table for names of days (nicer printing).
days = ("Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun")
#         0,     1,     2,     3,      4,     5,     6

# pylint: disable-msg=using-constant-test
if False:  # change to True if you want to set the time!
    #                     year, mon, date, hour, min, sec, wday, yday, isdst
    t = time.struct_time((2024, 12, 19, 08, 54, 13, 3, -1, -1))
    # you must set year, mon, date, hour, min, sec and weekday
    # yearday is not supported, isdst can be set but we don't do anything with it at this time
    print("Setting time to:", t)  # uncomment for debugging
    rtc.datetime = t
    print(t)

displayio.release_displays()

# This pinout works on a Feather M4 and may need to be altered for other boards.
spi = board.SPI()  # Uses SCK and MOSI
epd_cs = board.TX
epd_dc = board.RX
epd_reset = board.D15
epd_busy = board.D32

# Create the display object - the third color is red (0xff0000)
DISPLAY_WIDTH = 200
DISPLAY_HEIGHT = 200

# Set up Place holder for display.
d = rtc.datetime
dayOfWeekStr_text = days[int(d.tm_wday)] # Display Week Day Name
monthStr_text = monthsOfYear[int(d.tm_mon)-1]
dateStr_text = str(d.tm_mday) # Display Day
yearStr_text = str(d.tm_year) # Display Year
timeStr_text = "{:02}:{:02}:{:02}".format(d.tm_hour, d.tm_min, d.tm_sec) # Display Time of Day

# Set up fonts here
font = bitmap_font.load_font("Fonts/font_free_sans_12/font.pcf")
font1 = bitmap_font.load_font("Fonts/font_free_serif_bold_18/font.pcf")
font2 = bitmap_font.load_font("Fonts/font_free_sans_24/font.pcf")

display_bus = fourwire.FourWire(
    spi, command=epd_dc, chip_select=epd_cs, reset=epd_reset, baudrate=1000000
)

time.sleep(10)
print("starting program")
display = adafruit_ssd1681.SSD1681(
    display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, busy_pin=epd_busy, highlight_color=0xFF0000, rotation=180)
display_width = display.width

main_group = displayio.Group()

background_bitmap = displayio.Bitmap(DISPLAY_WIDTH, DISPLAY_HEIGHT, 1)
# Map colors in a palette
palette = displayio.Palette(1)
palette[0] = BACKGROUND_COLOR

# Setup the Calendar Display
screen = displayio.TileGrid(background_bitmap, pixel_shader=palette)

# Create a Tilegrid with the background and put in the displayio group
main_group.append(screen)

# Create Black rectangle in display
rect = Rect(0, 0, 200, 60, fill=RED_FOREGROUND_COLOR)
main_group.append(rect)

# Create Black boader around display
rect1 = Rect(0, 0, 200, 170, outline=FOREGROUND_COLOR, stroke=2)
main_group.append(rect1)

# Display the Day of the Week Name in Black rectangle
dayOfWeekStr = label.Label(font=font2, text=dayOfWeekStr_text, color=BACKGROUND_COLOR, scale=2) # Setup Day Name
text_width = dayOfWeekStr.bounding_box[2]
x_position = (display_width - text_width) // 2 # Center position
dayOfWeekStr.x = x_position - (text_width // 2)
dayOfWeekStr.y = 28 # Adjust the y position as needed
main_group.append(dayOfWeekStr)

# Display the Month
monthStr = label.Label(font=font1, text=monthStr_text, color=FOREGROUND_COLOR, scale=2) # x=60, y=75 # Setup Month
text_width = monthStr.bounding_box[2]
x_position = (display_width - text_width) // 2 # Center position
monthStr.x = (x_position - (text_width // 2))
monthStr.y = 75 # Adjust the y position as needed
main_group.append(monthStr)

# Display the Day
dateStr = label.Label(font=font1, text=dateStr_text, color=FOREGROUND_COLOR, scale=2)# x=80, y=110 # Setup Day Number
text_width = dateStr.bounding_box[2]
x_position = (display_width - text_width) // 2 # Center position
dateStr.x = x_position  - (text_width // 2)
dateStr.y = 110 # Adjust the y position as needed
main_group.append(dateStr)

# Display the Year
yearStr = label.Label(font=font1, text=yearStr_text, color=FOREGROUND_COLOR, scale=2) # x=62, y=145 # Setup Year
text_width = yearStr.bounding_box[2]
x_position = (display_width - text_width) // 2 # Center position
yearStr.x = x_position  - (text_width // 2)
yearStr.y = 145 # Adjust the y position as needed
main_group.append(yearStr)

# Display the time
timeStr = label.Label(font=font1, text=timeStr_text, color=FOREGROUND_COLOR, scale=1) # x=62, y=185 # Setup Time of Day
text_width = timeStr.bounding_box[2]
x_position = (display_width - text_width) // 2 # Center position
timeStr.x = (x_position - (text_width // 2))
timeStr.y = 185 # Adjust the y position as needed
main_group.append(timeStr)

print("starting loop")
while True:
    d = rtc.datetime
    dayOfWeekStr.text = days[int(d.tm_wday)] # Display Week Day Name
    monthStr.text = monthsOfYear[int(d.tm_mon)-1]
    dateStr.text = str(d.tm_mday) # Display Day
    yearStr.text = str(d.tm_year) # Display Year
    timeStr.text = "      {:02}:{:02}:{:02}".format(d.tm_hour, d.tm_min, d.tm_sec) # Display Time of Day
    time.sleep(1)  # wait a second
    display.root_group = main_group
    print(timeStr.text)
    print("refreshing")
    display.refresh()
    print("going to sleep now")
    time.sleep(3600) # Rest E-ink display before refreshing again
    print("wake up time")