#!/usr/bin/env python3

import tkinter as tk
from PIL import Image, ImageTk
import screeninfo, sys, os

def show_image(image_path, box_width=400, box_height=300, hover_header=None, click_url=None):
    root = tk.Tk()
    root.overrideredirect(True)
    root.withdraw()
    root.after(100, root.deiconify)
    root.attributes("-topmost", True)
    root.attributes("-alpha", 0.85)

    img = Image.open(image_path)
    img.thumbnail((box_width, box_height))
    img_tk = ImageTk.PhotoImage(img)

    monitors = screeninfo.get_monitors()
    primary_screen = next((m for m in monitors if m.is_primary), monitors[0])

    screen_x = primary_screen.x
    screen_y = primary_screen.y
    screen_width = primary_screen.width
    screen_height = primary_screen.height

    x_pos = screen_x + screen_width - box_width - 20
    y_pos = screen_y + screen_height - box_height - 60

    root.geometry(f"{box_width}x{box_height}+{x_pos}+{y_pos}")

    frame = tk.Frame(root, bg="black")
    frame.pack(fill="both", expand=True)

    label = tk.Label(frame, image=img_tk, borderwidth=0)
    label.image = img_tk
    label.place(relx=0.5, rely=0.5, anchor="center")

    header_label = tk.Label(root, text=hover_header, bg="black", fg="white", font=("Arial", 10))

    def show_header(event):
        if hover_header:
            header_label.place(relx=0.5, rely=0, anchor="n", relwidth=1)

    def hide_header(event):
        if hover_header:
            header_label.place_forget()

    def close_window(event=None):
        root.destroy()

    root.bind("<Button-1>", close_window if click_url is None else lambda e: os.system(f"xdg-open {click_url}"))
    root.bind("<Enter>", show_header)
    root.bind("<Leave>", hide_header)

    root.after(5000, close_window)
    root.mainloop()

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: show_image.py <image_path> [hover_header] [click_url]")
        sys.exit(1)

    image_path = sys.argv[1]
    hover_header = sys.argv[2] if len(sys.argv) > 2 else None
    click_url = sys.argv[3] if len(sys.argv) > 3 else None

    show_image(image_path, hover_header=hover_header, click_url=click_url)