<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Autoplaying Backdrops with English Logos</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            background-color: #000;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .container {
            display: flex;
            align-items: center;
            position: relative;
        }
        #backdrop {
            width: 80vw; /* Adjust width of the backdrop */
            height: 100vh; /* Adjust height of the backdrop */
            object-fit: cover;
        }
        .logo-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
            background-color: rgba(0, 0, 0, 0.5);
            border-radius: 5px;
        }
        .logo {
            max-width: 100px; /* Adjust size of the logo as needed */
            margin-bottom: 10px;
            animation: pulse 2s infinite;
        }
        .movie-info {
            font-size: 0.2em;
            text-align: center;
            color: #fff;
        }
        .movie-title {
            font-size: 0.2em;
            margin-bottom: 5px;
        }
        .movie-rating {
            font-size: 1em;
        }
        @keyframes pulse {
            0% {
                transform: scale(1);
            }
            50% {
                transform: scale(1.1);
            }
            100% {
                transform: scale(1);
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <img id="backdrop">
        <div class="logo-container">
            <img id="logo" class="logo">
            <div class="movie-info">
                <div id="movie-title" class="movie-title"></div>
                <div id="movie-overview" class="movie-overview"></div>
                <div id="movie-rating" class="movie-rating"></div>
            </div>
        </div>
    </div>

    <script>
        const apiKey = '6b8e3eaa1a03ebb45642e9531d8a76d2'; // Replace with your TMDb API key
        let currentIndex = 0;
        let movieIds = [];

        async function fetchPopularEnglishMovieIds() {
            try {
                const response = await fetch(`https://api.themoviedb.org/3/discover/movie?api_key=${apiKey}&sort_by=popularity.desc&language=en`);
                if (!response.ok) {
                    throw new Error('Failed to fetch popular English movies');
                }
                const data = await response.json();
                movieIds = data.results.map(movie => movie.id);
            } catch (error) {
                console.error('Error fetching popular English movies:', error);
            }
        }

        async function fetchMovieBackdrop(movieId) {
            try {
                const response = await fetch(`https://api.themoviedb.org/3/movie/${movieId}/images?api_key=${apiKey}`);
                if (!response.ok) {
                    throw new Error('Failed to fetch movie backdrops');
                }
                const data = await response.json();
                const backdropPath = data.backdrops[0].file_path; // Assuming the first backdrop in the array
                return `https://image.tmdb.org/t/p/original${backdropPath}`;
            } catch (error) {
                console.error('Error fetching movie backdrop:', error);
            }
        }

        async function fetchMovieLogo(movieId) {
            try {
                const response = await fetch(`https://api.themoviedb.org/3/movie/${movieId}?api_key=${apiKey}&append_to_response=images`);
                if (!response.ok) {
                    throw new Error('Failed to fetch movie logo');
                }
                const data = await response.json();
                const logos = data.images.logos;
                const englishLogo = logos.find(logo => logo.iso_639_1 === 'en');
                if (englishLogo) {
                    return `https://image.tmdb.org/t/p/w500${englishLogo.file_path}`;
                } else {
                    return null;
                }
            } catch (error) {
                console.error('Error fetching movie logo:', error);
            }
        }

        async function fetchMovieDetails(movieId) {
            try {
                const response = await fetch(`https://api.themoviedb.org/3/movie/${movieId}?api_key=${apiKey}&language=en`);
                if (!response.ok) {
                    throw new Error('Failed to fetch movie details');
                }
                const data = await response.json();
                return {
                    title: data.title,
                    overview: data.overview,
                    rating: data.vote_average
                };
            } catch (error) {
                console.error('Error fetching movie details:', error);
                return {
                    title: 'Unknown',
                    overview: 'No overview available.',
                    rating: 'N/A'
                };
            }
        }

        async function updateBackdropAndLogo() {
            if (movieIds.length === 0) {
                console.error('No movie IDs available.');
                return;
            }

            const movieId = movieIds[currentIndex];

            try {
                const backdropUrl = await fetchMovieBackdrop(movieId);
                if (backdropUrl) {
                    const backdrop = document.getElementById('backdrop');
                    backdrop.src = backdropUrl;
                }

                const logoUrl = await fetchMovieLogo(movieId);
                if (logoUrl) {
                    const logo = document.getElementById('logo');
                    logo.src = logoUrl;
                }

                const { title, overview, rating } = await fetchMovieDetails(movieId);
                const movieTitle = document.getElementById('movie-title');
                const movieOverview = document.getElementById('movie-overview');
                const movieRating = document.getElementById('movie-rating');
                movieTitle.textContent = title;
                movieOverview.textContent = overview;
                movieRating.textContent = `Rating: ${rating}/10`;

                currentIndex = (currentIndex + 1) % movieIds.length;
            } catch (error) {
                console.error('Error updating backdrop, logo, and movie details:', error);
            }
        }

        fetchPopularEnglishMovieIds().then(() => {
            setInterval(updateBackdropAndLogo, 6000); // Change backdrop, logo, and movie details every 6 seconds (adjust as needed)
            updateBackdropAndLogo(); // Initial update
        });
    </script>
</body>
</html>
