#!/bin/bash

# Directory where images are stored (Quote it!)
IMAGE_DIR="../../assets/pics/gallery/sculpture"
# Output file (Quote it!)
OUTPUT_FILE="./index.html"  # Or specify the full path: /path/to/your/file/index.html

# Ensure the images directory exists
if [ ! -d "$IMAGE_DIR" ]; then
    echo "Error: Directory '$IMAGE_DIR' not found!"
    exit 1
fi

# Start writing the HTML file (Redirect to the quoted variable)
cat > "$OUTPUT_FILE" <<EOL
<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="apple-touch-icon" sizes="180x180" href="assets/favicon/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="assets/favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="assets/favicon/favicon-16x16.png">
<link rel="manifest" href="assets/favicon/site.webmanifest">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Beach Party Balloons</title>
    <link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css">
  <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Autour+One&family=Mogra&family=Rubik+Glitch&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../../style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" integrity="sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <style>
        .gallery {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 15px;
            padding: 20px;
            max-width: 1200px;
            margin: auto;
        }

        .gallery-item {
            text-align: center;
        }

        .gallery img {
            object-fit: cover;
            border-radius: 10px;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            cursor: pointer; /* Make images clickable */
        }

        .gallery img:hover {
            transform: scale(1.05);
            box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.2);
        }

        .caption {
            font-size: 0.9rem;
            margin-top: 5px;
            color: #4a4a4a;
            font-family: 'Autour One', cursive;
        }

        .modal-image {
            max-width: 90%;
            max-height: 90vh;
            display: block;
            margin: 0 auto;
            border-radius: 10px;
        }

        .modal-content {
            overflow-y: auto;
            max-height: 95vh;
            background-color: transparent !important;
        }

        .modal-card-title {
            font-family: 'Autour One', cursive;
            background-color: transparent !important;
        }
    </style>
</head>
<body>
    <nav class="navbar is-info is-spaced has-shadow" role="navigation" aria-label="main navigation">
        <div class="navbar-brand is-size-1">
            <a class="navbar-item" href="../../index.html">
                <img style="background-color: white;" src="../../assets/logo/BeachPartyBalloons-logo.png" alt="Beach Party Balloons logo">
            </a>

          <a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
            <span aria-hidden="true"></span>
          </a>
        </div>
        <div id="navbarBasicExample" class="navbar-menu has-text-right">
            <div class="navbar-end">
                <a class="navbar-item" href="../../index.html">Home</a>
                <a class="navbar-item" href="../../about/index.html">About Us</a>
                <a class="navbar-item" href="../../faq/index.html">FAQ</a>
                <a class="navbar-item" href="../../terms/index.html">Terms</a>
                <a class="navbar-item is-tab is-active" href="../../gallery/index.html">Gallery</a>
                <a class="navbar-item" href="../../contact/index.html">Contact</a>
            </div>
        </div>
    </nav>
           <button onclick="topFunction()" class="has-text-dark" id="top" title="Go to top">Top</button> 

    <nav class="breadcrumb is-centered" aria-label="breadcrumbs">
  <ul>
    <li><a href="../index.html">Gallery</a></li>
    <li class="is-active"><a href="#" aria-current="page">Sculptures and Themes</a></li>
  </ul>
</nav>
    <section class="section">
        <div class="container">
            <h1 class="title has-text-centered has-text-dark">Sculptures and Themes</h1>
            <div class="gallery">
EOL

