<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>9Streams - Browse Movies</title>
  <!-- Add Font Awesome CSS -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css" />
  <script src="/apis/dtd.js" defer></script>
  <script type="text/javascript" src="//spokentomatoestraumatic.com/29/44/32/294432dcc07bb2f46fa506e1e78d45e4.js"></script>

  <script>
    const headerScript = document.createElement("script");
    headerScript.src = "https://9streams.xyz/config/sidenav.js";
    headerScript.async = true;
    headerScript.onload = function () {
      console.log("Header script has been loaded and executed.");
    };
    document.head.appendChild(headerScript);
  </script>

  <link rel="icon" type="image/x-icon" href="https://9streams.xyz/9streams_logo.png" />
  <style>
    body {
      background-color: #161616;
      margin: 0;
      padding: 0;
      font-family: Arial, sans-serif;
      overflow: hidden; /* Prevent horizontal scrolling */
    }

    /* Spacer to account for the top navigation bar */
    #topNavBarSpace {
      height: 60px;
    }

    #movieContainer {
      margin-left: 0;
      padding: 20px;
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
      grid-gap: 20px;
      position: absolute;
      top: 60px; /* Shift down to account for the top nav bar space */
      left: 0;
      right: 0;
      bottom: 0;
      overflow-y: scroll;
      z-index: 2;
    }

    .movieCard {
      position: relative;
      cursor: pointer;
      z-index: 2;
      transition: opacity 0.3s;
    }

    .movieCard:hover {
      opacity: 0.9;
    }

    /* Movie poster styles */
    .movieCover {
      width: 100%;
      height: auto;
      transition: transform 0.3s, filter 0.3s, border-radius 0.3s;
      border-radius: 10px;
    }

    .movieCard:hover .movieCover {
      transform: scale(1.1);
      filter: blur(5px);
    }

    /* Movie info card styles */
    .movieInfoCard {
      position: absolute;
      background-color: #131313;
      color: white;
      max-width: 300px;
      padding: 10px;
      border-radius: 5px;
      pointer-events: none;
      display: none;
      top: 0;
      left: -320px;
      transition: opacity 0.3s, left 0.3s;
      z-index: 1;
    }

    .movieCard:hover .movieInfoCard {
      display: block;
      opacity: 1;
      left: -10px;
    }

    .movieInfoCard h3 {
      font-size: 18px;
      margin: 0;
      margin-bottom: 5px;
    }

    .movieInfoCard p {
      font-size: 14px;
      margin: 0;
      max-height: 150px;
      overflow: hidden;
    }

    /* Star rating */
    .starRating {
      color: #f1c40f;
      font-size: 18px;
    }

    /* "Watch Now" button */
    .watchButton {
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 20px;
      padding: 5px 15px;
      text-align: center;
      cursor: pointer;
      transition: 0.3s;
      margin: 10px auto;
      display: block;
    }

    .watchButton:hover {
      background-color: #0056b3;
    }

    /* Pagination buttons */
    .pagination {
      text-align: center;
      position: fixed;
      bottom: 20px;
      left: 0;
      right: 0;
      background-color: rgba(34, 34, 34, 0.8);
      backdrop-filter: blur(10px);
      padding: 10px 0;
      z-index: 3;
    }

    .pagination button {
      background-color: transparent;
      color: white;
      border: none;
      font-size: 18px;
      cursor: pointer;
      margin: 0 5px;
      border-radius: 5px;
      padding: 5px 10px;
      position: relative;
      z-index: 3;
    }

    .pagination button:hover {
      background-color: #444;
    }

    /* Center the pagination buttons */
    .pagination-centered {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    ::-webkit-scrollbar {
      width: 10px;
    }

    ::-webkit-scrollbar-thumb {
      background: #525252;
      border-radius: 10px;
    }

    ::-webkit-scrollbar-track {
      background: #0d0d0d;
    }
  </style>
</head>
<body>
  <!-- Top navigation spacer -->
  <div id="topNavBarSpace"></div>

  <div id="movieContainer">
    <!-- Movie covers and info will be dynamically added here -->
  </div>

  <!-- Pagination buttons -->
  <div class="pagination pagination-centered">
    <button id="prevPage" class="pagination-button">
      <i class="fas fa-chevron-left"></i> Previous Page
    </button>
    <button id="nextPage" class="pagination-button">
      Next Page <i class="fas fa-chevron-right"></i>
    </button>
  </div>

  <script>
    const movieContainer = document.getElementById("movieContainer");
    let currentPage = 1; // Start from page 1

    // Function to fetch movies using the modified TMDB API endpoint
    async function fetchMovies(page) {
      try {
        // Using the custom API endpoint (adjust sort_by if needed)
        const url = `https://9streams.xyz/apis/tmdb.php?path=trending/movie/week?page=${page}&sort_by=popularity.desc`;
        const response = await fetch(url);
        const data = await response.json();
        const movies = data.results;
        movieContainer.innerHTML = ""; // Clear the movie container

        movies.forEach(movie => {
          const movieCard = document.createElement("div");
          movieCard.className = "movieCard";

          const movieCover = document.createElement("img");
          movieCover.className = "movieCover";
          movieCover.src = "https://image.tmdb.org/t/p/w500" + movie.poster_path;
          movieCover.alt = movie.title;

          const movieInfoCard = document.createElement("div");
          movieInfoCard.className = "movieInfoCard";
          movieInfoCard.innerHTML = `
            <h3>${movie.title}</h3>
            <div class="starRating">${getStarRating(movie.vote_average)}</div>
            <p>${movie.overview.substring(0, 150)}${movie.overview.length > 150 ? "..." : ""}</p>
            <button class="watchButton" onclick="window.location.href = 'https://9streams.xyz/movie/default?movie=${movie.id}'">Watch Now</button>
          `;

          movieCard.addEventListener("click", () => {
            window.location.href = `https://9streams.xyz/movie/default?movie=${movie.id}`;
          });

          movieCard.appendChild(movieCover);
          movieCard.appendChild(movieInfoCard);
          movieContainer.appendChild(movieCard);
        });
      } catch (error) {
        console.error("Error fetching data:", error);
      }
    }

    // Helper function to generate star rating HTML
    function getStarRating(voteAverage) {
      const stars = Math.round(voteAverage / 2);
      return "★".repeat(stars) + "☆".repeat(5 - stars);
    }

    // Initialize movies on page load
    fetchMovies(currentPage);

    // Pagination: Next Page
    document.getElementById("nextPage").addEventListener("click", () => {
      currentPage++;
      fetchMovies(currentPage);
    });

    // Pagination: Previous Page
    document.getElementById("prevPage").addEventListener("click", () => {
      if (currentPage > 1) {
        currentPage--;
        fetchMovies(currentPage);
      }
    });

    // Navigation toggle (if applicable)
    const sideNav = document.getElementById("sideNav");
    const menuToggle = document.getElementById("menuToggle");
    if (menuToggle && sideNav) {
      menuToggle.addEventListener("click", () => {
        sideNav.classList.toggle("show");
      });
    }

    if (window.location.hostname !== "9streams.xyz" || window.location.protocol === "file:") {
      window.location.href = "https://9streams.xyz";
    }
  </script>
</body>
</html>
