<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Telepic v0.1.0</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }

        .header {
            text-align: center;
            margin-bottom: 20px;
            padding: 10px;
        }

        .logo {
            max-width: 200px;
            height: auto;
            margin: 0 auto;
            display: block;
        }

        .image-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            padding: 20px;
        }

        .image-container {
            background: white;
            padding: 10px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            text-align: center;
            position: relative;
        }

        .image-container img {
            max-width: 100%;
            height: auto;
            border-radius: 4px;
            transition: transform 0.3s ease;
        }

        .image-container:hover .hover-zoom {
            display: block;
        }

        .hover-zoom {
            display: none;
            position: fixed;
            z-index: 1000;
            background: white;
            padding: 10px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            width: 600px;
            max-width: max-content;
            pointer-events: none;
        }

        .hover-zoom img {
            max-width: 100%;
            height: auto;
            transform: scale(1.5);
            transform-origin: center;
        }

        .image-name {
            margin-top: 10px;
            font-size: 14px;
            color: #333;
            word-break: break-all;
        }

        .image-link {
            text-decoration: none;
            color: inherit;
            cursor: pointer;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            gap: 10px;
        }

        .pagination button {
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .pagination button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }

        .page-info {
            margin: 0 10px;
            line-height: 35px;
        }

        .loading {
            text-align: center;
            padding: 20px;
            font-size: 18px;
            color: #007bff;
        }
    </style>
</head>
<body>
    <!-- Add header with logo -->
    <div class="header">
        <img src="{{ url_for('static', filename='telepic.png') }}" alt="Telepic Logo" class="logo">
        <p>Total images: {{ total_images }}</p>
    </div>

    <div class="image-grid" id="imageGrid">
        {% for index, image in images %}
            <div class="image-container">
                <a href="{{ url_for('serve_image', index=index) }}" target="_blank" class="image-link">
                    <img src="{{ url_for('serve_image', index=index) }}" alt="{{ image.name }}">
                    <div class="image-name">{{ image.name }}</div>
                </a>
                <div class="hover-zoom">
                    <img src="{{ url_for('serve_image', index=index) }}" alt="{{ image.name }}">
                </div>
            </div>
        {% endfor %}
    </div>

    <div id="loading" class="loading" style="display: none;">
        Loading images...
    </div>

    <div class="pagination">
        <button id="prevPage" onclick="changePage(-1)" disabled>Previous</button>
        <span id="pageInfo" class="page-info">Page {{ current_page + 1 }} of {{ (total_images + images_per_page - 1) // images_per_page }}</span>
        <button id="nextPage" onclick="changePage(1)" {% if (current_page + 1) * images_per_page >= total_images %}disabled{% endif %}>Next</button>
    </div>

    <script>
        const imagesPerPage = {{ images_per_page }};
        const totalImages = {{ total_images }};
        let currentPage = {{ current_page }};
        let totalPages = Math.ceil(totalImages / imagesPerPage);

        // Add mouse tracking for hover zoom with boundary checking
        function setupHoverZoom() {
            document.querySelectorAll('.image-container').forEach(container => {
                const hoverZoom = container.querySelector('.hover-zoom');
                if (!hoverZoom) return;

                container.addEventListener('mousemove', (e) => {
                    const x = e.clientX;
                    const y = e.clientY;

                    // Get viewport dimensions
                    const viewportWidth = window.innerWidth;
                    const viewportHeight = window.innerHeight;

                    // Get hover zoom dimensions
                    const hoverWidth = hoverZoom.offsetWidth;
                    const hoverHeight = hoverZoom.offsetHeight;

                    // Calculate position while keeping the hover zoom within viewport
                    let left = x;
                    let top = y;

                    // Adjust horizontal position
                    if (x + (hoverWidth / 2) > viewportWidth) {
                        left = viewportWidth - (hoverWidth / 2);
                    } else if (x - (hoverWidth / 2) < 0) {
                        left = hoverWidth / 2;
                    }

                    // Adjust vertical position
                    if (y + (hoverHeight / 2) > viewportHeight) {
                        top = viewportHeight - (hoverHeight / 2);
                    } else if (y - (hoverHeight / 2) < 0) {
                        top = hoverHeight / 2;
                    }

                    // Apply the calculated position
                    hoverZoom.style.left = left + 'px';
                    hoverZoom.style.top = top + 'px';
                    hoverZoom.style.transform = 'translate(-50%, -50%)';
                });
            });
        }

        // Initialize the page with event handlers
        setupHoverZoom();

        // Function to fetch images from the server
        async function fetchImages(page) {
            const loadingElement = document.getElementById('loading');
            loadingElement.style.display = 'block';

            try {
                const response = await fetch(`/api/images?page=${page}`);
                if (!response.ok) {
                    throw new Error('Failed to fetch images');
                }

                const data = await response.json();
                return data;
            } catch (error) {
                console.error('Error fetching images:', error);
                return null;
            } finally {
                loadingElement.style.display = 'none';
            }
        }

        // Function to render images in the grid
        function renderImages(images) {
            const imageGrid = document.getElementById('imageGrid');
            imageGrid.innerHTML = '';

            images.forEach(image => {
                const container = document.createElement('div');
                container.className = 'image-container';

                const link = document.createElement('a');
                link.href = image.url;
                link.className = 'image-link';
                link.target = '_blank';

                const img = document.createElement('img');
                img.src = image.url;
                img.alt = image.name;

                const nameDiv = document.createElement('div');
                nameDiv.className = 'image-name';
                nameDiv.textContent = image.name;

                const hoverDiv = document.createElement('div');
                hoverDiv.className = 'hover-zoom';

                const hoverImg = document.createElement('img');
                hoverImg.src = image.url;
                hoverImg.alt = image.name;

                link.appendChild(img);
                link.appendChild(nameDiv);
                hoverDiv.appendChild(hoverImg);

                container.appendChild(link);
                container.appendChild(hoverDiv);
                imageGrid.appendChild(container);
            });

            // Re-setup hover zoom for new images
            setupHoverZoom();
        }

        // Function to update pagination UI
        function updatePaginationUI() {
            document.getElementById('prevPage').disabled = currentPage === 0;
            document.getElementById('nextPage').disabled = currentPage >= totalPages - 1;
            document.getElementById('pageInfo').textContent = `Page ${currentPage + 1} of ${totalPages}`;
        }

        // Function to change the page
        async function changePage(delta) {
            const newPage = currentPage + delta;

            if (newPage < 0 || newPage >= totalPages) {
                return;
            }

            const data = await fetchImages(newPage);
            if (data) {
                currentPage = newPage;
                renderImages(data.images);
                updatePaginationUI();
            }
        }
    </script>
</body>
</html>