#!/usr/bin/env python3
import argparse
import os
import sys
from datetime import datetime
from pathlib import Path

from PIL import Image, ImageDraw, ImageFont

sys.path.append(str(Path(__file__).parent / "lib"))
from waveshare_epd import epd7in3e
from immich import ImmichClient, get_random_photo_of_people, get_random_photo_from_album
from homeassistant import HomeAssistantClient

IMMICH_URL = os.environ.get("IMMICH_URL", "https://immich.schmelczer.dev")
IMMICH_API_KEY = os.environ.get("IMMICH_API_KEY", "6crxVS1JLTJxsfGlzVhN2kefdL4EP7HPkkoMk9L6ZOE")

HA_URL = os.environ.get("HA_URL", "https://homeassistant.schmelczer.dev")
HA_TOKEN = os.environ.get("HA_TOKEN", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmZjk3OTNmOWMzOWU0YjdmYmRjYTc5YmJkMTUyODcyNSIsImlhdCI6MTc2OTIwMjg1NCwiZXhwIjoyMDg0NTYyODU0fQ.IiL_1vTrGMlOoPMksN6lAopE0aInlY_wRnL4Jc-CeBs")
HA_PRESENCE_ENTITIES = ["person.andras", "person.ruby"]

DEFAULT_SATURATION = 1.3
DEFAULT_CONTRAST = 1.05
DEFAULT_GAMMA = 0.90

def display_image(image_path: Path, orientation: int, saturation: float,
                  contrast: float, gamma: float, enhance: bool) -> None:
    epd = epd7in3e.EPD()
    try:
        epd.init()
        img = Image.open(image_path).convert("RGB")
        if orientation:
            img = img.rotate(orientation, expand=True)
        buf = epd.getbuffer(img, saturation=saturation, contrast=contrast,
                            gamma=gamma, enhance=enhance)
        epd.display(buf)
    finally:
        epd.sleep()


def main() -> None:
    parser = argparse.ArgumentParser(description="Display image on e-ink frame")
    parser.add_argument("--people", default="Me,Ruby",
                        help="Comma-separated names for Immich search")
    parser.add_argument("--album", help="Fetch from album (overrides --people)")
    parser.add_argument("-o", "--orientation", type=int, choices=[0, 90, 180, 270],
                        default=0, help="Rotation in degrees")
    parser.add_argument("--saturation", type=float, default=DEFAULT_SATURATION)
    parser.add_argument("--contrast", type=float, default=DEFAULT_CONTRAST)
    parser.add_argument("--gamma", type=float, default=DEFAULT_GAMMA)
    parser.add_argument("--no-enhance", action="store_true")
    args = parser.parse_args()

    now = datetime.now()
    print(f"Time: {now.strftime('%H:%M')}")

    if 0 <= now.hour < 7:
        print("Night time, skipping")
        sys.exit(0)

    ha = HomeAssistantClient(HA_URL, HA_TOKEN)
    home = [e.split(".")[-1].title() for e in HA_PRESENCE_ENTITIES if ha.is_person_home(e)]
    if not home:
        print("No one home, skipping")
        sys.exit(0)
    print(f"Home: {', '.join(home)}")

    client = ImmichClient(IMMICH_URL, IMMICH_API_KEY)
    if args.album:
        image_path = get_random_photo_from_album(client, args.album, args.orientation)
        print(f"Album: {args.album}")
    else:
        names = [n.strip() for n in args.people.split(",")]
        image_path = get_random_photo_of_people(client, names, args.orientation)
        print(f"People: {', '.join(names)}")

    try:
        display_image(image_path, args.orientation, args.saturation,
                      args.contrast, args.gamma, not args.no_enhance)
    finally:
        image_path.unlink(missing_ok=True)


if __name__ == "__main__":
    main()
