import pygame
import requests
from tkinter import Tk, simpledialog
from PIL import Image
from io import BytesIO
import sys

# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption('Fullscreen Image Viewer')
clock = pygame.time.Clock()

image_path = 'img.jpg'

def load_and_display_image(path):
    try:
        img = pygame.image.load(path)
        img = pygame.transform.scale(img, screen.get_size())
        screen.blit(img, (0, 0))
        pygame.display.flip()
    except Exception as e:
        print(f'Error loading image: {e}')

def download_image(url, save_path='img.jpg'):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            img = Image.open(BytesIO(response.content))
            img.save(save_path)
            print('Image downloaded and saved as img.jpg')
        else:
            print(f'Failed to download image. Status code: {response.status_code}')
    except Exception as e:
        print(f'Error downloading image: {e}')

load_and_display_image(image_path)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            elif event.key == pygame.K_s:
                root = Tk()
                root.withdraw()
                url = simpledialog.askstring('Image URL', 'Enter the image URL:')
                root.destroy()
                if url:
                    download_image(url)
                    load_and_display_image(image_path)
    clock.tick(30)

pygame.quit()
sys.exit()
