import time
from datetime import datetime
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
import json
import pytz

try:
    with open('clock_screensaver.json', 'r') as f:
        settings = json.load(f)['clock2']
except:
    settings1 = {"clock1": {"time_color": "White", "weekday_color": "Cyan", "date_color": "Blue", "timezone": "Etc/GMT+4", "display_seconds": True, "display_pm": True, "12hour": True, "brightness": "10"}, "clock2": {"time_color": "Orange", "date_color": "White", "timezone": "Etc/GMT+4", "display_seconds": True, "display_pm": True, "12hour": True, "brightness": "10"}, "world_clock": {"city_color": "White", "display_seconds": True, "display_pm": True, "12hour": True, "brightness": "10"}}
    with open('clock_screensaver.json', 'w') as f:
        json.dump(settings1, f)
    settings = settings1['clock2']

# Configuration for the LED matrix
options = RGBMatrixOptions()
options.rows = 32
options.cols = 128
options.chain_length = 1
options.parallel = 1
options.hardware_mapping = 'adafruit-hat'  # Change this if you have a different GPIO mapping
options.gpio_slowdown = 2
options.brightness = int(settings['brightness']) * 10
# Create the RGBMatrix object
matrix = RGBMatrix(options=options)

# Create a canvas to draw on
canvas = matrix.CreateFrameCanvas()
timezone = pytz.timezone(settings['timezone'])
# Set the font and text color
font = graphics.Font()
font.LoadFont("fonts/clR6x12.bdf")
#font.LoadFont("fonts/6x12.bdf")  # Change this to the path of your desired font file
font2 = graphics.Font()
font2.LoadFont("fonts/texgyre-27.bdf") 
time_colors = {
    "White": graphics.Color(255,255,255),
    "Red": graphics.Color(255, 0,0),
    "Green": graphics.Color(0,255,0),
    "Dark Green": graphics.Color(0, 100,0),
    "Blue": graphics.Color(0,0,255),
    "Purple": graphics.Color(145,0,255),
    "Pink": graphics.Color(255,0,255),
    "Yellow": graphics.Color(255,255,0),
    "Orange": graphics.Color(255,130,0),
    "Gold": graphics.Color(255,190,0),
    "Gray": graphics.Color(100,100,100),
    "Cyan": graphics.Color(0,255,255)
}
colon_visible = False
while True:
    canvas.Clear()

    if settings['12hour']:
        if settings['display_pm']:
            current_time = datetime.now(timezone).strftime("%I:%M %p")
        else:
            current_time = datetime.now(timezone).strftime("%I:%M")
    else:
        if settings['display_pm']:
            current_time = datetime.now(timezone).strftime("%H:%M %p")
        else:
            current_time = datetime.now(timezone).strftime("%H:%M")

    if colon_visible:
        current_time = current_time.replace(":", " ")
        colon_visible = False
    else:
        colon_visible = True
    
    current_date = datetime.now(timezone).strftime("%a %d %b %Y")

    date_width = graphics.DrawText(canvas, font, 0, 0, time_colors[settings['date_color']], current_date.upper())
    time_width = graphics.DrawText(canvas, font2, 0, 0, time_colors[settings['time_color']], current_time)

    canvas.Clear()

    x_coordinate_time = int((options.cols - time_width) / 2)
    x_coordinate_date = int((options.cols - date_width) /2)

    graphics.DrawText(canvas, font2, x_coordinate_time, 21, time_colors[settings['time_color']], current_time)
    graphics.DrawText(canvas, font, x_coordinate_date, 31, time_colors[settings['date_color']], current_date.upper())

    canvas = matrix.SwapOnVSync(canvas)

    time.sleep(0.5)