count=1
for img in "$IMAGE_DIR"/*.{jpg,jpeg,png,gif,webp}; do
    if [[ -f "$img" ]]; then
        filename=$(basename "$img")
        caption_file="${img%.*}.txt"

        if [[ -f "$caption_file" ]]; then
            caption=$(cat "$caption_file")
        else
            caption=""
        fi

        printf "                <div class=\"gallery-item\">\n" >> "$OUTPUT_FILE"
        printf "                    <figure class=\"image is-square\">\n" >> "$OUTPUT_FILE"
        printf "                        <img src=\"%s\" alt=\"%s\" data-target=\"modal%d\" aria-haspopup=\"true\">\n" "$IMAGE_DIR/$filename" "$caption" "$count" >> "$OUTPUT_FILE"
        printf "                    </figure>\n" >> "$OUTPUT_FILE"
        printf "                    <p class=\"caption\">%s</p>\n" "$caption" >> "$OUTPUT_FILE"
        printf "                </div>\n" >> "$OUTPUT_FILE"

        # Modal structure
        printf "                <div class=\"modal\" id=\"modal%d\">\n" "$count" >> "$OUTPUT_FILE"
        printf "                    <div class=\"modal-background\"></div>\n" >> "$OUTPUT_FILE"
        printf "                    <div class=\"modal-card\">\n" >> "$OUTPUT_FILE"
        printf "                        <header class=\"modal-card-head\">\n" >> "$OUTPUT_FILE"
        printf "                            <p class=\"modal-card-title\">%s</p>\n" "$caption" >> "$OUTPUT_FILE"
        printf "                            <button class=\"delete\" aria-label=\"close\" data-action=\"close\"></button>\n" >> "$OUTPUT_FILE"
        printf "                        </header>\n" >> "$OUTPUT_FILE"
        printf "                        <section class=\"modal-card-body\">\n" >> "$OUTPUT_FILE"
        printf "                            <img class=\"modal-image\" src=\"%s\" alt=\"%s\">\n" "$IMAGE_DIR/$filename" "$caption" >> "$OUTPUT_FILE"
        printf "                        </section>\n" >> "$OUTPUT_FILE"
        printf "                    </div>\n" >> "$OUTPUT_FILE"
        printf "                </div>\n" >> "$OUTPUT_FILE"

        count=$((count+1))
    fi
done

cat >> "$OUTPUT_FILE" <<EOL
            </div>
        </div>
    </section>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            function openModal(modalId) {
                const modal = document.getElementById(modalId);
                modal.classList.add('is-active');
            }

            function closeModal(modal) {
                modal.classList.remove('is-active');
            }

            const images = document.querySelectorAll('.gallery img');
            images.forEach(image => {
                image.addEventListener('click', () => {
                    const targetModal = image.dataset.target;
                    openModal(targetModal);
                });
            });

            const closeButtons = document.querySelectorAll('.modal .delete[data-action="close"]');
            closeButtons.forEach(button => {
                button.addEventListener('click', () => {
                    const modal = button.closest('.modal');
                    closeModal(modal);
                });
            });

            const modalBackgrounds = document.querySelectorAll('.modal-background');
            modalBackgrounds.forEach(background => {
                background.addEventListener('click', () => {
                    const modal = background.closest('.modal');
                    closeModal(modal);
                });
            });
        });
    </script>
        <footer class="footer has-background-primary-light">
      <div class="content has-text-centered">
        <div>
          <a target="_blank" href="https://mastodon.social/@beachpartyballoons@mastodon.social"><i class="fa-brands fa-mastodon is-size-2"></i>
</a>
<a target="_blank" href="https://www.facebook.com/beachpartyballoons"><i class="fa-brands fa-facebook-f is-size-2"></i>
</a>
<a target="_blank" href="https://www.instagram.com/beachpartyballoons/"><i class="fa-brands fa-instagram is-size-2"></i>
</a>
<a target="_blank" href="https://bsky.app/profile/beachpartyballoons.bsky.social">
<i class="fa-brands fa-bluesky is-size-2"></i>
</a>
        </div>

          <h7>Copyright &copy; <span id="year"></span> Beach Party Balloons</h7>
          <h7>All images & content are property of Beach Party Balloons. Use of images without written permission is prohibited.</h7>
      </div>
    </footer>
    <script src="../../script.js"></script>
</body>
</html>
EOL

echo "Gallery generated with Bulma modals! Open $OUTPUT_FILE in your browser."