import subprocess
import time
import epd2in13_V3
from PIL import Image, ImageDraw, ImageFont

def get_wifi_status():
    try:
        # Get the SSID of the currently connected Wi-Fi network
        result = subprocess.run(['iwgetid', '-r'], capture_output=True, text=True)
        ssid = result.stdout.strip()
        if ssid:
            return "OK", ssid
        else:
            return "Disconnected", "N/A"
    except Exception as e:
        return "Error", str(e)

def get_time_date():
    current_time = time.strftime('%I:%M %p')
    current_date = time.strftime('%a, %b %d, %Y')
    return current_time, current_date

def main():
    # Initialize the e-paper display
    epd = epd2in13_V3.EPD()
    epd.init()
    epd.Clear(0xFF)  # White background

    # Load fonts
    font_large = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 18)
    font_small = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 14)

    while True:
        # Create a new image with white background
        image = Image.new('1', (epd.height, epd.width), 255)  # '1' for 1-bit color
        draw = ImageDraw.Draw(image)

        # Get connection status and SSID
        status, ssid = get_wifi_status()

        # Get current time and date
        current_time, current_date = get_time_date()

        # Draw text on image
        draw.text((10, 10), f"CONNECTION: {status}", font=font_large, fill=0)
        draw.text((10, 30), f"SSID: {ssid}", font=font_large, fill=0)
        draw.text((10, 50), f"{current_time}", font=font_large, fill=0)
        draw.text((10, 70), f"{current_date}", font=font_large, fill=0)
        draw.text((10, 100), "DO NOT UNPLUG | rintyuu.dev", font=font_small, fill=0)

        # Rotate image if necessary
        rotated_image = image.rotate(180)

        # Display the image
        epd.display(epd.getbuffer(rotated_image))
        epd.sleep()

        # Wait for a minute before updating
        time.sleep(60)

if __name__ == "__main__":
    main()
